I have an binary search implemented in python. Now I want to check if element math.floor(n ^ (1/p))
is in my binary search. But p is a very, very large number. I wrote using fractions module
:
binary_search.search(list,int (n**fractions.Fraction('1'+'/'+str(p))))
But I have an error OverflowError: integer division result too large for a float
How can I take to n to the power, which is a fraction and do it fast?
Unless your values of n
are also incredibly large, floor(n^(1/p))
is going to tend toward 1 for "very, very large" values of p. Since you're only interested in the integer portion, you could get away with a simple loop to test if 1^P, 2^p, 3^p and so on are greater than n.
Don't waste time finding exact values if you don't need them.
n^(1/p)=exp(ln(n)/p) ~~ 1+ln(n)/p
for big p values
So you can compare p with natural logarithm of n. If the ratio p/ln(n) >> 1 (much larger), then you can use approximation above (which tends to 1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.