Find the pattern of the following sequence and determine the next few terms:
2, 10, 12, 38, 42, 52, 56, 142, 150, 170
(In reply to
Solution (spoiler) by Larry)
I looked at the process differently, and get 212 and 232 as the next two terms in the sequence.
At each step, if one or more pairs of bits within the binary representation are "01" then change them to "10". The result is the next number. If, however, no "01" pairs occur in the binary representation, reverse all the bits, preface the reversed set with a "1" bit and tack a "0" bit at the end, and that's the next number.
The binary representation makes a nice diamond pattern when continued:
2 10
10 1010
12 1100
38 100110
42 101010
52 110100
56 111000
142 10001110
150 10010110
170 10101010
212 11010100
232 11101000
240 11110000
542 1000011110
558 1000101110
598 1001010110
682 1010101010
852 1101010100
936 1110101000
976 1111010000
992 1111100000
2110 100000111110
2142 100001011110
2222 100010101110
2390 100101010110
2730 101010101010
3412 110101010100
3752 111010101000
3920 111101010000
4000 111110100000
4032 111111000000
8318 10000001111110
8382 10000010111110
8542 10000101011110
8878 10001010101110
9558 10010101010110
10922 10101010101010
13652 11010101010100
15016 11101010101000
15696 11110101010000
16032 11111010100000
16192 11111101000000
16256 11111110000000
33022 1000000011111110
33150 1000000101111110
33470 1000001010111110
34142 1000010101011110
35502 1000101010101110
The program:
b$ = "10"
FOR ct = 1 TO 48
n = 0
FOR i = 1 TO LEN(b$)
n = n * 2 + VAL(MID$(b$, i, 1))
NEXT
PRINT n, b$
b2$ = b$
FOR i = 2 TO LEN(b$)
IF MID$(b$, i - 1, 2) = "01" THEN
MID$(b2$, i - 1, 2) = "10"
END IF
NEXT
IF b2$ = b$ THEN
b2$ = "1"
FOR i = LEN(b$) TO 1 STEP -1
b2$ = b2$ + MID$(b$, i, 1)
NEXT
b2$ = b2$ + "0"
END IF
b$ = b2$
NEXT
|
Posted by Charlie
on 2004-12-07 15:47:48 |