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
#5
Maths is definitely not my thing, but I tried like this:

Apart from 2, all prime numbers are odd.
Numbers ending in 0 or 5 are divisible by 5.
Numbers whose cross-sum is divisible by 3 are divisible by 3
11 is a strange number with weird properties, but that's another story!

A function to find prime numbers. I never played with prime numbers before, I hope this works!

def isPrime(anum):
    noP = {'0', '2', '4', '5', '6', '8'}
    P = {1, 2, 3, 5, 7}
    word = str(anum)
    if anum in P:
        #print(anum, 'is a prime number ... ')
        return True
    elif word[-1] in noP:
        #print(anum, 'is not a prime number.')
        return False
    sum_num = 0
    for w in word:
        sum_num = sum_num + int(w)
    if sum_num % 3 == 0:
        #print('The sum of', anum, 'is divisible by 3 so', anum, 'is not a prime number.')
        return False
    others = {7, 9}
    for n in others:
        if anum % n == 0:
            #print(anum, ' is divisible by', n, 'is not a prime number.')
            return False
    print('Can not find a factor for', anum, 'so it must be prime ... ')
    return True
You can get prime numbers using this:

for num in range(3, 2004, 2):
    if isPrime(num):
        print('*****************')
        print('The next prime number is', num)
        print('*****************')
For a given number, find the next prime number:

start = int(input('Enter a number ... '))
if not isPrime(start + 1):
    count = 1
    while not isPrime(start+ count):
        isPrime(start + count)
        count +=1
else:
    print('The next prime number is', start + count)
Check if a number is prime:

for n in range(1,2004):
    if 2003 % n == 0:
        print(n)
But I don't know if this uses too much time!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,493 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  calculate the 10001st prime number topologh 7 6,353 Oct-05-2020, 07:41 PM
Last Post: deanhystad
  Prime number code ryfoa6 3 2,944 Mar-21-2020, 12:23 AM
Last Post: ryfoa6
  Trouble interpreting prime number code ryfoa6 1 2,305 Mar-20-2020, 03:47 PM
Last Post: stullis
  Prime number Python homework mkrisz98 11 5,756 May-10-2019, 05:23 PM
Last Post: mkrisz98
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 3,224 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,119 Mar-26-2018, 04:44 AM
Last Post: PyMan
  how to find a next prime number? iamyourfather 2 6,574 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