I like experimenting with the Collatz sequence. Today, I would like to display the binary representation of the Collatz sequence with a randomly selected initial point.
The following function generates the sequence (only the odd part) from an initial seed.
(defun collatz (n &optional carry)
(cond
((< n 1) nil)
((= n 1) (cons 1 carry))
((zerop (mod n 2)) (collatz (/ n 2) carry))
((collatz (1+ (* 3 n)) (cons n carry)))))
COLLATZ
And the following code snippet prints the binary representation:
(dolist (x (collatz (random (expt 2 50))))
(format t "~50b~%" x))
NIL
1
101
110101
100011
10111
111101
101000101
110110001
1001000001
110000000101
100000000011
10101010111
1110001111
100101111101
11001010011
10000110111
1011001111
111011111
100111111
110101001
100011011
101111001
11111011
10100111
110111101
1001010001
110001011
100000111
10101111
11101001
10011011
1100111
10001001
1011011
1111001
10100001
1101011
1000111
101111
1111101
1010011
110111
100100101
1100001101
100000100010101
10101101100011
11100111011001
100110100100001
110011011010110101
1000100100011110001
101101101101001011
1111001111000011101
1010001010000010011
110110001010110111
100100000111001111
110000001001101001
100000000110011011
10101011001100111
11100100010001001
10011000001011011
1100101011100111
10000111010001001
1011010001011011
1111000001111001
10100000010100001
11010101110000001
100011101000000001
101111100000000001
11111101010101011
10101000111000111
1110000100101111
1001011000011111
1100100000101001
10000101011100001
1011000111101011
11101101001110010101
1001111000100110001101
110100101101110110011
1000110010010011101110101
101110110110111110100011
11111001111010100010111
10100110100111000001111
1101111000100101011111
1001010000011000111111
1100010101110110101001
1000001110100100011011
1010111110000101111001
111010100000011111011
100111000000010100111
1101000000000110111101
1000101010101111010011
101110001110100110111
11110110100011001111
10100100010111011111
1101101100100111111
10010010000110101001
11000010110011100001
10000001110111101011
1010110100101000111
1110011011100001001
1001100111101011011
110011010011100111
1000100011010001001
1011011001101100001
111100110011101011
1010001000100111001
110110000011010000101
1001000000100010110001
11000000001011100101101
10000000000111101110011
1010101011010011110111
111000111100010100100101
10010111110110001100001101
110010100111011001011001101
1000011011111001100100010001
1011001111110111011011000001
111011111111010010010000000101
100111111111100001100000000011
11010101010010110010101010111
100011100011001000011100011110101
101111011001100000100101111110001
1111110011001010110111010100101101010101
10101000100001110011111000110010001110001
1110000010110100010100101110110110100000101
10010101110011011000110010011110011010110001
11000111101111001011101101111101111001000001
10000101001010000111110011111110100110000000101
10110001100010110101000101010011011101010110001
11101100101110011100000111000100100111001000001
1001110111010001001010111101100001101000010101101
110100100110110000111010011101011110000001110011
1000110001001000001001101111100101000000010011001
1011101100001010110111101010000110101011000100001
111110010110001111010011100000100011100101101011
1010011001000010100011010000000101111011100111001
110111011010111000010001010101110100111101111011