hey guys i know i'm gonna look very stupid but can anybody tell me what's wrong with this code: can = 5 loop = 1 c = 1 while loop <5: for m in range (2,can): if can % m == 0 : c = int (1) break c = str(1) if c == str(1) : print can can = can +2 loop = loop +1 else:print ' not candidate' , can can = can + 2 print 'the tenth prime is ' , can
Don't ever worry about looking stupid. We are all at different skill levels. As for your question I have no idea because I just started this class, so I am even stupider :)
you're right (: no one's stupid and i hope that i solve this
what are you trying to do? http://pastebin.com/UtQG1PHp using a code pasting site to post your code helps: pastebin.com, dpaste.com, codepad.org, ideone.com - i bet there are a zillion of them
this code is wrong but what i'm trying to do is to compute and print the 1000'th prime number
well lines 5- 8 are a good start, they will figure out if can is prime of not and you preserve that information with c so you can use it later. the problem must be later. an easier way to use c might be to give it a value of True or False - something like this http://pastebin.com/FPhfd4qb
ok i think i've got it but it keeps adding 2 on the last prime: can = 3 loop = 1 c = 1 while loop <10: for m in range (2,can): if can % m == 0 : c = int (1) break c = str(1) if c == str(1) : print can can = can +2 loop = loop +1 else: print 'NOT CANDIDATE' , can can = can + 2 print 'the tenth prime is ' , can
this is suppost to compute the 10"th prime
I wonder why it keeps adding two to the last prime? you should use a code pasting site to post your code http://pastebin.com/fVm5QXBq
thanks (:
Hi! It took quite long time, but... as I think I have figured out your problem: you missed '2' as prime, so you programm gives right answer for 10-th it is really 31(but if we exclude from sequence of prime numbers '2'). Desition of your problem: start loop from 2!
Also it is interesting to notice some fact, while cheking if number is prime: a=b*c+r a is not prime if it can be writen as b*c (both integer) and r-reminder is zero
so as we know b>2 and c>2 due to them both integer and we check only odd numbers for prime, as even (all except 2) were eliminated as they have 2 as divider. So form b>2 and c>2 we get that c=a/b where b>2, so instead checking range from 2 to prime-1 we can check range from 2 to prime/2. Result will be the same/ Think about it ))
Join our real-time social learning platform and learn together with your friends!