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.
I am having the same problem, but I am not sure your code calculates the prime number correctly. Try putting in an odd number that is not prime, such as 27, for x and it will say that it is prime. I just don't know how to calculate if a number is prime.
if you wanna the nth number you should try from 1 to \[\infty\]
Your code is wrong. You are saying that a number is a prime if it is odd (x % 2 != 0) and a multiple of 1 (x % 1 == 0). All integers are multiples of 1. So your program will return true for all odd numbers. You should check the definition of a prime number http://en.wikipedia.org/wiki/Prime_number Make sure what a prime number is clear for you. After that, start coding. You will see that it's in fact easy to do. Once you have a code testing properly if a number is a prime, go to the next step and write a program which calculates the nth prime. To help you, I attached a little diagram.
Ok wi ll try it. and see what happens.
The way I (eventually) solved the issue of finding the 1000th prime is by creating a list of the known primes and having the program count them using the len() function. It's a concept that's not covered in the first two lectures really, but it's a neat trick to play with for a super n00b like me. I thought I'd at least share it since you may find it useful in the future. Every time the program successfully calculated a prime (if is_prime:), before going on to try the next number (num += 1) I would ask it to record the prime it just found and store it in a list using the append method. The code I wrote begins by asking whether or not I have reached the 1000th prime by counting the number of items in my primes list. As soon as it finds the 1000th, it prints it out. Here's what I'm talking about - hope it helps! For your reference: #Problem 1: Calculating the nth prime n = raw_input('Gimme a number. How many primes you want? ') #Enter 1000 to find the 1000th n = int(n) #Changing that input to an integer so I can use it for calculations number = 2 #No need to start at zero or it will tell me that 1 is a prime (it's not) primelist= #2 is the first prime number while len(primelist) <= n: test = 2 is_prime = True while (test*test) <= number: #Looking for squares here for the square root test of finding primes if number%test == 0: is_prime = False test += 1 # Test out the next number if is_prime: primelist.append(number) #Just keeping track of the primes so I can count to 1000 number += 1 #Let's try the next number down the line print primelist[-1], "is the ", n, "th prime number." print "done"
I have two things to say to improve this program: 1. when you have found a number is not a prime, why continue to test other divisors. You could end the inner looping and go to the next number. 2. Instead of keeping track of all the primes, why not count them (increase a counter each time you find one)? That way you will spare some memory, and you don't have to calculate the length of the list each time. I don't know how Python implements len(list), but just imagine that it has to walk through all the elements to count them....
This is how I did it. I used len() as well. First I set up my primes list so I had something to measure. Then I devised a test if the number was prime or not. If the number was prime it was added to the primes list which was measured and set for the loop. Because I wanted to get only up to the 1000th prime I made my loop stop there. primes=[2,3] def prime_test(int_to_test): A= (int(int_to_test**.5)+1)### you only need to test up to the ### square root of the number if int_to_test%2==0 or int_to_test%3==0: return False elif int_to_test%A ==0: return False elif int_to_test%(A)!=0: while A>2: A=A-1 if int_to_test%A==0: return False elif int_to_test%A!=0: True if True: print int_to_test primes.append(int_to_test) k=5 while len(primes)<1000: prime_test(k) print len(primes) k=k+2 if True: print int_to_test primes.append(int_to_test) k=5 while len(primes)<1000: prime_test(k) print len(primes) k=k+2