Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

dgo

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

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. rb01
    Best Response
    You've already chosen the best response.
    Medals 0

    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 years ago
  2. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 1

    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 ?)

    • 2 years ago
    1 Attachment
  3. dgo
    Best Response
    You've already chosen the best response.
    Medals 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?

    • 2 years ago
  4. dgo
    Best Response
    You've already chosen the best response.
    Medals 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

    • 2 years ago
  5. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  6. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • 2 years ago
  7. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 1

    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'.

    • 2 years ago
  8. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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!

    • 2 years ago
  9. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    test_divisor = range(2,test_number-1) still reading....

    • 2 years ago
  10. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • 2 years ago
  11. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • 2 years ago
  12. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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?

    • 2 years ago
  13. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • 2 years ago
  14. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  15. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    but i cant figure out how

    • 2 years ago
  16. maitre_kaio
    Best Response
    You've already chosen the best response.
    Medals 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'

    • 2 years ago
  17. jimtuv
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  18. ZenoCitium
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • 2 years ago
  19. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  20. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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!

    • 2 years ago
  21. dgo
    Best Response
    You've already chosen the best response.
    Medals 1

    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?

    • 2 years ago
  22. ZenoCitium
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • 2 years ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.