## dgo 3 years ago ps1: help please. I can't figure out the code for testing a number to see if it's prime. I want to write something that says divide test_number by all integers between 1 and test_number. this is my code so far: http://codepaste.net/w7jcru

1. rb01

this is my code for ps1a: prime = 0 number = 1 while prime < 1000: number = number + 1 # starting from 2 # testing if the number is prime or not divisor = 2 numberIsPrime = True while divisor*divisor <= number: if number % divisor == 0: numberIsPrime = False break divisor = divisor + 1 # found one? if numberIsPrime: prime = prime + 1 print number

2. maitre_kaio

First write some code that checks if a given integer is a prime. Look at the diagram I made (must be the 10th time I attach it, but hey that is really cool, no ?)

3. dgo

@maitre, I have the diagram already and I used it to write what I have so far. You say write some code that checks if a given integer is a prime, but thats the very thing I came on here asking for help about. I cant figure it out. Did you look at my code? Can you give me a hint as to how i fill in the "?" in my code?

4. dgo

@rb01, I didn't look at your answer because we're supposed to figure it out, not copy others work. but I appreciate you trying to help. Others might want the answers like that, but the best posters on here consistently avoid giving the answers away and rather encourage you to figure it out

5. maitre_kaio

In your code you are already trying to solve the whole problem: find the 1000th prime. Explain here, in english or pseudo-code, how you would test if a number is a prime.

6. dgo

I would try to divide it, using the modulus operator, by all integers from 2 to the square root of the number. but i dont know how to do the square root thing, so I would divide by all integers from 2 to the test_number minus 1 and if the result did not equal zero I would know its a prime number

7. maitre_kaio

ok, now rewrite the sentence 'I would divide by all integers from 2 to the test_number minus 1 and if the result did not equal zero I would know its a prime number', using the word 'for' and 'if'.

8. dgo

omg for is a type of loop, don't know how i missed that, gonna read up on it now in the wikibooks thing, never heard of it before. thank you!

9. dgo

10. dgo

if 17 % range(2,17-1) !=0 #number is prime unsupported operand type for %: "int" and "list" still reading...

11. dgo

http://codepaste.net/y4awhv not quite right but getting there

12. dgo

this "works" , not sure if i can continue loop with print statement all the way on the left but was the only way to get it to not print 15 17's tho http://codepaste.net/fnwftf how does that look?

13. maitre_kaio

test your code with 4. It says it's a prime. Where's the bug ?

14. dgo

The way I have it written if ANY test_divisor gives a result that != 0 it will say the test_number is prime. what I need to say is that if ALL test_divisors yield a result that != 0 only then is it prime, otherwise its composite.

15. dgo

but i cant figure out how

16. maitre_kaio

'if ALL test_divisors yield a result that != 0 only then is it prime, otherwise its composite' Another way to test the same thing, easier to code: 'if one test_divisor yield a result == 0 then it is not a prime, if I can test all the divisors without getting that result, then it is a divisor'

17. jimtuv

I found it easier to use a while loop then you can check for prime up to the square root of the number that reduces the number of test to a reasonable number. You don't have to check any division above the numbers square root. Also it is sometimes easier to assume something to be true and falsify rather then the other way around. By adding a break command to the while loop that breaks at a false you can shorten your search. the sudo code would go something like this: assume the number to be prime set prime to true while the number of iterations <= the square of the number to check if I divide and remainder is 0: number is not prime set prime to false break increment iteration return prime I hope this helps.

18. ZenoCitium

I also found a while loop to be well suited to this problem, but it can be done with a for loop as well of course (actually I turned a while loop into a do while loop). I used a list to store primes for checking (initialized only with 2, primeNumbers = [2]). You might want to consider what your code range(2,test_number) actually means and if this is really what you want. Have you made any progress on this problem in the last couple of days?

19. dgo

Thanks for your help guys, I'm still a little vague on many of the concepts/constructs. I feel like I'm writing an essay and I don't even know vocab, grammar, etc. I'm going to re-read some of the relevant texts assigned in the course as well as some others that I've come across on python.org/.../nonprogrammers. Will be returning to this thread asap. Thanks again.

20. dgo

ok I think this code works to see if a number is prime: test_number = 17 divisor = 2 while divisor < test_number: if test_number % divisor != 0: divisor = divisor + 1 print test_number @jimtuv, I am confused about setting things to trueor false, not sure why I'm confused by it but I am. maybe I haven't gotten there in the lectures/texts yet. also, that break command was unfamiliar to me, im sure i'll be using it soon, thx. @zeno, i reluctantly discarded the code: range(2,test_number), thx @all, what do i do now? lol. i'll continue working and try to post a complete answer to ps1a before you guys can respond. gl me!

21. dgo

regarding the code i just wrote...i think it says: "once test_number % divisor == 0, we will stop adding 1 to the divisor." but at that point the divisor is still < test_number (line 3) so why is it printing test_number? doesnt the line of code "print test_number" only get executed if divisor => test_number?

22. ZenoCitium

I see why you were using range now. There was nothing entirely wrong about it. We'll get to why I made comments about it later, but for now let's make some progress on this code. Can't tell where your print statement is indentend to, but I think its going to print test_number regardless of whether test_number is prime or not. In which case do you want it to print? Maybe both, maybe not? Consider that if test_number is 16, and 16 % 2 == 0, then what happens? You haven't accounted for negatively proving a number. Also consider that since 17 (your test_number) is prime: 17 % any_number != 0. Therefore maybe your code actually works depending on what your goals were at this stage in the development. I'll get a better comment out later today if I can.