Aug-14-2023, 11:04 AM
Apart from 2 and 5, all prime numbers end in 1, 3, 7, or 9
So all you need to look at are those numbers.
Then, looking for factors of a number, the smallest possible factor is really 3, and the largest factor will be 1/3 of the number.
So if you start dividing by 3 and work up by odd numbers to 1/3 * number, if you don't find any factors, the number must be prime, I think.
Something like this below: nextPrime(num) calls checkPrime(anum) until checkPrime(anum) returns True:
So all you need to look at are those numbers.
Then, looking for factors of a number, the smallest possible factor is really 3, and the largest factor will be 1/3 of the number.
So if you start dividing by 3 and work up by odd numbers to 1/3 * number, if you don't find any factors, the number must be prime, I think.
Something like this below: nextPrime(num) calls checkPrime(anum) until checkPrime(anum) returns True:
def checkPrime(anum): word = str(anum) # numbers ending in 0 or 5 can't be prime numbers no matter how big if word[-1] in {'0', '5'}: return False # speed things up at the beginning P = [2, 3, 5, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39, 41, 43, 47, 49, 51, 53, 57, 59, 61, 63, 67, 69, 71, 73, 77, 79, 81, 83, 87, 89, 91, 93, 97, 99, 103, 107, 109, 111, 113, 117, 119, 121, 123, 127, 129, 131, 133, 137, 139, 141, 143, 147, 149, 151, 153, 157, 159, 161, 163, 167, 169, 171, 173, 177, 179, 181, 183, 187, 189, 191, 193, 197, 199] if anum in P: #print(anum, 'is a prime number ... ') return True print('Looking for a prime number. Number is', anum) test = int((1/3) * anum) print('test is', test) # even numbers can't be prime so only check odd numbers if test % 2 == 0: test = test + 1 print('test is', test) for n in range(3, test, 2): # look for a factor, if found return False if anum % n == 0: print(anum, 'divided by', n, ' = ', anum/n) print('Found a factor for', anum, 'which is', n, 'so', anum, 'is not a prime number') return False else: return True def nextPrime(start): print('Check for the next prime number following', start) evens = {'0', '2', '4', '6', '8'} odds = {'1', '3', '5', '7', '9'} word = str(start) original = start # make start an odd number if it is even # could be the next number is a prime number if word[-1] in evens: start = start + 1 count = 0 # if start is odd, the next prime number must be at least start + 2 if word[-1] in odds: count = 2 # a prime number must be an odd number so increase by 2 each time looking for prime numbers while not checkPrime(start + count): count += 2 print('After', original, 'the next prime number is', start + count)To confirm that a result from checkPrime(anum) is actually a prime number, you can do this:
def isPrime(num): for i in range(1, num + 1): if num % i == 0: print('Found a factor for', num, 'which is', i) breakI get this kind of output:
Output:nextPrime(1100)
Check for the next prime number following 1100
Looking for a prime number. Number is 1101
test is 367
1101 divided by 3 = 367.0
Found a factor for 1101 which is 3 so 1101 is not a prime number
Looking for a prime number. Number is 1103
test is 367
After 1100 the next prime number is 1103