A community for students.
Here's the question you clicked on:
 0 viewing
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
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

This Question is Closed

rb01
 3 years ago
Best ResponseYou've already chosen the best response.0this 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

maitre_kaio
 3 years ago
Best ResponseYou've already chosen the best response.1First 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 ?)

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1@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?

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1@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

maitre_kaio
 3 years ago
Best ResponseYou've already chosen the best response.1In your code you are already trying to solve the whole problem: find the 1000th prime. Explain here, in english or pseudocode, how you would test if a number is a prime.

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1I 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

maitre_kaio
 3 years ago
Best ResponseYou've already chosen the best response.1ok, 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'.

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1omg 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!

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1test_divisor = range(2,test_number1) still reading....

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1if 17 % range(2,171) !=0 #number is prime unsupported operand type for %: "int" and "list" still reading...

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1http://codepaste.net/y4awhv not quite right but getting there

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1this "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?

maitre_kaio
 3 years ago
Best ResponseYou've already chosen the best response.1test your code with 4. It says it's a prime. Where's the bug ?

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1The 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.

maitre_kaio
 3 years ago
Best ResponseYou've already chosen the best response.1'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'

jimtuv
 3 years ago
Best ResponseYou've already chosen the best response.1I 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.

ZenoCitium
 3 years ago
Best ResponseYou've already chosen the best response.0I 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?

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1Thanks 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 reread 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.

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1ok 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!

dgo
 3 years ago
Best ResponseYou've already chosen the best response.1regarding 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?

ZenoCitium
 3 years ago
Best ResponseYou've already chosen the best response.0I 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.