Compare commits
2 commits
9e2ea469db
...
d403da53db
| Author | SHA1 | Date | |
|---|---|---|---|
| d403da53db | |||
| 3a407b6456 |
5 changed files with 106 additions and 16 deletions
|
|
@ -1,16 +0,0 @@
|
||||||
from imp.math.primefac import factors
|
|
||||||
|
|
||||||
def divisors(n: int) -> int:
|
|
||||||
'''
|
|
||||||
Returns the proper divisors of an integer n.
|
|
||||||
Also called the "aliquot parts" of n.
|
|
||||||
'''
|
|
||||||
pf = factors(n)
|
|
||||||
for (prime, multiplicity) in pf:
|
|
||||||
|
|
||||||
|
|
||||||
def aliquots(n: int) -> int:
|
|
||||||
return proper_divisors(n)
|
|
||||||
|
|
||||||
def amicable(n: int) -> int:
|
|
||||||
sum(proper_divisors())
|
|
||||||
82
imp/math/numbers/__init__.py
Normal file
82
imp/math/numbers/__init__.py
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
'''
|
||||||
|
Terminology:
|
||||||
|
Although "divisor" and "factor" mean the same thing.
|
||||||
|
When Celeste discusses "divisors of n" it is implied to
|
||||||
|
mean "proper divisors of n + n itself", and "factors" are
|
||||||
|
the "prime proper divisors of n".
|
||||||
|
'''
|
||||||
|
|
||||||
|
from imp.math.primefac import primefac
|
||||||
|
|
||||||
|
def factors(n: int) -> int:
|
||||||
|
pfactors: list[tuple[int, int]] = []
|
||||||
|
# generate primes and progressively store them in pfactors
|
||||||
|
pfgen = primefac(n)
|
||||||
|
watching = next(pfgen)
|
||||||
|
mult = 1
|
||||||
|
# ASSUMPTION: prime generation is (non-strict) monotone increasing
|
||||||
|
while True:
|
||||||
|
p = next(pfgen, None)
|
||||||
|
if p == watching:
|
||||||
|
mult += 1
|
||||||
|
else:
|
||||||
|
pfactors.append((watching, mult))
|
||||||
|
watching = p # reset
|
||||||
|
mult = 1 # reset
|
||||||
|
if p is None:
|
||||||
|
break
|
||||||
|
return pfactors
|
||||||
|
|
||||||
|
def factors2divisors(pfactors: list[tuple[int, int]],
|
||||||
|
sorted: bool = True) -> list[int]:
|
||||||
|
'''
|
||||||
|
Generates all divisors < n of an integer n given its prime factorisation.
|
||||||
|
Input: prime factorisation of n (excluding 1 and n, and duplicates)
|
||||||
|
in the typical form: list[(prime, multiplicity)]
|
||||||
|
'''
|
||||||
|
divisors = [1]
|
||||||
|
for (prime, multiplicity) in pfactors:
|
||||||
|
extension = []
|
||||||
|
for i in range(1, multiplicity+1):
|
||||||
|
term = prime**i
|
||||||
|
extension.extend(list([divisor*term for divisor in divisors]))
|
||||||
|
divisors.extend(extension)
|
||||||
|
if sorted: divisors.sort()
|
||||||
|
return divisors
|
||||||
|
|
||||||
|
def factors2aliquots(pfactors: list[tuple[int, int]]) -> list[int]:
|
||||||
|
return factors2divisors(pfactors)[:-1]
|
||||||
|
|
||||||
|
# "aliquots(n)" is an alias for "divisors(n)"
|
||||||
|
def aliquots(n: int) -> int:
|
||||||
|
'''
|
||||||
|
Returns all aliquot parts (proper divisors) of
|
||||||
|
an integer n, that is all divisors 0 < d <= n.
|
||||||
|
'''
|
||||||
|
return factors2aliquots(factors(n))
|
||||||
|
|
||||||
|
def divisors(n: int) -> int:
|
||||||
|
'''
|
||||||
|
Returns all divisors 0 < d < n of an integer n.
|
||||||
|
'''
|
||||||
|
return factors2divisors(factors(n))
|
||||||
|
|
||||||
|
def aliquot_sum(n: int) -> int:
|
||||||
|
return sum(aliquots(n))
|
||||||
|
|
||||||
|
|
||||||
|
def littleomega(n: int) -> int:
|
||||||
|
'''
|
||||||
|
The Little Omega function counts the number of
|
||||||
|
distinct prime factors of an integer n.
|
||||||
|
Ref: https://en.wikipedia.org/wiki/Prime_omega_function
|
||||||
|
'''
|
||||||
|
return len(factors(n))
|
||||||
|
|
||||||
|
def bigomega(n: int) -> int:
|
||||||
|
'''
|
||||||
|
The Big Omega function counts the total number of
|
||||||
|
prime factors (including multiplicity) of an integer n.
|
||||||
|
Ref: https://en.wikipedia.org/wiki/Prime_omega_function
|
||||||
|
'''
|
||||||
|
return sum(factor[1] for factor in factors(n))
|
||||||
5
imp/math/numbers/functions.py
Normal file
5
imp/math/numbers/functions.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
def factorial(n: int) -> int:
|
||||||
|
if n == 0: return 1
|
||||||
|
return n * factorial(n-1)
|
||||||
|
|
||||||
|
def
|
||||||
1
imp/math/numbers/kinds.py
Normal file
1
imp/math/numbers/kinds.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -1,5 +1,23 @@
|
||||||
from math import gcd
|
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
|
Euler's Totient (Phi) Function
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue