At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
divisor = 3 primecounter = 1 nprime = 0 nthprime = int(raw_input('how many prime numbers shall we count? ')) while nprime < nthprime: ##still haven't reached our limit if primecounter%2 == 0: ## if it's even.. throw away primecounter += 2 else: while divisor < primecounter/2: ## no divisors past this if primecounter%divisor != 0: ## there is a remainder divisor += 2 ##try another divisor elif primecounter%divisor == 0: ## no remainder- not prime primecounter += 2 ## try again with a higher number divisor = 1 else: ## no divisors found. Number is prime nprime += 1 ##account for prime number primecounter += 2 ##repeat test for new number divisor = 1 ##reset divisor else: print 'the ',nthprime,'th prime number is:',primecounter
i feel like i'm close... but can't seem to find out where my problem is. Thanks in advance for any help
im taking a look at it now, ill go through my steps to debugging as i go. 1st: divided the program up by adding in print statements, narrow down the location of the problem.
i placed a print statement in each of the while statements, notice the program never gets to the second while statement, infinite loops occurs with the first one
this statement is never true while divisor < primecounter/2:
the first part of code that is redundant is the first if else statement, as long as you increment by +2 it will never be an even number
if you look at your if else statement, you can notice if the first statement is true then the second one is false and vice versa, ie it will never reach the final else statement.
good man. seems like i got a lot of work to. And yes the even number statement I should have realized. When you say the first statement is true then the second one is false I am not entirely sure where you are looking. Thanks a bunch for the help
if primecounter%divisor != 0: ## there is a remainder elif primecounter%divisor == 0: ## no remainder- not prime else: ## will never execute
You cannot know if a divisor was found until your innermost while loop ends. You need to end your inner while loop as soon as you find a divisor, then have another if to check to see if you found a divisor or not. If so, it's prime, if not, then move on.
you can accomplish what polpak is saying by using a flag. This is something that you can use to show that you have or haven't found something. i e found = true
Alternatively you could break out of the loop when you find a divisor, then check again outside the while loop if primecounter%divisor != 0 then you didn't break early and it's a prime.
ok this is great help. Seems that the way I roadmapped should be improved. And I need to create better parameters for my inner while loop to make sure it gets out of it. Let's see if I can't make this happen. Thanks again to both of yall. I'll post some more again when I either make it happen or my brain gets lost in its own infinite loop
polpak whats your position on using break tho i have always thought of it as bad programming pratice
Break is ok, though return is better. If I were doing this, I'd have a function to test for the 'primeness' of a number (returning true/false). That function would handle the inner while loop. Then to find the 1000th prime is just a matter of looping through all the odds, until 999 of them were prime. If I wanted to do it without a function, I'd make the modulo against divisor part of my inner while predicate. e.g. while (divisor < primecounter/2) and (divisor%primecounter != 0): divisor += 2
actually, in the case of having a function, that while loop would just be in the function, so I'd do that in both cases ;p
yea i think that is the best answer for this also. thanks
In general, I feel that break is ok. But I also feel that nested loops sould often be avoided, so that may be why I don't have much of a problem with break. The cases where break is often viewed as problematic are in situations where you have a couple of levels of nesting and it's not clear which ones you're breaking out of.