One of my homework questions, and I am only allowed to import pi.
The question asks to compute the cosine function by Taylor series, which I have done so far. The outputs I get are correct however when k gets larger than 90, I get OverflowError: int too large to convert to float.
from math import pi
def myCos(angle,k):
total=0
for i in range(k):
total += (((-1)**(i))*((angle*pi/180)**(2*(i))))/(fact(2*(i)))
return total
def fact(n):
if n == 0:
return 1
else:
return fact(n-1)*n
In order to get full marks for this question, the code has to accept k > 100.
i.e
myCos(45,5)
0.7071068056832942
myCos(45,60)
0.7071067811865475
myCos(45,90)
total += (((-1)**(i))*((angle*pi/180)**(2*(i))))/(fact(2*(i)))
OverflowError: int too large to convert to float
Can someone please enlighten me on this?
Your main division in the summation exceeded the range of type float. To do that division, you have to convert the denominator to type float. fact(2*85) is larger than the maximum float value.
Do the operations in a difference order. For instance:
numer = (angle*pi/180)**(2*(i))
for denom in range(1, 2*i + 1):
numer /= denom
Now numer is a reasonable (?) representation of the value desired. If you need better reliability, "chunk" the denominator values and divide by the product of, say, groups of 10 denom values.
def myCos(angle,k):
total=0
for i in range(k):
numer = (angle*pi/180)**(2*(i))
for denom in range(2, 2*i+1):
numer /= denom
total += (-1) ** i * numer
return total
print(myCos(45,5))
print(myCos(45,60))
print(myCos(45,90))
Output:
0.7071068056832942
0.7071067811865475
0.7071067811865475
Related
I've been trying to convert this math formula into python.
So basically the approximation of 1/math.pi.
I tried making this iteration with
import math
N = 132
for k in range(1, N+1):
print("The N value is",N , end="\r", flush=True)
rec_pi_approximate = (2*math.sqrt(2)/9801) * N * ((math.factorial(4*k) * (1103+26390*k))/((math.factorial(k)**4) * (396**4*k)))
But my values have been weird and I need guidance on how to actually type the formula and make a summation loop for it. Currently I am trying to find the reciprocal of pi with this formula equivalent to 1/math.pi
The idea with the for loop is that you add up a part of the summation on each iteration. So you should keep track of the total sum so far and add to it each time. Because of math properties, you can multiply by the constant that’s outside the summation while summing so you don't need to at the end.
import math
N = 132
pi_approximate_inv = 0
for k in range(0, N+1):
print("The N value is", k)
pi_approximate_inv += (2*math.sqrt(2)/9801) * ((math.factorial(4*k) * (1103+26390*k))/((math.factorial(k)**4) * (396**(4*k))))
print("The pi approximate is", 1.0/pi_approximate_inv)
You also missed some parentheses at (396**(4*k)) which I added. Also, it should be print("The N value is", k) (k not N) because it is showing what the result would be if it stopped right there at k.
Because of floating point precision limits, it appears to max out at N=2 (meaning 3 iterations).
fraction.Fraction.limit_denominator exists, and finds the closest fraction that has a denominator smaller than a given maximum. But there is no obvious way to limit the numerator.
What is the best way to also limit the numerator? (I.e. to find the closest fraction that has both numerator and denominator smaller than a given maximum.)
(Goal: I need to limit the numerator also to 2**32, because TIFF files store fractions as two unsigned 32-bit integers.)
A crude approach (that probably does not find the truly closest fraction):
from fractions import Fraction
def limit_32bit_rational(f: float) -> Fraction:
ndigits = 15
while True:
r = Fraction.from_float(f).limit_denominator(1000000)
if r.numerator < 2**32:
return r
f = round(f, ndigits)
ndigits -= 1
Is there a better way to find the closest fraction that has nominator and denominator less than 2**32?
You can use the float.as_integer_ratio() method to get the numerator and denominator of any float:
f = 2345.245624
numerator, denominator = (f).as_integer_ratio()
print("Numerator", numerator)
Output:
2578624833578781
EDIT:
You can try using an if statement to check if the numerator or denominator is greater than 2 ** 32; if so, multiply each of them by a scale and round them to the nearest whole numbers:
def limit_32bit_rational(f):
numerator, denominator = (f).as_integer_ratio()
max_num = 2 ** 32
if numerator > max_num or denominator > max_num:
scale = max_num / max(numerator, denominator)
return round(numerator * scale), round(denominator * scale)
return numerator, denominator
You could also flip the fraction and call limit_denominator to limit the numerator and flip the result back again:
def limit_32bit_rational(f: float) -> Fraction:
tmp = Fraction(f).limit_denominator(0xFFFF_FFFF)
if tmp.numerator <= 0xFFFF_FFFF:
# makes sure we don't flip if numerator is 0
return tmp
else:
# flip numerator with denominator to limit the inputs numerator
tmp = Fraction(numerator=tmp.denominator, denominator=tmp.numerator).limit_denominator(0xFFFF_FFFF)
return Fraction(numerator=tmp.denominator, denominator=tmp.numerator)
I am trying to define a function which will approximate pi in python using one of Euler's methods. His formula is as follows:
My code so far is this:
def pi_euler1(n):
numerator = list(range(2 , n))
for i in numerator:
j = 2
while i * j <= numerator[-1]:
if i * j in numerator:
numerator.remove(i * j)
j += 1
for k in numerator:
if (k + 1) % 4 == 0:
denominator = k + 1
else:
denominator = k - 1
#Because all primes are odd, both numbers inbetween them are divisible by 2,
#and by extension 1 of the 2 numbers is divisible by 4
term = numerator / denominator
I know this is wrong, and also incomplete. I'm just not quite sure what the TypeError that I mentioned earlier actually means. I'm just quite stuck with it, I want to create a list of the terms and then find their products. Am I on the right lines?
Update:
I have worked ways around this, fixing the clearly obvious errors that were prevalent thanks to msconi and Johanc, now with the following code:
import math
def pi_euler1(n):
numerator = list(range(2 , 13 + math.ceil(n*(math.log(n)+math.log(math.log(n))))))
denominator=[]
for i in numerator:
j = 2
while i * j <= numerator[-1]:
if (i * j) in numerator:
numerator.remove(i * j)
j += 1
numerator.remove(2)
for k in numerator:
if (k + 1) % 4 == 0:
denominator.append(k+1)
else:
denominator.append(k-1)
a=1
for i in range(n):
a *= numerator[i] / denominator[i]
return 4*a
This seems to work, when I tried to plot a graph of the errors from pi in a semilogy axes scale, I was getting a domain error, but i needed to change the upper bound of the range to n+1 because log(0) is undefined. Thank you guys
Here is the code with some small modifications to get it working:
import math
def pi_euler1(n):
lim = n * n + 4
numerator = list(range(3, lim, 2))
for i in numerator:
j = 3
while i * j <= numerator[-1]:
if i * j in numerator:
numerator.remove(i * j)
j += 2
euler_product = 1
for k in numerator[:n]:
if (k + 1) % 4 == 0:
denominator = k + 1
else:
denominator = k - 1
factor = k / denominator
euler_product *= factor
return euler_product * 4
print(pi_euler1(3))
print(pi_euler1(10000))
print(math.pi)
Output:
3.28125
3.148427801913721
3.141592653589793
Remarks:
You only want the odd primes, so you can start with a list of odd numbers.
j can start with 3 and increment in steps of 2. In fact, j can start at i because all the multiples of i smaller than i*i are already removed earlier.
In general it is very bad practise to remove elements from the list over which you are iterating. See e.g. this post. Internally, Python uses an index into the list over which it iterates. Coincidently, this is not a problem in this specific case, because only numbers larger than the current are removed.
Also, removing elements from a very long list is very slow, as each time the complete list needs to be moved to fill the gap. Therefore, it is better to work with two separate lists.
You didn't calculate the resulting product, nor did you return it.
As you notice, this formula converges very slowly.
As mentioned in the comments, the previous version interpreted n as the limit for highest prime, while in fact n should be the number of primes. I adapted the code to rectify that. In the above version with a crude limit; the version below tries a tighter approximation for the limit.
Here is a reworked version, without removing from the list you're iterating. Instead of removing elements, it just marks them. This is much faster, so a larger n can be used in a reasonable time:
import math
def pi_euler_v3(n):
if n < 3:
lim = 6
else:
lim = n*n
while lim / math.log(lim) / 2 > n:
lim //= 2
print(n, lim)
numerator = list(range(3, lim, 2))
odd_primes = []
for i in numerator:
if i is not None:
odd_primes.append(i)
if len(odd_primes) >= n:
break
j = i
while i * j < lim:
numerator[(i*j-3) // 2] = None
j += 2
if len(odd_primes) != n:
print(f"Wrong limit calculation, only {len(odd_primes)} primes instead of {n}")
euler_product = 1
for k in odd_primes:
denominator = k + 1 if k % 4 == 3 else k - 1
euler_product *= k / denominator
return euler_product * 4
print(pi_euler_v2(100000))
print(math.pi)
Output:
3.141752253548891
3.141592653589793
In term = numerator / denominator you are dividing a list by a number, which doesn't make sense. Divide k by the denominator in the loop in order to use the numerator element for each of the equation's factors one by one. Then you could multiply them repeatedly to the term term *= i / denominator, which you initialize in the beginning as term = 1.
Another issue is the first loop, which won't give you the first n prime numbers. For example, for n=3, list(range(2 , n)) = [2]. Therefore, the only prime you will get is 2.
my homework is "Write a function that takes in two numbers and recursively multiplies them together", the solution for integers appears in the website in few questions, but is is possible for floats i wrote a code that can handle one float but I can't imagine how to deal with 2 as a novice i wonder is there such possibility that's my code
def multy(n, m):
# deals with one float situation
if m % 1 != 0:
return multy(m, n)
else:
if m == 0:
return 0
elif m < 0:
return -(n - multy(n, m+1))
else:
return n + multy(n, m-1)
You can "convert" your float to int and use your multy function, then "convert" it back to the float:
def fmulty(n, m):
nd = len(str(n).split('.')[1])
md = len(str(m).split('.')[1])
return multy(n * pow(10, nd), m * pow(10, md)) / pow(10, md+nd)
The idea is to make n and m integers by shifting the decimal to the right. For instance you have n = 1.234, then you need to multiply by 1,000 (nd=3). The same for m in md. Once done, you can use multy and divide the result by 1E(nd+md). Let say m = 5.67 then you have md = 2. You then need to divide the result by 1E5, i.e. 100,000.
I'm trying to maximize the Euler Totient function on Python given it can use large arbitrary numbers. The problem is that the program gets killed after some time so it doesn't reach the desired ratio. I have thought of increasing the starting number into a larger number, but I don't think it's prudent to do so. I'm trying to get a number when divided by the totient gets higher than 10. Essentially I'm trying to find a sparsely totient number that fits this criteria.
Here's my phi function:
def phi(n):
amount = 0
for k in range(1, n + 1):
if fractions.gcd(n, k) == 1:
amount += 1
return amount
The most likely candidates for high ratios of N/phi(N) are products of prime numbers. If you're just looking for one number with a ratio > 10, then you can generate primes and only check the product of primes up to the point where you get the desired ratio
def totientRatio(maxN,ratio=10):
primes = []
primeProd = 1
isPrime = [1]*(maxN+1)
p = 2
while p*p<=maxN:
if isPrime[p]:
isPrime[p*p::p] = [0]*len(range(p*p,maxN+1,p))
primes.append(p)
primeProd *= p
tot = primeProd
for f in primes:
tot -= tot//f
if primeProd/tot >= ratio:
return primeProd,primeProd/tot,len(primes)
p += 1 + (p&1)
output:
totientRatio(10**6)
16516447045902521732188973253623425320896207954043566485360902980990824644545340710198976591011245999110,
10.00371973209101,
55
This gives you the smallest number with that ratio. Multiples of that number will have the same ratio.
n = 16516447045902521732188973253623425320896207954043566485360902980990824644545340710198976591011245999110
n*2/totient(n*2) = 10.00371973209101
n*11*13/totient(n*11*13) = 10.00371973209101
No number will have a higher ratio until you reach the next product of primes (i.e. that number multiplied by the next prime).
n*263/totient(n*263) = 10.041901868473037
Removing a prime from the product affects the ratio by a proportion of (1-1/P).
For example if m = n/109, then m/phi(m) = n/phi(n) * (1-1/109)
(n//109) / totient(n//109) = 9.91194248684247
10.00371973209101 * (1-1/109) = 9.91194248684247
This should allow you to navigate the ratios efficiently and find the numbers that meed your need.
For example, to get a number with a ratio that is >= 10 but closer to 10, you can go to the next prime product(s) and remove one or more of the smaller primes to reduce the ratio. This can be done using combinations (from itertools) and will allow you to find very specific ratios:
m = n*263/241
m/totient(m) = 10.000234225865265
m = n*(263...839) / (7 * 61 * 109 * 137) # 839 is 146th prime
m/totient(m) = 10.000000079805726
I have a partial solution for you, but the results don't look good.. (this solution may not give you an answer with modern computer hardware (amount of ram is limiting currently)) I took an answer from this pcg challenge and modified it to spit out ratios of n/phi(n) up to a particular n
import numba as nb
import numpy as np
import time
n = int(2**31)
#nb.njit("i4[:](i4[:])", locals=dict(
n=nb.int32, i=nb.int32, j=nb.int32, q=nb.int32, f=nb.int32))
def summarum(phi):
#calculate phi(i) for i: 1 - n
#taken from <a>https://codegolf.stackexchange.com/a/26753/42652</a>
phi[1] = 1
i = 2
while i < n:
if phi[i] == 0:
phi[i] = i - 1
j = 2
while j * i < n:
if phi[j] != 0:
q = j
f = i - 1
while q % i == 0:
f *= i
q //= i
phi[i * j] = f * phi[q]
j += 1
i += 1
#divide each by n to get ratio n/phi(n)
i = 1
while i < n: #jit compiled while loop is faster than: for i in range(): blah blah blah
phi[i] = i//phi[i]
i += 1
return phi
if __name__ == "__main__":
s1 = time.time()
a = summarum(np.zeros(n, np.int32))
locations = np.where(a >= 10)
print(len(locations))
I only have enough ram on my work comp. to test about 0 < n < 10^8 and the largest ratio was about 6. You may or may not have any luck going up to larger n, although 10^8 already took several seconds (not sure what the overhead was... spyder's been acting strange lately)
p55# is a sparsely totient number satisfying the desired condition.
Furthermore, all subsequent primorial numbers are as well, because pn# / phi(pn#) is a strictly increasing sequence:
p1# / phi(p1#) is 2, which is positive. For n > 1, pn# / phi(pn#) is equal to pn-1#pn / phi(pn-1#pn), which, since pn and pn-1# are coprime, is equal to (pn-1# / phi(pn-1#)) * (pn/phi(pn)). We know pn > phi(pn) > 0 for all n, so pn/phi(pn) > 1. So we have that the sequence pn# / phi(pn#) is strictly increasing.
I do not believe these to be the only sparsely totient numbers satisfying your request, but I don't have an efficient way of generating the others coming to mind. Generating primorials, by comparison, amounts to generating the first n primes and multiplying the list together (whether by using functools.reduce(), math.prod() in 3.8+, or ye old for loop).
As for the general question of writing a phi(n) function, I would probably first find the prime factors of n, then use Euler's product formula for phi(n). As an aside, make sure to NOT use floating-point division. Even finding the prime factors of n by trial division should outperform computing gcd n times, but when working with large n, replacing this with an efficient prime factorization algorithm will pay dividends. Unless you want a good cross to die on, don't write your own. There's one in sympy that I'm aware of, and given the ubiquity of the problem, probably plenty of others around. Time as needed.
Speaking of timing, if this is still relevant enough to you (or a future reader) to want to time... definitely throw the previous answer in the mix as well.