Monday, 2 September 2013

How to convert an int to base 9, with no 0's?

How to convert an int to base 9, with no 0's?

How can I convert a number to its equivalent in base9 with the caveat that
its base9 has no "0" digit.
For example:
Base10 = 1, 2, 3, .. 9, 10, 12 ... 28, 29, 30, ... 62, 63, 64, etc
Base9 = 1, 2, 3, .. 9, 11, 12 ... 31, 32, 33, ... 68, 69, 71, etc
I'm new to python and programming in general, not to mention my lack of
knowledgeable as far as math is concerned, so my first attempt is rather
sloppy. It only works on smaller numbers and I have a feeling there's a
much easier way to do this.
def base9(n):
count = 0
x = xrange(10,100,9)
while count != x:
if x < n[count]:
return x + count
count += 1
for each in range(1,91):
print base9(each)

No comments:

Post a Comment