Write a function that calculates and returns the summation o

Write a function that calculates and returns the summation of the inverses of all the primes less than or equal to N: 1/p_1 + 1/p_2 + ... + 1/p_k. Note that p_k is the largest prime less than or equal to N, i.e. P_1

Solution

#This isPrime function is only for testing sumInversePrimes
def isPrime(m):
   count=0
   for i in range(2,m):
       if(m%i==0):
           count = count+1
           break
   if(count==0):
       return 1
   else:
       return 0
def sumInversePrimes(n):
   ans=0
   for i in range(2,n+1):
       if(isPrime(i)==1):
           ans = ans + (1.0/i)
   return ans
print sumInversePrimes(6)

 Write a function that calculates and returns the summation of the inverses of all the primes less than or equal to N: 1/p_1 + 1/p_2 + ... + 1/p_k. Note that p_

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site