A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 6 years ago
need help with problem set 1, problems 1 and 2. no idea how to start...
anonymous
 6 years ago
need help with problem set 1, problems 1 and 2. no idea how to start...

This Question is Closed

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0hey what is the problem you're having? have you installed the python environment?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0I've installed python just not sure how to tackle this problem.. managed to do problem set 0 already

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0ok cool. the main thing is, what do all numbers besides 2 have in common?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0let me show you what I've had, commented, maybe that will help

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0then you can work through 1b

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0so 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("#>>"))

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0this explains what the program does, and the basic algorithm used. then you ask the user for which prime they want

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

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0I'd try to get it running on your system. And just with the commented out inefficient version

sandra
 6 years ago
Best ResponseYou've already chosen the best response.0here, 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

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

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0confused.com is written on my forehead right now

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0haha , it always starts that way. I would start by copying and pasting what sandra wrote into notepad or something

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0but 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

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0primerank is the number of primes you want to find before your program quits

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0sorry, maxprime is the number you want to find, primerank is the number your program as found during its run

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0so 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

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0so everything within that "while block" will be executed repeatedly until that condition is false

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0(which means you've found the prime number you want)

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0wow thats a lot to take in

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0now 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

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0and so we move on to finding the next, until we've found the one requested by the user (maxprime)

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0yes it's a lot to take in

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0but honestly , this is the hardest part about getting started programming

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0is there an example code i could look at?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0getting to know the actual control structures that allow you to code logic

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0control statements being things like while/for/if

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0the code that sandra wrote works for 1a

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0copy and paste it into your text editor, and then run the program it should work

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0how do you use that code?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0no matter what number i enter when i prompts me to, it comes out with 3

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0i 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'

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0well in that case, let's take 3

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0or rather, let's take 5

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0sorry, problem with that code is you need to cast the input to an int

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0when it comes from the command line, it comes in as a string

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0well what is the actual goal of 1a?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0so your first line should be: n = int(raw_input ('What number do you want to test '))

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0the actual goal of 1a is to find the nth prime, depending on what number the user enters

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0well 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'

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0is it not working for you?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0no it does work.. does is solve problem 1a?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0what you've done is written a program that takes in a number and determines whether or not it is prime

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0the 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)

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0if the user enters "10", your program should print the 10th smallest prime number (29)

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0ok one problem with your first program. what happens if you enter 21?

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0fixed problem in code, works just fine to my knowledge

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0johnny5, 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.

python
 6 years ago
Best ResponseYou've already chosen the best response.0k1e4v, johnny5 used the iteration counter to show the efficiency of the two different methods of printing prime numbers.

anonymous
 6 years ago
Best ResponseYou've already chosen the best response.0ah i see, so it tallies the number of divisors checked (which is more or less the marker of efficiency for this program)?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0username19'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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0SPOILER 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)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0If 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Can 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0oh btw that "if testNum%3 == 0:" half way down should be "elif" not "if"

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.0Inside 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).

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks for catching that shadowfiend. I know this is not the solution for prob set 1 though and have fixed it so it works. Thanks!

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.0Good to hear! Glad to help.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0do you guys read or watch other stuff about python because you're using some stuff I haven't seen in the lecture videos...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0has 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hi! What have you got so far?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0here 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0First, 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0There'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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Another way of asking the same question is: When you start testing a new testNumb, what number do you want the divisor to be?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yeah 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?

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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yeah 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yeah thanks now time to revise and make it better lol

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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hi  sorry about this reposting...for some reason everytime I login this posts...I am not sure why...

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ps. Not bashing captain 119 in any way...I am just a rookie and just thought there should be something simpler...

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.0Hm. We'll look at that. That's a problem on our end :)
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.