A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

been at problem set one for days now. i have a number of elements but cant get them to work together.

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    anyone wanna help?

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

    what have you got so far?

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    haha well now that i look i feel like i have a lot less than i thought i did. basically i have this: for n in range(1000): if n < 2: #number must be larger than two pass elif n%2==0: #number must not be even pass elif not n & 1: pass for x in range(3, int(n**0.5)+1, 2): if n % x == 0: pass but i feel like i am doing many many many things wrong

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    for instance, should i be using a for loop? and should i be using a range? i get that i should get the program to count prime numbers 1000 times (or 999 not including 2) but i have no idea how to make that happen

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    well, let's try to describe the problem. What is the program going to do?

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    count 1000 prime numbers

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok. So if we can find prime numbers, it's easy to count them, right? that's just an int variable that gets one added every time we find a prime.

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    okay...

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So we need to start at a number, check to see if it's prime, and If it is prime, add one to the counter otherwise move to the next number

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i see. so it will count to 1000 and show the prime number corresponding to the count?

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    right. the 1000th prime number

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    which is 7919, for when you need to check output

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So say we have a number. We know we can get a number, we know we can increment a counter if it turns out to be prime, but for now we've got this number. What process can we use to tell if it's prime?

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    first make sure it's positive, then an integer, then odd, and then indivisible by... 2, 3, or 5?

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    how do we increment a counter if it turns out to be prime?

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    first, we assign a counter variable, as in: ctr = 0 then, if the number is prime, we add to it: if numberisprime: ctr +=1 the += means 'equals itself plus,' so that's like saying ctr = ctr+1

  17. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    okay. so then are my criteria for prime correct?

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

    well, what if the number is divisible by 7? or 11? or a really high prime number that neither of us can think of? One way to be absolutely sure is to just check. Try dividing the number by every number less than it.

  19. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    We're using computers. We don't have to be clever, because we can do 2.9 million operations in a second.

  20. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so what function will divide by all integers below n?

  21. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    well, a for loop would work. have you used those?

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

    ive been playing around with them, went through this chapter last night - http://en.wikibooks.org/wiki/Python_Programming/Loops

  23. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So you know that with a for loop you can specify a range

  24. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    what if you used a for loop with the range set to the number you wanted to test? That way it would iterate over every number less than the number you wanted to test.

  25. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    but what number do i want to test? i could throw the 1000th prime in but wouldnt that defeat the purpose?

  26. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So if numberToTest = 7 and your range is for divisor in range (2,numberToTest) then the for loop will start with x = 2, then x=3...up to 6.

  27. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh, you want to start with a low number, and test every number going up until you collect 1000 primes

  28. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    okay so i would start with a loop that lists primes and tell it to stop after it has counted 1000?

  29. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    right: while we haven't found as many primes as we need: Check the next number if the next number is prime add one to the counter When we have enough primes Print the last prime we found.

  30. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    while we haven't found as many primes as we need: # So we need a counter variable Check the next number ##we need a number to test variable if the next number is prime add one to the counter When we have enough primes ##we also need a goal variable Print the last prime we found.

  31. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So we can start out saying NumToTest = The number we're going to test for primeness Divisor = The number we're going to divide NumToTest by to see if it's prime Goal = the number of primes we want to find ctr = the number of primes we've found

  32. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Now we can use those things as if they were actual numbers. So we can say while ctr < Goal: and that means "while we haven't found enough primes"

  33. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    while ctr = goal: ##"while we haven't found enough primes while Divisor < NumToTest ## while the number we're trying to divide by is smaller than the number we're testing

  34. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so ive got: NumToTest = 2 Divisor = Goal = 1000 ctr = 0 while ctr < Goal: but i do not know what to set divisor to

  35. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    or should i start at 3?

  36. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    while ctr = goal: ##"while we haven't found enough primes while Divisor < NumToTest <try to use an operation to see if the number is prime here> <if the operation proves the number isn't prime> <add something to the number to test to get the next one> <reset the divisor> <Otherwise> <add something to the divisor> <If we've made it this far, we've found a prime number> <add one to our counter> <reset the divisor> <add something to the number to test to get the next one> <if we've made it down here, we've found ENOUGH prime numbers> <print the current NumberToTest minus whatever we added two lines up to get the next number>

  37. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    We don't want to try to divide by 1--nothing would seem to be prime. We don't have to start at two if we only use odd numbers, and if we start out testing an odd number and always add two to it, we'll never have to test an even number. So the divisor should probably start at 3.

  38. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ahh thats nice.

  39. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm going to head home now. I'll be on later, but see what you can do to fill in that outline of a function. Make sure you start out with NumberToTest being odd, and make sure you increment things consistently--that is, whenever you reset the divisor, make sure you reset it to the same number, and whenever you add something to NumberToTest to get the next one, make sure you add the same number.

  40. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.