I am trying to run a simple conformance test in Python.
Alignment with Wikipedia, the criterion of primitiveness is as follows:
Given the input number n, check to see if any integer m divides from 2 to n - 1. If n is divisible by any m, then n is compound, otherwise it is simple.
I started by striking out even numbers - with the exception of 2 - as candidates for simple
def prime_candidates(x): odd = range(1, x, 2) odd.insert(0, 2) odd.remove(1) return odd
Then we write a function for checking primes in accordance with the above rules.
def isprime(x): for i in range(2, x-1): if x % i == 0: return False else: return True
And this is the main function that iterates through a list of 8000 main candidates and checks their primacy
def main(): end = 8000 candidates = prime_candidates(end) for i in candidates: if isprime(i) and i < end: print 'prime found ' + str(i)
The problem is that the isprime function returns True for numbers that are not prime.
source share