Assume the availability of a function is_prime . Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n . Associate this number with the variable k .
def main():
n=int(input('n: '))
k=0
i=2
sum=0
while sum<=n:
if is_prime(i):
sum+=i
i+=1
k+=1
print(k)
def is_prime(n):
for divisor in range(2,int(n**0.5)+1):
if n/divisor==int(n/divisor):
return False
return True
main()
would really appreciate some pointers.
I modified the code a little bit and it is working fine but the program that grades these codes says that I almost certainly should be using a + sign some where. I have no idea. Modified code is:
while sum<=n:
if is_prime(i):
sum+=i
k+=1
i+=1
print(k)
output:
n: 10
i: 2
2
i: 3
5
when it should actually go upto i=5 and total =10.