A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
problem set 1: i thought i figured out a test to see if a number is prime but now i'm having problems with the script. If I enter a prime number, I get the correct answer (I included a print statement to indicate when a number is prime.) If I enter any other number, the script either runs indefinitely until I force it to quit or I get a false positive. Here's a link to my current script:
http://dpaste.com/524568/
I most recently ran the test with the number 8 and got the statement: "8 is a prime number." Does anyone see what I'm doing wrong here?
anonymous
 5 years ago
problem set 1: i thought i figured out a test to see if a number is prime but now i'm having problems with the script. If I enter a prime number, I get the correct answer (I included a print statement to indicate when a number is prime.) If I enter any other number, the script either runs indefinitely until I force it to quit or I get a false positive. Here's a link to my current script: http://dpaste.com/524568/ I most recently ran the test with the number 8 and got the statement: "8 is a prime number." Does anyone see what I'm doing wrong here?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, what happens when you've given it a number like 8? It will execute the first line, assigning divisor the value 3. Then it will ask "Is 3 < 8/2?", and since it is it will execute the block for the loop. The loop block first asks, "Is 8%3 != 0?". Since 8%3 is 2 and therefore not 0, it will execute assign divisor the value 3 + 1, or 4. Then it will ask "Is 4 < 8/2?". Since 4 is 8/2 this will fail and the loop will end. Then it will claim 8 is prime. I think it would be helpful and informative to insert some print statements to see the value of x and divisor at each step along the way.Something like: print 'In loop: x=', x, ', divisor=', divisor # between lines 3 & 4. and also print 'x%divisor was', x%divisor, ' so incrementing divisor' #between lines 4 & 5.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0polpak, i will try this technique. i keep thinking i need to create a print statement for when the isprime test fails and exits the loop. but, i don't know where to put it in the program. i'll give your suggestions a go and hope this will spark some insight.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Sure thing. Let me know if you get stuck.
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.