Python Forum
Trying to find the next prime number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to find the next prime number
#8
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:

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)
            break
I 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
Reply


Messages In This Thread
RE: Trying to find the next prime number - by Pedroski55 - Aug-14-2023, 11:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,525 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  calculate the 10001st prime number topologh 7 6,406 Oct-05-2020, 07:41 PM
Last Post: deanhystad
  Prime number code ryfoa6 3 2,975 Mar-21-2020, 12:23 AM
Last Post: ryfoa6
  Trouble interpreting prime number code ryfoa6 1 2,330 Mar-20-2020, 03:47 PM
Last Post: stullis
  Prime number Python homework mkrisz98 11 5,801 May-10-2019, 05:23 PM
Last Post: mkrisz98
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 3,259 Apr-10-2019, 03:46 AM
Last Post: scidam
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 10,182 Mar-26-2018, 04:44 AM
Last Post: PyMan
  how to find a next prime number? iamyourfather 2 6,603 Oct-01-2017, 04:21 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020