Reman - 1 year ago 75

Python Question

I want to find all bases and exponents of a number.

Example:

`Number = 64`

2^6=64

4^3=64

8^2=64

64^1=64

Number = 1845.28125

4.5^5=1845.28125

Number = 19683

3^9=19683

27^3=19683

19683^1=19683

What I do now is to make an integer of 'Number' and just see of the results of multiple calculations gives the correct result:

`basehits, expohits = [], []`

if eval(Number) > 1000:

to = 1000 #base max 1000 in order to avoid too many calculations

else:

to = int(eval(Number))

for n in range(1,to):

for s in range(1,31): #just try with exponents from 1 to 30

calcres = pow(n,s)

if calcres == eval(Number):

basehits.append(n)

expohits.append(s)

elif calcres > eval(Number):

break

The problem is that this never find a Floating Number as for example

`1845.28125`

Is there a better way to find exponents and bases when only the result is known?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

how about

```
import math
num=64
for i in range(2,int(math.sqrt(num))+1):
if math.log(num,i).is_integer():
print i,int(math.log(num,i))
```

the output is:

```
2 6
4 3
8 2
```

and of course, you can always add:

```
print num,1
```

to get

```
64,1
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**