A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

need help with problem set 1, problems 1 and 2. no idea how to start...

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

    hey what is the problem you're having? have you installed the python environment?

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

    I've installed python just not sure how to tackle this problem.. managed to do problem set 0 already

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

    ok cool. the main thing is, what do all numbers besides 2 have in common?

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

    let me show you what I've had, commented, maybe that will help

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

    then you can work through 1b

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

    ok sounds good

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

    so here's the setup, at the top of your python file ## This program asks the user how many prime numbers to display. Two methods are available to ## calculate the primes. The inefficient version checks the divisibility of every number < MaxPrime ## (user input) against MaxPrime. The efficient version stops checking if any number < MaxPrime is ## divisible. I built in a variable to count the number of iterations each method must go through in ## order to check the efficiency. I used Python 2.4, the one used in this course. PrimeCounter = 1 MaxPrime = 1 OddNumber = 1 IsPrime = 1 Primes = [2] Counter = 2 IterCount = 0 ## Get user input and initialize counter: print "How many primes do you want to see?" MaxPrime = int(raw_input("#>>"))

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

    this explains what the program does, and the basic algorithm used. then you ask the user for which prime they want

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

    ## Problem Set 1a tan x ## This program asks the user how many prime numbers to display. Two methods are available to ## calculate the primes. The inefficient version checks the divisibility of every number < MaxPrime ## (user input) against MaxPrime. The efficient version stops checking if any number < MaxPrime is ## divisible. I built in a variable to count the number of iterations each method must go through in ## order to check the efficiency. I used Python 2.4, the one used in this course. PrimeCounter = 1 MaxPrime = 1 OddNumber = 1 IsPrime = 1 Primes = [2] Counter = 2 IterCount = 0 ## Get user input and initialize counter: print "How many primes do you want to see?" MaxPrime = int(raw_input("#>>")) while PrimeCounter < MaxPrime: OddNumber += 2 IsPrime = 1 Counter = 2 ## This the efficient version of the algorithm: while IsPrime == 1 and Counter < OddNumber: IterCount += 1 if OddNumber % Counter == 0: IsPrime = 0 Counter += 1 ## This is the inefficient version: ## for i in range(2, OddNumber): ## if OddNumber % i == 0: ## IsPrime = 0 ## IterCount += 1 ## I used a list instead of a tuple, because I can't figure out tuples. if IsPrime == 1: Primes += [OddNumber] PrimeCounter += 1 ## Display output. This step is not strictly necessary, but I used it to check the algorithm's accuracy. print Primes print "Iterations: ", IterCount

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

    ...........?

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

    I'd try to get it running on your system. And just with the commented out inefficient version

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

    ok then

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

    here, this version may be easier to understand than that (warning compact!) import math ##Import square root function testnumber = 3 ##testnumber is the number to check for primeness; 3 is the first prime after 2, so we initialize it to 3 primerank = 2 ##We want to get this to 1000 maxprime = input('What prime are you looking for? ') while primerank <= maxprime: divisor = 2 ##We devide by this number to check for primeness while divisor <= math.sqrt(testnumber): ##If the testnumber is not divisible by any integer less than or equal to its square root, it is prime. if testnumber%divisor == 0: ##This checks divisibility. If testnumber is divisible by the divisor, it cannot be prime. testnumber = testnumber+2 ##moves on to next odd number (Even numbers can't be prime.) divisor = 2 ##Resets divisor else: ##If testnumber was not divisible... divisor = divisor+1 ##...we move on to the next divisor. ##The while loop ends, meaning testnumber is the next prime. if primerank == maxprime: ##If testnumber is the 1000th prime... print testnumber ##...let's see it! primerank = primerank+1 else: ##Otherwise, lets check the next odd number. primerank = primerank+1

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

    ^^ I highly suggest copying/pasting this into your code editor/text editor. The indentation and lack of space hamper readability of python a bit.

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

    confused.com is written on my forehead right now

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

    haha , it always starts that way. I would start by copying and pasting what sandra wrote into notepad or something

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

    but as far as the logic goes. in Sandra's version "testNumber" is a variable that keeps track of which numbers you've checked to be prime

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

    primerank is the number of primes you want to find before your program quits

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

    sorry, maxprime is the number you want to find, primerank is the number your program as found during its run

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

    so the first "while" loop is the main control statement. basically it says "don't quit trying until we've found the requested prime number". i.e., when primerank is equal to maxprime

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

    so everything within that "while block" will be executed repeatedly until that condition is false

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

    (which means you've found the prime number you want)

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

    wow thats a lot to take in

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

    now within that while loop, is another loop - the inner while loop basically checks the next odd number that hasn't been checked (since even numbers can't be prime). it does this by attempting to divide that number by every number between 2 and its square root (weird property of prime numbers). if that division has no remainder for any of those numbers (hence the inner while loop), then we know we've found the next prime

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

    and so we move on to finding the next, until we've found the one requested by the user (maxprime)

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

    yes it's a lot to take in

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

    but honestly , this is the hardest part about getting started programming

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

    is there an example code i could look at?

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

    getting to know the actual control structures that allow you to code logic

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

    control statements being things like while/for/if

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

    the code that sandra wrote works for 1a

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

    copy and paste it into your text editor, and then run the program it should work

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

    how do you use that code?

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

    no matter what number i enter when i prompts me to, it comes out with 3

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

    i tried to do this... can you point out whats wrong with it... n = raw_input ('What number do you want to test ') print n if 'n'/2.0 == n/2 and n/n == 1: print 'test number is not prime' else: print 'test number is prime'

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

    well in that case, let's take 3

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

    or rather, let's take 5

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

    sorry, problem with that code is you need to cast the input to an int

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

    when it comes from the command line, it comes in as a string

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

    well what is the actual goal of 1a?

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

    so your first line should be: n = int(raw_input ('What number do you want to test '))

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

    and then it works

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

    the actual goal of 1a is to find the nth prime, depending on what number the user enters

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

    well then this program should work: n = int(raw_input ('What number do you want to test ')) print n if (n/2.0) == (n/2) and (n/n) == 1 and n !=2: print 'test number is not prime' else: print 'test number is prime'

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

    is it not working for you?

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

    no it does work.. does is solve problem 1a?

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

    no ok I get it

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

    what you've done is written a program that takes in a number and determines whether or not it is prime

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

    okay

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

    the goal is to take in a number and find *that* prime in the sequence of prime. so if you user enters "3", your program should print the third prime (5)

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

    if the user enters "10", your program should print the 10th smallest prime number (29)

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

    ok i understand

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

    ok one problem with your first program. what happens if you enter 21?

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

    well thats a problem

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

    fixed problem in code, works just fine to my knowledge

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

    johnny5, could you explain why you have an iteration counter? i came up with something that is more or less the same code (yours is simpler but mine works!) and i was looking at your code to see how to simplify mine (which would be easy now that i understand it), but i don't understand why or what your iteration counter was doing.

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

    k1e4v, johnny5 used the iteration counter to show the efficiency of the two different methods of printing prime numbers.

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

    ah i see, so it tallies the number of divisors checked (which is more or less the marker of efficiency for this program)?

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

    yes

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

    username19's function is flawed. It only tests to see if the number is divisible by 2 only. Also when is n/n != 1? Below is a function I wrote to return True if n is a prime and False if it is not. def PrimeTest(n): if n < 2: return False for i in range(2, (n/2+1)): if n % i == 0: return False return True

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

    SPOILER ALERT - answer below: So I just wrote a second function that uses PrimeTest (in my last post) to finish the exercise: def PrimeCount(n): primesFound = 0 lastPrime = 0 testNum = 0 while primesFound < n: if PrimeTest(testNum): #below is executed if testNum is a true Prime number. lastPrime = testNum primesFound = primesFound + 1 testNum = testNum + 1 else: testNum = testNum + 1 print "primesFound: " + str(primesFound) print "lastPrime: " + str(lastPrime)

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

    If you want to play around with these functions, copy them into a file called probset1.py and save it into your python path (where you can import py files from). Then fire up your interpreter and run: import probset1 probset1.PrimeCount(1000) if you want to play around with the functions (I suggest adding some print statements for debug so you can see it work) make sure to restart the interpreter after you save the probset.py file.

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

    Can someone clue me into why this program isn't working? I'm trying to print numbers not divisible by 2, 3, 5, 7 and 11 (assuming they will be prime) up to the first 1000 primes. #This is a program to print the first 1000 prime numbers. iteration = 1 testNum = 3 while (iteration<1001): #continue to iterate until you have reached the first 1000 primes. if testNum == 3: #exception for 3 since 3%3=0 testNum == testNum+2 iteration = iteration+1 print testNum elif testNum == 5: #exception for 5 iteration = iteration+1 testNum == testNum+2 print testNum elif testNum == 7: #exception for 7 iteration = iteration+1 testNum == testNum+2 print testNum elif testNum == 11: #exception for 11 iteration = iteration+1 testNum == testNum+2 print testNum if testNum%3 == 0: iteration = iteration+1 testNum == testNum+2 elif testNum%5 == 0: iteration = iteration+1 testNum == testNum+2 elif testNum%7 == 0: iteration = iteration+1 testNum == testNum+2 elif testNum%11 == 0: iteration = iteration+1 testNum == testNum+2 else: print testNum testNum == testNum+2 iteration = iteration+1

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

    oh btw that "if testNum%3 == 0:" half way down should be "elif" not "if"

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

    Inside the if statements, you're saying testNum == testNum+2, which is a test, not an assignment. testNum == testNum+2 is a check to see if they are equal, and evaluates to False. You want testNum = testNum+2 (note the single equals sign).

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

    Thanks for catching that shadowfiend. I know this is not the solution for prob set 1 though and have fixed it so it works. Thanks!

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

    Good to hear! Glad to help.

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

    do you guys read or watch other stuff about python because you're using some stuff I haven't seen in the lecture videos...

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

    has anyone solved this problem? i have a working version of it that i was able to put together using function but i'm not able to do it just using the while and if statements

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

    Hi! What have you got so far?

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

    here is what i got so far i think i got it done to calculate prime numbers only but i think what i'm doing wrong is not putting the addition in the right price to get the counter up or something from math import sqrt testNumb = 1 counter = 2 divisor = 1 maxPrime = int(raw_input ('Enter a number for to get that prime ')) if maxPrime == 1: print 2 elif maxPrime==2: print 3 else: while (counter < maxPrime): while(divisor < int(sqrt(testNumb)+1)): if (testNumb % divisor ==0): testNumb += 2 else: divisor +=1 counter +=1 divisor += 2 testNumb += 2 print testNumb

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

    right place** lol

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

    ok so i found out whats wrong it is the counter ever time it goes thru the loop it raises what is added to the counter so it goes counter + 1 then counter + 2 then counter +3 i dont know why its doing that

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

    First, it's good that you've accounted for the first and 2nd prines, but because you've done that, where should you start checking for new primes? In the current program, you're starting at testNumb = 1

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

    i was playing around with that just because of the results so when i changed testNumb = 3 if i test for 3rd prime i will get 13

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

    ok i think i fixed the counter problem with this but now i realized sometimes it is allowing multiples of 5 to be counted from math import sqrt testNumb = 3 counter = 2 divisor = 3 maxPrime = int(raw_input ('Enter a number for to get that prime ')) if maxPrime == 1: print 2 elif maxPrime==2: print 3 else: while (counter < maxPrime): testNumb += 2 while(divisor < int(sqrt(testNumb)+1)): if (testNumb % divisor ==0): testNumb += 2 else: divisor +=1 divisor += 2 counter +=1 print testNumb

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

    There's an issue in the initial values, but what they should be depends on how the rest of the program is structured. First, we've got this: else: while (counter < maxPrime): while(divisor < int(sqrt(testNumb)+1)): if (testNumb % divisor ==0): testNumb += 2 else: divisor +=1 counter +=1 divisor += 2 testNumb += 2 print testNumb Try going through this program line by line. What happens to the flow of control after the statements if (testNumb % divisor ==0): testNumb += 2 What is the value of the divisor and what happens to it?

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

    Another way of asking the same question is: When you start testing a new testNumb, what number do you want the divisor to be?

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

    yeah i get what you are saying i should have a place where if the number is prime to set the divisor back to the initial value right?

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

    I would think that every time the testNumb changed, you'd want to reset the divisor. For instance, if one number is divisible by 7 (not prime) the next testNumb will be (oldtestNumb +2). If you only reset the divisor when a number is prime, you're going to start trying to divide the new testNumb by 9.

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

    yeah i totally didnt check for that or even thought about the divisor to set it back thanks a lot Somna :) its working great now from math import sqrt testNumb = 3 counter = 2 divisor = 3 maxPrime = int(raw_input ('Enter a number for to get that prime ')) if maxPrime == 1: print 2 elif maxPrime==2: print 3 else: while (counter < maxPrime): testNumb += 2 divisor = 3 while(divisor < int(sqrt(testNumb)+1)): if (testNumb % divisor ==0): testNumb += 2 divisor = 3 else: divisor +=1 divisor = 3 counter +=1 print testNumb

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

    nice!

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

    yeah thanks now time to revise and make it better lol

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

    I think what is important is that you read on how to find prime numbers without mathematics first. This will point anyone in the correct direction. ex. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. From there you can easily create a loop, or for sequence to calculate any primes up to any number. Then if you create a list of them, without using captain 119's code, you can just list the index for any value - 1. ex. For prime # 777 in a list of 1000 prime numbers, just ask to show index for 776...my guess is that it should work without complicated code.

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

    ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

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

    ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

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

    ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

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

    Hi - sorry about this reposting...for some reason everytime I login this posts...I am not sure why...

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

    ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

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

    Hm. We'll look at that. That's a problem on our end :)

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