Reputation: 8109
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
(see above).
Is there a better way to find exponents and bases when only the result is known?
Upvotes: 0
Views: 658
Reputation: 3382
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
If you want to add fractions, with n decimal digits after the dot, you can use this:
from __future__ import division
import math
num=1845.28125
decimal_digits=1
ans=3
x=1
while(ans>=2):
ans=num**(1/x)
if (ans*10**decimal_digits).is_integer():
print ans,x
x+=1
where decimal_digits
indicates the number of places after the dot.
For this example the answer will be
4.5 5
,
If you change for example num
to 39.0625
and decimal_digits
to 2, the output will be:
2.5 4
6.25 2
Upvotes: 1
Reputation: 54243
For integers you could look at the prime factors of your number. Once you know that 64 is 2**6
, it's easy to list all the results you wanted.
Now, which result do you expect for numbers that have at least 2 different prime factors? For example : should 15 be written 3*5
, 3**1 * 5**1
or 15**1
?
It's not clear how your problem is defined for Floats.
What's special about 4.5
?
If you calculate 1845.28125**(1.0/5)
, Python returns 4.5
, but for other input numbers, the result might be off by 1e-16.
import math
def find_possible_bases(num, min_base = 1.9, max_decimals = 9, max_diff = 1e-15):
max_exponent = int(math.ceil(math.log(num,min_base)))
for exp in range(1,max_exponent):
base = round(num**(1.0/exp),max_decimals)
diff = abs(base**exp-num)
if diff < max_diff:
print('%.10g ** %d = %.10g' % (base, exp, base ** exp))
find_possible_bases(64)
# 64 ** 1 = 64
# 8 ** 2 = 64
# 4 ** 3 = 64
# 2 ** 6 = 64
find_possible_bases(19683)
# 19683 ** 1 = 19683
# 27 ** 3 = 19683
# 3 ** 9 = 19683
find_possible_bases(1845.28125)
# 1845.28 ** 1 = 1845.28
# 4.5 ** 5 = 1845.28
find_possible_bases(15)
# 15 ** 1 = 15
It iterates over possible exponents, and calculates what the base would be. It rounds it to 9 decimals, and checks what the error becomes. If it's small enough, it displays the result. You could play with the parameters and find what best suits your problem. As a bonus, it also works fine with Integers (e.g. 64 and 15).
It might be better to work with Rational numbers.
Upvotes: 1
Reputation: 36033
Your problem needs more constraints, but here's some help:
>>> from math import log
>>> help(log)
Help on built-in function log in module math:
log(...)
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
>>> for base in range(2, 10):
... exp = log(64, base)
... print('%s ^ %s = %s' % (base, exp, base ** exp))
...
2 ^ 6.0 = 64.0
3 ^ 3.785578521428744 = 63.99999999999994
4 ^ 3.0 = 64.0
5 ^ 2.5840593484403582 = 63.99999999999999
6 ^ 2.3211168434072493 = 63.99999999999998
7 ^ 2.1372431226481328 = 63.999999999999964
8 ^ 2.0 = 64.0
9 ^ 1.892789260714372 = 63.99999999999994
Upvotes: 3