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

9 years agohey what is the problem you're having? have you installed the python environment?

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

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

9 years agolet me show you what I've had, commented, maybe that will help

9 years agothen you can work through 1b

9 years agook sounds good

9 years agoso 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("#>>"))

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

9 years ago## 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

9 years ago...........?

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

9 years agook then

9 years agohere, 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

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

9 years agoconfused.com is written on my forehead right now

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

9 years agobut 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

9 years agoprimerank is the number of primes you want to find before your program quits

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

9 years agoso 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

9 years agoso everything within that "while block" will be executed repeatedly until that condition is false

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

9 years agowow thats a lot to take in

9 years agonow 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

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

9 years agoyes it's a lot to take in

9 years agobut honestly , this is the hardest part about getting started programming

9 years agois there an example code i could look at?

9 years agogetting to know the actual control structures that allow you to code logic

9 years agocontrol statements being things like while/for/if

9 years agothe code that sandra wrote works for 1a

9 years agocopy and paste it into your text editor, and then run the program it should work

9 years agohow do you use that code?

9 years agono matter what number i enter when i prompts me to, it comes out with 3

9 years agoi 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'

9 years agowell in that case, let's take 3

9 years agoor rather, let's take 5

9 years agosorry, problem with that code is you need to cast the input to an int

9 years agowhen it comes from the command line, it comes in as a string

9 years agowell what is the actual goal of 1a?

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

9 years agoand then it works

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

9 years agowell 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'

9 years agois it not working for you?

9 years agono it does work.. does is solve problem 1a?

9 years agono ok I get it

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

9 years agookay

9 years agothe 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)

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

9 years agook i understand

9 years agook one problem with your first program. what happens if you enter 21?

9 years agowell thats a problem

9 years agofixed problem in code, works just fine to my knowledge

9 years agojohnny5, 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.

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

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

9 years agoyes

9 years agousername19'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

9 years agoSPOILER 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)

9 years agoIf 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.

9 years agoCan 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

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

9 years agoInside 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).

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

9 years agoGood to hear! Glad to help.

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

9 years agohas 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

9 years agoHi! What have you got so far?

9 years agohere 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

9 years agoright place** lol

9 years agook 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

9 years agoFirst, 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

9 years agoi 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

9 years agook 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

9 years agoThere'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?

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

9 years agoyeah 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?

9 years agoI 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.

9 years agoyeah 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

9 years agonice!

9 years agoyeah thanks now time to revise and make it better lol

9 years agoI 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.

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

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

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

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

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

9 years ago