Hey guys, I am new here and have found many answers to my problems from this website so I figured it was time to join.
Here's the deal: I have just started trying to learn Python through MIT's OpenCourseWare. I am currently on PS1a (Problem set 1a) and am having trouble figuring this one out. My assignment is to test all odd integers from 2 to the 1000th prime (which happens to be 7919) for primality. Now I can do this (more or less) using Fermat's algorithm for primes, but it gives me quite a few pseudo primes and prints out the wrong number as the 1000th prime. So, I am revising my code to try and find a solution WITHOUT writing a function to determine primality. I want to do this test with a FOR loop. Any help will be appreciated.
2 things to note:
1. I am not a student looking for help on homework
2. I ran a search on the site and was unable to locate what I needed to solve this problem without using an IsPrime function.
fpn = 1000 # establishing my variable for i in range(1, int(fpn**.5)+1): # setting up loop to check only to the sqrt of max number if (i/2)*2 != i: # testing only odd numbers if fpn%i == 0: # This may be my problem. I am seeing what odd #'s return no remainder print i, 'is prime' else: print i, 'is not prime'
Edited 5 Years Ago by Dunkler: n/a