I need to find the total of prime numbers less than or equal to a given natural number. Ex, with natural number (n) = 4, than the total = 2 +3 = 5.
I wrote the below codes to solve the problem and it seems to be correct until n = 4, however, when n > 4, it starts appearing the repetitive result of eligible f. Eg. when n = 5, f = 3,5,5,5 => total of prime = 20.
Could you explain what is wrong with my codes and how to fix it?
n = eval(input("Input n (natural number): "))
e = 0
if n == 1:
e = 0
else:
f = 3
e = 2
while f <= n:
for r in range(2,f):
if f %r == 0:
break
else:
e += f
f +=1
print("Total of prime = ",e )