Could someone explain to me why this code will not print the 10th prime number? It says there is a syntax error on the fourth line (the x = range(3, math.sqrt(test_num)). It also says there is a syntax error on the sixth line (while prime_count < 10:).

import math
test_num = 3
prime_count = 1
x = range(3, math.sqrt(test_num))

while prime_count < 10:
    if test_num % x == 0:
        test_num = test_num + 2
    else:
        prime_count = prime_count + 1
        test_num = test_num + 2
print(test_num)
raw_input("Press<enter>")

Could someone explain to me why this code will not print the 10th prime number? It says there is a syntax error on the fourth line (the x = range(3, math.sqrt(test_num)). It also says there is a syntax error on the sixth line (while prime_count < 10:).

import math
test_num = 3
prime_count = 1
x = range(3, math.sqrt(test_num))

while prime_count < 10:
if test_num % x == 0:
test_num = test_num + 2
else:
prime_count = prime_count + 1
test_num = test_num + 2
print(test_num)
raw_input("Press<enter>")

x is range list, you can not divide number with list of numbers.

So, what should be my test to see if the candidate is prime?

prime_count = prime_count + 1

and same

prime_count += 1

I think you need to execute a loop from range(2,test_num/2), doing a modulus (%) operation with every number and the test_num value. Like this:

import math
test_num = 2
prime_count = 0

while prime_count < 10:
    x = 2
    while x <= ((test_num / 2)+ (test_num % 2)):
        if test_num % x == 0:   # if evenly divisble
            x = (test_num * 2)  # flag it as bad
            break               # stop iterating
        x += 1                  # else increment to next number to check
    if ( x != (test_num * 2)):  # if the bad flag is not there
        print(test_num)         # print it out
        prime_count += 1        # increment count
    test_num += 1               # get next number. If start odd, then can increment by 2
raw_input("Press<enter>")

Now that I've done your homework for you, you need to help out someone else today that needs help. Schoolwork, housework, whatever you deem appropriate. :-)

That definitely works, but let me see if I understand the process here. The initial variables are test_num = 2, prime_count = 0, and x = 2. Line 7 will initially be false (because 2 is not less than or equal to 1), so it skips to line 12. Since 2 is not equal to 4 (test_num * 2) it prints 2, adds one to the prime count, and one to the test_num. Now the variables are test_num = 3, prime_count = 1, and x = 2 or does x = 3 because of line 11? I don't believe x changes yet because it is indented. So x is still 2. Now, the program checks to see if 2 is less than or equal to 3/2 + the remainder of 3/2. Is 3/2 + the remainder of 3/2 6? Because 3/2 is 1 and the remainder of 3/2 is 5. I could keep going but I think I've asked enough questions for now.

Okay. Outer loop executes until 10 prime numbers are found. Inner loop runs once for every number we are testing. To test a number, you modulus by 2, 3, 4, 5, ... until you reach the number halfway to the target number (anything bigger than 1/2 of the target number cannot go evenly into the target number). If the modulus has no remainder, then it is a divisor of the target number, which is then not prime. Use X as a flag by setting to a value it could not have achieved through looping, and then break. If modulus was zero, then X was not a divisor so we keep incrementing X until the loop terminates or we find a divisor.

After the loop, we see if X is the flag value. If it is NOT, then the number was prime. Print it and increment count. Increment the next number we are checking. X is set to 2 before we enter the inner loop since that is where we start doing modulus operations. This will also serve to reject even numbers after a single test.

I hope this helps.

well explained buddy. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.