First the python:

# convert decimal number to base b. # number, base need to be kets. category_number_to_number() should take care of that. def decimal_to_base(number,base): r = int(category_number_to_number(number).value) b = int(category_number_to_number(base).value) # print("r:",r) # print("b:",b) current_base = 1 result = superposition() while r > 0: rem = r%b r //= b result += ket(str(current_base),rem) current_base *= b return resultNow a couple of examples:

sa: to-base(|255>,|2>) |1> + |2> + |4> + |8> + |16> + |32> + |64> + |128> sa: to-base(|213>,|2>) |1> + 0.000|2> + |4> + 0.000|8> + |16> + 0.000|32> + |64> + |128> sa: to-base(|359822>,|10>) 2.000|1> + 2.000|10> + 8.000|100> + 9.000|1000> + 5.000|10000> + 3.000|100000>Probably don't need any more examples than that, the idea is simple enough!

Update: now with the magic of tables:

sa: table[base,coeff] to-base(|255>,|2>) +------+-------+ | base | coeff | +------+-------+ | 1 | 1 | | 2 | 1 | | 4 | 1 | | 8 | 1 | | 16 | 1 | | 32 | 1 | | 64 | 1 | | 128 | 1 | +------+-------+ sa: table[base,coeff] to-base(|213>,|2>) +------+-------+ | base | coeff | +------+-------+ | 1 | 1 | | 2 | 0 | | 4 | 1 | | 8 | 0 | | 16 | 1 | | 32 | 0 | | 64 | 1 | | 128 | 1 | +------+-------+ sa: table[base,coeff] to-base(|359822007>,|10>) +-----------+-------+ | base | coeff | +-----------+-------+ | 1 | 7 | | 10 | 0 | | 100 | 0 | | 1000 | 2 | | 10000 | 2 | | 100000 | 8 | | 1000000 | 9 | | 10000000 | 5 | | 100000000 | 3 | +-----------+-------+Nice.

Update: now with the magic of "to-comma-number":

sa: table[base,coeff] to-comma-number to-base(|10973498714>,|2>) +---------------+-------+ | base | coeff | +---------------+-------+ | 1 | 0 | | 2 | 1 | | 4 | 0 | | 8 | 1 | | 16 | 1 | | 32 | 0 | | 64 | 1 | | 128 | 0 | | 256 | 1 | | 512 | 0 | | 1,024 | 1 | | 2,048 | 1 | | 4,096 | 0 | | 8,192 | 0 | | 16,384 | 1 | | 32,768 | 0 | | 65,536 | 0 | | 131,072 | 1 | | 262,144 | 0 | | 524,288 | 0 | | 1,048,576 | 1 | | 2,097,152 | 0 | | 4,194,304 | 0 | | 8,388,608 | 0 | | 16,777,216 | 0 | | 33,554,432 | 1 | | 67,108,864 | 1 | | 134,217,728 | 1 | | 268,435,456 | 0 | | 536,870,912 | 0 | | 1,073,741,824 | 0 | | 2,147,483,648 | 1 | | 4,294,967,296 | 0 | | 8,589,934,592 | 1 | +---------------+-------+

## No comments:

## Post a Comment