-3

How to transfer number from 3rd number system to 10th, i have a example from 10th to 7 here:

def fromtenthtoseven(x):
  result=" "
  while(x>0):
    result+=str(x%7)
    x=x//7
  return(int(result[::-1]))
print(fromtenthtoseven(512))

10th system is 512 and in 7th will be 1331

Minko
  • 1
  • 3

1 Answers1

0

being lazy I kept your piece of code that converts from base10 to base7 and added something to convert to base10.

def fromtenthtoseven(x, frombase, tobase):
    result=" "
    if (frombase != 10):
        sx = str(x)
        p = 1
        b = 0
        while (sx != ""):
            d = int(sx[-1])
            sx = sx[:-1]
            b = b + d * p
            p = p * frombase
    else:
        b = x
  
    while(b>0):
        result+=str(b%tobase)
        b=b//tobase
    return(int(result[::-1]))
print(fromtenthtoseven(512, 10, 7))
print(fromtenthtoseven(1331, 7, 10))
print(fromtenthtoseven(1331, 7, 5))

The result looks like

$ python x.py
1331
512
4022

There are certainly a billion ways to do this better, but it works

Ronald
  • 2,842
  • 16
  • 16