In [1]:
function to_dec(b,x)
ret = 0
pos = 0
while x >= 10^pos
digit = Int(trunc((x % (10^(pos+1)))/(10^pos)))
ret += digit * b^pos
pos += 1
end
return ret
end
Out[1]:
In [2]:
println("5240 base 6 in decimal is ", to_dec(6, 5240))
println("12012 base 3 in decimal is ", to_dec(3, 12012))
println("888 base 9 in decimal is ", to_dec(9, 888))
In [3]:
function cnt_dgt(b,x)
pos = 0
while (x / b^pos) > 1
pos += 1
end
return pos
end
Out[3]:
In [4]:
for i in 2:9
println("1111 base 10 needs ", cnt_dgt(i, 1111), " digits in base ", i)
end
In [ ]: