Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (sudhansharma):

where the code for 1000th prime no. goes for infinite loop counter=3 n=6 while counter!=1000: n+=1 for c in range(2,n-1): if(n%c==0): counter+=1 print('1000th prime number is '+n)

OpenStudy (p0sitr0n):

set n=1 and counter=1

OpenStudy (sudhansharma):

it's still coming out to be an infinite loop.

OpenStudy (kmd284):

thats because your counter variable passes 1000 when inside the for loop which means it will never equal 1000 when the while loop tests the condition.

OpenStudy (kmd284):

you need to ensure that it doesn't increment count till the very last iteration and only if nothing before it divided evenly into n

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!