Here's the question you clicked on:
Cynical
I'm having difficulty writing a program that calculates the first 1000 primes. After brooding for a while this is what I came up with: a=0 while a<100: a=a+1 r=2*a+1 f=1 while f<r: f=f+1 if r%f==0: pass else: if int(f)>(1/2)*int(r): print r I would prefer it if you did not tell me the answer but only point out the part of the script that is wrong
First thing, use more descriptive variable names. Upon first glance, it's hard to tell what a, r, and f are all supposed to do. Secondly, can you describe in english what this block should do and why? if r%f==0: pass else: if int(f)>(1/2)*int(r): print r
if r%f==0: # to check if r divided by the number f gives remainder of 0 pass #If true then do nothing else: if int(f)>(1/2)*int(r): # if f exceeds half of the odd number r then it no longer needs to check print r
So r is the candidate you're checking to see if it's a prime number. Stepping through the first few lines of your program, let's see where issues arise. a=0 while a<100: a=a+1 r=2*a+1 So initially a is 0, then you add 1 to a, so it's 1. Then you set r to (2*a + 1) which is 3. Next is: f=1 while f<r: F is set to 1, which is less than r's value of 3, so we enter the loop. f=f+1 Increment f by one, so now it's 2. if r%f==0: pass else: if int(f)>(1/2)*int(r): print r 3 % 2 is 1, so we go to the else section. Now we're hitting a problem. This test may not be testing what you want. Open up a python window and type: >>> 1/2 Hit enter, what does it return?
I think we should describe the algorithm we gonna use before writing some code.
So I rewrote what I had. I think i made progress but I am unsure. To make it easier i am just testing the first 50 primes. Included are notes for which each line is intended to do. odd=0 #state variable while odd<50: #ends variable at 50th odd number odd=odd+1 #prevents infinite loop sequence=2*odd+1 #creates sequence of off numbers excluding one any_number=1 #divisor while any_number<sequence: #tells program to stop testing after divisor is equal to or greater than the sequence any_number=any_number+1 #increases divisor to test all numbers if sequence%any_number==0: #tests if sequence is perfectly divisible by number odd=odd+1 #if sequence is true, this progesses to test next odd number sequence=2*odd+1 #calculates the next odd number any_number=1 #resets the divisor if any_number >=(1.0/2)*sequence: #if there is no number that perfectly divides the sequence beyond half of the sequence then sequence is prime. print sequence #prints out that number
That's much easier to read now, but I think there's some other issues with your method of finding the prime numbers. Try to describe what you're doing in the while loop, and how that gets you the first prime, then the second and the third, and so on. What is the first prime your program finds? Can you add code to have it print something when it finds it?
I think iyasvami makes a good point. After two weeks of trial and error code writing, and some lecture re-watching, I started physically writing my algorithms down on paper. It helps tremendously. And not only from a code standpoint. There is a definite neurological bonus when writing the ideas down by hand, rather than typing them. It sounds so mundane and maybe irrelevant, but I promise you, it helps.
So I took your advice and wrote my algorithms down and its much better. though i still have an error. It's not spitting out the numbers I want it to. I don't see where exactly did I go wrong. Here is my Code with notes: #to find the nth number of primes the user asks. nth_prime=raw_input("nth prime you are looking for?")#defines the counter number=0#sets variable for numbers to try divisor=2#tells what to divide by starting with 2 list_of_primes=()#tuple used to collect all primes found while nth_prime>0:#initiates loop if number%divisor==0:#first test number +=1#if evenly divisible, its not prime skip to next number else: divisor+=1# if not even with fist divisor try next divisor if divisor >= number:#tells the divisor to stop if it reaches this point. list_of_primes= list_of_primes +(number, )#adds number to list because it is prime nth_prime= int(nth_prime)-1#tells program to find primes until counter reaches 0 else: pass#to guard against possiblities i didnt think of print list_of_primes#prints out my lst of primes
You are stuck in your second IF statement - IF divisor >= number:. Well as you have it, your divisor will always be greater than your numerator, so will always return 1, because: if 0%2 == 0 0 = 0 + 1 (1/2) now - 1/2 has remainder - else: divisor += 1 - so - 1/3. if 3 greater than 1, print 1 in tuple. Restart loop... number = 1. so if 1/3 no remainder...and 3 greater than 1, add 1 to tuple...etc. Until counter goes to zero, where you pass, and print list of all ones.
An if statement doesn't need an else clause. Just skip it if you don't need it.
sorry should be: Restart loop... number = 1. so if 1/3 HAS remainder...and 3 greater than 1, add 1 to tuple...etc. Until counter goes to zero, where you pass, and print list of all ones.
You should be testing out any modifications you make with the same methodology Aslander just used. Run through step by step and jot down what the values of each variable are, and where the program is going as a result. But I think the issue is that you're having trouble properly coding how to find a prime. Write down the properties of prime numbers. Sketch out ways you could test to see if a number was prime or not. Then test your method by feeding it prime and non-prime numbers. You need to nail down your algorithm.
Okay. I'll do it on paper now. I've been following them in my head and i didnt see the error. Thank you for the help.
I'm currently online, if you hop in chat, we can talk faster. :)
Here's my version of solution for Problem1 from ps1: cand = 11 prime_count = 5 div = 3 isFound = True while prime_count < 1000: cand = cand + 2 div = 3 isFound = True while div*div<=cand and isFound == True: if(cand%div==0): isFound = False else: div = div + 2 if isFound == True: prime_count = prime_count + 1 if prime_count == 1000: print 'The 1000th prime number is', cand