Skip to content

Instantly share code, notes, and snippets.

@telliott99
Last active February 10, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telliott99/3043a0d9ddc44f8503c83c848b2f8382 to your computer and use it in GitHub Desktop.
Save telliott99/3043a0d9ddc44f8503c83c848b2f8382 to your computer and use it in GitHub Desktop.
import sys
def first_prime_factor(n,pL):
for p in pL:
if n % p == 0:
return p, n/p
return 1,n
def get_primes(N):
pL = [2,3]
for n in range(4, N+1):
if first_prime_factor(n,pL)[0] == 1:
pL.append(n)
return pL
def fmt(L):
rL = list()
while L:
sL = L[:8]
L = L[8:]
sL = [str(e).rjust(5) for e in sL]
rL.append(''.join(sL))
return '\n'.join(rL)
def check_if_prime(m,do_print=False):
N = int(m**0.5)
pL = get_primes(N)
if do_print:
print(fmt(pL))
t = first_prime_factor(m,pL)
print(t)
if __name__ == "__main__":
m = int(sys.argv[1])
check_if_prime(m)
'''
> python is_prime.py 13717421
(3607, 3803)
>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment