Variations on a prime-number-finding program

Incomplete primes finder

Only works for numbers smaller than the square of the largest listed prime.

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 15 11:02:42 2021

@author: rmontant
"""
primes_list = [2, 3, 5, 7, 11, 13, 17, 19]

candidate = int( input('Possible prime? '))

primeflag = True
for p in primes_list:
    remainder = candidate % p
    if remainder == 0:
        primeflag = False
        print(candidate, 'is divisible by', p)
        break

if primeflag == True:
    print(candidate, 'is prime')

Extend the list of primes first...

The list can start with only the first prime, it'll be extended to as many more as needed.

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 15 11:02:42 2021

@author: rmontant
"""
primes_list = [2, 3, 5, 7, 11, 13, 17, 19]

candidate = int( input('Possible prime? '))

for n in range(primes_list[-1], candidate + 1):
    primeflag = True
    for p in primes_list:
        remainder = n % p
        if remainder == 0:
            primeflag = False
            break
    if primeflag == True:
        primes_list.append(n)

print(primes_list)

primeflag = True
for p in primes_list:
    remainder = candidate % p
    if remainder == 0:
        primeflag = False
        print(candidate, 'is divisible by', p)
        break

if primeflag == True:
    print(candidate, 'is prime')

Primes finder, showing all prime divisors

We only need to test the candidate once. And if we are testing it, display any divisors. (Otherwise we can stop testing a number as soon as a divisor is found.)

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 15 11:02:42 2021

@author: rmontant
"""
primes_list = [2]

candidate = int( input('Possible prime? '))

for n in range(2, candidate + 1):
    primeflag = True
    for p in primes_list:
        remainder = n % p
        if remainder == 0:
            primeflag = False
            if n == candidate:
                print(n, 'is divisible by', p)
            #break
    if primeflag:
        primes_list.append(n)

print(len(primes_list), \
      'primes less than or equal to', \
          candidate)

#primeflag = True
#for p in primes_list:
#    remainder = candidate % p
#    if remainder == 0:
#        primeflag = False
#        print(candidate, 'is divisible by', p)
#        break
#
#if primeflag == True:
if candidate in primes_list:
    print(candidate, 'is prime')
else:
    print(candidate, 'is not prime')