-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfNPrimes.py
More file actions
55 lines (49 loc) · 2.79 KB
/
SumOfNPrimes.py
File metadata and controls
55 lines (49 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#Importing math library to use log function to calculate the upper bound of iteration to go till the n'th prime
import math
#Function to validate input n
def validate_input(n):
try:
num=int(n) #Trying to typecast n into int
if num<0: #Checking for negative input
print("Please enter some non negative value") #Printing error message
return False #Returning false for wrong n
except ValueError: #If the typecast is unsusccessful
print("Please enter a valid non negative integer") #Printing error message
return False #Returning false for wrong n
return True #Returning True if all checks pass
def sum_of_n_primes(n):
primes=[] #Initializing empty list for storing the primes using the Seive algorithm
sum_of_primes=0 #Initializing the sum variable to 0
k=15 if n<6 else int(n * (math.log(n) + math.log(math.log(n)))) + 1 #Calculating the upper bound to iterate to reach the n'th prime
vis=[0]*(k+1) #Initializing the visited list with 0 values to be used in Seive algorithm
for i in range(2, k+1): #Iterating over till the calculated upper bound
if(n==0): #Since the bound is approximate, we continue till we get the n'th prime and stop afterwards
break; #Break the loop after getting the n'th prime
if vis[i]==1: #If the number is marked visited then continue
continue
elif vis[i]==0: #If the number is not visited, then it is a prime
primes.append(i) #so append it to primes
n=n-1; #and decrease n by 1 to count it as a prime
#Now since i is a prime, mark all it's multiples as visited, since they will no longer remain prime due to i as their factor
for j in range(i, k+1, i):
if vis[j]==1: #If the number is already marked visited by some earlier number, then continue
continue
elif vis[j]==0: #Mark the multiples visited
vis[j]=1
for x in primes: #After receiving the n primes, sum them over to get the sum of n primes
sum_of_primes+=x
return sum_of_primes #Returning the summed result
#Function main
def main():
while(True): #Looping over until user gives a valid input
n=(input("Enter n").rstrip().lstrip()) #Prompting user for input n and truncating leading or trailing white spaces
if validate_input(n)==False: #Continue further only if the validate_input condition holds
continue
else:
break
n=int(n) #Typecasting n to int
sum_of_primes=sum_of_n_primes(n) #Calculating the sum of n primes
print(sum_of_primes) #Printing the sum of n primes
#If the file containing the main and not the file in which this file is imported is running the program then call main
if __name__ == "__main__":
main()