From d403da53db27a824fc44a328d602c52b37f0d125 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Tue, 1 Jul 2025 18:04:00 +1000 Subject: [PATCH] Add basic tests of semi-primality and k-almost primality --- imp/math/numbers/functions.py | 5 +++++ imp/math/numbers/kinds.py | 1 + imp/math/primes.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 imp/math/numbers/functions.py create mode 100644 imp/math/numbers/kinds.py diff --git a/imp/math/numbers/functions.py b/imp/math/numbers/functions.py new file mode 100644 index 0000000..6cafa7f --- /dev/null +++ b/imp/math/numbers/functions.py @@ -0,0 +1,5 @@ +def factorial(n: int) -> int: + if n == 0: return 1 + return n * factorial(n-1) + +def diff --git a/imp/math/numbers/kinds.py b/imp/math/numbers/kinds.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/imp/math/numbers/kinds.py @@ -0,0 +1 @@ + diff --git a/imp/math/primes.py b/imp/math/primes.py index 513f61e..eaeebcb 100644 --- a/imp/math/primes.py +++ b/imp/math/primes.py @@ -1,5 +1,23 @@ from math import gcd +from imp.math.numbers import bigomega +def coprime(n: int, m: int) -> bool: + return gcd(n, m) == 1 + +def almostprime(n: int, k: int) -> bool: + ''' + A natural n is "k-almost prime" if it has exactly + k prime factors (including multiplicity). + ''' + return (bigomega(n) == k) + +def semiprime(n: int) -> bool: + ''' + A semiprime number is one that is 2-almost prime. + Ref: https://en.wikipedia.org/wiki/Semiprime + ''' + return almostprime(n, 2) + ''' Euler's Totient (Phi) Function '''