number_theory_functions¶
shortfx.fxNumeric.number_theory_functions
¶
Number Theory Functions Module.
Provides functions for primality testing, prime generation, divisor analysis, Fibonacci sequences, digital roots, and other number-theoretic operations.
Example
from shortfx.fxNumeric.number_theory_functions import is_prime, divisors is_prime(7) True divisors(12) [1, 2, 3, 4, 6, 12]
Functions¶
additive_persistence(n: int) -> int
¶
Count the additive persistence of n.
The number of times you must sum the digits of n to reach a single digit (the digital root).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of addition steps. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
additive_persistence(199) 3 additive_persistence(5) 0
Complexity: O(log(n) * steps)
Source code in shortfx/fxNumeric/number_theory_functions.py
aliquot_sum(n: int) -> int
¶
Compute the aliquot sum s(n) = σ₁(n) - n (sum of proper divisors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of proper divisors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
aliquot_sum(12) 16
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
bell_number(n: int) -> int
¶
Return the n-th Bell number.
The Bell number B(n) counts the number of partitions of a set with n elements. Computed via the Bell triangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Bell number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
bell_number(5) 52
Complexity: O(n²)
Source code in shortfx/fxNumeric/number_theory_functions.py
big_omega(n: int) -> int
¶
Count prime factors with multiplicity Ω(n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of prime factors counted with multiplicity. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
big_omega(12) 3
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
carmichael_lambda(n: int) -> int
¶
Compute the Carmichael function λ(n).
Description
The smallest positive integer m such that a^m ≡ 1 (mod n) for every integer a coprime to n. Also known as the reduced totient function or least universal exponent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer ≥ 1. |
required |
Returns:
| Type | Description |
|---|---|
int
|
λ(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
carmichael_lambda(8) 2 carmichael_lambda(15) 4
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 | |
catalan_number(n: int) -> int
¶
Calculates the nth Catalan number.
Catalan numbers count the number of correct bracket sequences, binary trees with n+1 leaves, triangulations of polygons, and many other combinatorial structures.
C(n) = (2n)! / ((n+1)! * n!)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The nth Catalan number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
catalan_number(0) 1 catalan_number(5) 42 catalan_number(10) 16796
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
chinese_remainder_theorem(remainders: List[int], moduli: List[int]) -> int
¶
Solves a system of simultaneous congruences via CRT.
Given x ≡ r_i (mod m_i) for pairwise coprime moduli, returns the unique solution x in [0, M) where M = product of all moduli.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remainders
|
List[int]
|
List of remainders r_i. |
required |
moduli
|
List[int]
|
List of pairwise coprime moduli m_i (each > 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The unique solution x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of integers. |
ValueError
|
If lists are empty, different lengths, or moduli not coprime. |
Example
chinese_remainder_theorem([2, 3, 2], [3, 5, 7]) 23
Complexity: O(n log M) where M is the product of moduli.
Source code in shortfx/fxNumeric/number_theory_functions.py
collatz_length(n: int) -> int
¶
Counts the number of steps in the Collatz sequence until reaching 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
A positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of steps. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n < 1. |
Example
collatz_length(6) 8 collatz_length(1) 0
Complexity: O(?) -- conjectured to terminate for all n.
Source code in shortfx/fxNumeric/number_theory_functions.py
collatz_steps(n: int) -> int
¶
Count steps to reach 1 in the Collatz sequence.
Rules: if n is even → n/2, if odd → 3n+1. Repeat until n == 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of steps to reach 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
collatz_steps(6) 8 collatz_steps(1) 0
Complexity: O(steps) — conjectured to always terminate
Source code in shortfx/fxNumeric/number_theory_functions.py
continued_fraction_expansion(numerator: int, denominator: int) -> list[int]
¶
Compute the continued fraction expansion of a rational number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
int
|
Integer numerator. |
required |
denominator
|
int
|
Integer denominator (non-zero). |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of continued fraction coefficients [a₀, a₁, a₂, …]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If denominator is zero. |
Example
continued_fraction_expansion(355, 113) [3, 7, 16]
Complexity: O(log(min(numerator, denominator)))
Source code in shortfx/fxNumeric/number_theory_functions.py
convergents(cf: list[int]) -> list[tuple[int, int]]
¶
Compute the convergents of a continued fraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cf
|
list[int]
|
Continued fraction coefficients [a₀, a₁, a₂, …]. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
List of |
list[tuple[int, int]]
|
from the 0th to the last. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If cf is not a list of integers. |
ValueError
|
If cf is empty. |
Example
convergents([3, 7, 16]) [(3, 1), (22, 7), (355, 113)]
Complexity: O(n), n = len(cf)
Source code in shortfx/fxNumeric/number_theory_functions.py
coprime(a: int, b: int) -> bool
¶
Check if a and b are coprime (gcd(a, b) = 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Integer. |
required |
b
|
int
|
Integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if gcd(a, b) = 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b is not an integer. |
Usage Example
coprime(8, 15) True
Complexity: O(log(min(a, b)))
Source code in shortfx/fxNumeric/number_theory_functions.py
count_digits(n: int) -> int
¶
Counts the number of decimal digits of an integer.
For negative integers, the sign is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
An integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of digits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
count_digits(12345) 5
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
count_divisors(n: int) -> int
¶
Count the number of positive divisors of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of divisors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
count_divisors(12) 6 count_divisors(7) 2
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
delannoy_number(m: int, n: int) -> int
¶
Return the central Delannoy number D(m, n).
D(m, n) counts the paths from (0,0) to (m,n) using steps (1,0), (0,1), and (1,1).
Recurrence: D(m, n) = D(m−1, n) + D(m, n−1) + D(m−1, n−1), with D(0, ·) = D(·, 0) = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Row index (must be ≥ 0). |
required |
n
|
int
|
Column index (must be ≥ 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The Delannoy number D(m, n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If m or n is not an integer. |
ValueError
|
If m or n is negative. |
Example
delannoy_number(3, 3) 63
Complexity: O(m · n)
Source code in shortfx/fxNumeric/number_theory_functions.py
digit_count(n: int) -> int
¶
Count the number of digits in a non-negative integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of digits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
digit_count(12345) 5 digit_count(0) 1
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
digit_factorial_sum(n: int) -> int
¶
Compute the sum of factorials of the digits of n.
Description
For each digit d of n, computes d! and sums them. Numbers equal to their digit factorial sum are called factorions (e.g. 145 = 1! + 4! + 5!).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of digit factorials. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Usage Example
digit_factorial_sum(145) 145 digit_factorial_sum(123) 9
Complexity: O(d) where d is the number of digits.
Source code in shortfx/fxNumeric/number_theory_functions.py
digital_root(n: int) -> int
¶
Calculate the digital root of n.
The digital root is the single-digit value obtained by repeatedly summing the digits of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Single-digit digital root. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
digital_root(493) 7 digital_root(0) 0
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
digital_sum(n: int) -> int
¶
Computes the sum of digits of an integer.
For negative integers, the sign is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
An integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of its decimal digits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
digital_sum(12345) 15
Complexity: O(d) where d is the number of digits
Source code in shortfx/fxNumeric/number_theory_functions.py
discrete_logarithm(g: int, h: int, p: int) -> int
¶
Compute the discrete logarithm: find x such that g^x ≡ h (mod p).
Uses the baby-step giant-step algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
int
|
Base. |
required |
h
|
int
|
Target value. |
required |
p
|
int
|
Modulus (should be prime for guaranteed result). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The exponent x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not integers. |
ValueError
|
If no solution is found. |
Example
discrete_logarithm(2, 8, 13) 3
Complexity: O(√p) time and space.
Source code in shortfx/fxNumeric/number_theory_functions.py
divisors(n: int) -> list[int]
¶
Returns all positive divisors of n in ascending order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
A positive integer. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
Sorted list of divisors. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n < 1. |
Example
divisors(12) [1, 2, 3, 4, 6, 12]
Complexity: O(sqrt(n))
Source code in shortfx/fxNumeric/number_theory_functions.py
egyptian_fraction(numerator: int, denominator: int) -> list[int]
¶
Decompose a fraction into a sum of distinct unit fractions.
Uses the greedy (Sylvester-Fibonacci) algorithm: find the smallest n such that 1/n ≤ a/b, subtract, repeat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
int
|
Positive integer numerator. |
required |
denominator
|
int
|
Positive integer denominator. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of denominators of the unit fractions. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not integers. |
ValueError
|
If numerator or denominator ≤ 0. |
Example
egyptian_fraction(3, 7) [3, 11, 231]
Complexity: O(numerator) average.
Source code in shortfx/fxNumeric/number_theory_functions.py
euler_totient(n: int) -> int
¶
Calculates Euler's totient function phi(n).
Returns the count of integers from 1 to n that are coprime to n. Fundamental in number theory and used in RSA cryptography.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of integers coprime to n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Example
euler_totient(12) 4 euler_totient(7) 6
Complexity: O(sqrt(n))
Source code in shortfx/fxNumeric/number_theory_functions.py
eulerian_number(n: int, k: int) -> int
¶
Return the Eulerian number A(n, k).
The Eulerian number counts the number of permutations of {1, …, n} with exactly k ascents.
Recurrence: A(n, k) = (k+1)·A(n−1, k) + (n−k)·A(n−1, k−1), with A(0, 0) = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Permutation size (must be ≥ 0). |
required |
k
|
int
|
Number of ascents (must be 0 ≤ k < n, or k = 0 if n = 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The Eulerian number A(n, k). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k is not an integer. |
ValueError
|
If n < 0 or k is out of range. |
Example
eulerian_number(4, 1) 11
Complexity: O(n · k)
Source code in shortfx/fxNumeric/number_theory_functions.py
fibonacci_sequence(n: int) -> list[int]
¶
Returns the first n Fibonacci numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
How many numbers to generate (>= 0). |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
A list with the first n Fibonacci numbers. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n is negative. |
Example
fibonacci_sequence(7) [0, 1, 1, 2, 3, 5, 8]
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
get_primes_up_to(limit: int) -> list[int]
¶
Generates a list of prime numbers up to a given limit using the Sieve of Eratosthenes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
The upper limit to search for prime numbers. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: List of prime numbers found. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the limit is less than 2. |
Example
get_primes_up_to(10) [2, 3, 5, 7] get_primes_up_to(20) [2, 3, 5, 7, 11, 13, 17, 19]
Cost: O(n log log n), Sieve of Eratosthenes algorithm.
Source code in shortfx/fxNumeric/number_theory_functions.py
goldbach_partition(n: int) -> tuple
¶
Finds a Goldbach partition for an even integer >= 4.
Goldbach's conjecture states that every even integer ≥ 4 can be expressed as the sum of two primes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
An even integer >= 4. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple (p, q) where both are prime and p + q == n, with p <= q. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is odd or less than 4. |
Example
goldbach_partition(28) (5, 23) goldbach_partition(100) (3, 97)
Complexity: O(n * sqrt(n))
Source code in shortfx/fxNumeric/number_theory_functions.py
integer_partitions_count(n: int) -> int
¶
Counts the number of integer partitions of n.
A partition of n is a way of writing n as a sum of positive integers, where order does not matter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of partitions p(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
integer_partitions_count(5) 7 integer_partitions_count(0) 1
Complexity: O(n^2)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_abundant_number(n: int) -> bool
¶
Check if n is an abundant number (σ₁(n) > 2n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is abundant. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_abundant_number(12) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_achilles_number(n: int) -> bool
¶
Check if n is an Achilles number.
An Achilles number is powerful (p²|n for every prime p|n) but not a perfect power.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is an Achilles number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Example
is_achilles_number(72) True is_achilles_number(36) False
Complexity: O(√n · log n)
Source code in shortfx/fxNumeric/number_theory_functions.py
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 | |
is_amicable_pair(a: int, b: int) -> bool
¶
Check if (a, b) is an amicable pair: s(a) = b and s(b) = a.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Positive integer. |
required |
b
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if (a, b) is an amicable pair. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b is not an integer. |
ValueError
|
If a or b < 1 or a == b. |
Usage Example
is_amicable_pair(220, 284) True
Complexity: O(√a + √b)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_automorphic_number(n: int) -> bool
¶
Check if n is automorphic: n² ends with n in decimal representation.
Examples: 1, 5, 6, 25, 76, 376, 625, ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is automorphic, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
is_automorphic_number(76) True is_automorphic_number(77) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_coprime(a: int, b: int) -> bool
¶
Checks whether two integers are coprime.
Two integers are coprime if their greatest common divisor is 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First integer. |
required |
b
|
int
|
Second integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if gcd(a, b) == 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
Example
is_coprime(14, 15) True is_coprime(14, 21) False
Complexity: O(log(min(a, b)))
Source code in shortfx/fxNumeric/number_theory_functions.py
is_cube_number(n: int) -> bool
¶
Check if n is a perfect cube.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a perfect cube, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
is_cube_number(27) True is_cube_number(10) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_deficient_number(n: int) -> bool
¶
Check if n is a deficient number (σ₁(n) < 2n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is deficient. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_deficient_number(8) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_even(n: int) -> bool
¶
Checks whether an integer is even.
Description
Returns True if n is divisible by 2. Equivalent to Excel ISEVEN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The integer to test. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if n is even, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
is_even(4) True is_even(7) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_fibonacci_number(n: int) -> bool
¶
Check whether a non-negative integer belongs to the Fibonacci sequence.
Description
Uses the property that n is Fibonacci if and only if 5n² + 4 or 5n² − 4 is a perfect square.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a Fibonacci number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Usage Example
is_fibonacci_number(13) True is_fibonacci_number(14) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_happy_number(n: int) -> bool
¶
Check if n is a happy number.
A happy number eventually reaches 1 when replaced by the sum of the squares of its digits repeatedly. A number that never reaches 1 enters a cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a happy number, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
is_happy_number(19) True is_happy_number(2) False
Complexity: O(log(n) * cycle_length)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_harshad_number(n: int) -> bool
¶
Check if n is a Harshad (Niven) number (divisible by its digit sum).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a Harshad number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_harshad_number(18) True
Complexity: O(log n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_kaprekar_number(n: int) -> bool
¶
Check if n is a Kaprekar number.
n is Kaprekar if n² can be split into two parts that sum to n (right part must not be zero and left part can be empty/0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a Kaprekar number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_kaprekar_number(45) True
Complexity: O(log n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_lychrel_candidate(n: int, max_iterations: int = 50) -> bool
¶
Check if a number is a Lychrel candidate.
Description
A Lychrel number never forms a palindrome through the iterative process of reversing digits and adding. Since this is unproven for any base-10 number, this function tests up to max_iterations steps. If no palindrome is found, the number is considered a candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
max_iterations
|
int
|
Maximum reverse-and-add steps (default 50). |
50
|
Returns:
| Type | Description |
|---|---|
bool
|
True if n does not produce a palindrome within the limit. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0 or max_iterations < 1. |
Usage Example
is_lychrel_candidate(196) True is_lychrel_candidate(56) False
Complexity: O(max_iterations × d) where d is the digit count.
Source code in shortfx/fxNumeric/number_theory_functions.py
is_narcissistic_number(n: int) -> bool
¶
Checks if n is a narcissistic (Armstrong) number.
An m-digit number where the sum of each digit raised to the m-th power equals the number itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
A non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is narcissistic, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
is_narcissistic_number(153) True is_narcissistic_number(10) False
Complexity: O(m) where m = number of digits
Source code in shortfx/fxNumeric/number_theory_functions.py
is_odd(n: int) -> bool
¶
Checks whether an integer is odd.
Description
Returns True if n is not divisible by 2. Equivalent to Excel ISODD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The integer to test. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if n is odd, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
is_odd(7) True is_odd(4) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_palindrome_number(n: int) -> bool
¶
Checks whether an integer is a palindrome.
A palindrome number reads the same forwards and backwards. Negative numbers are not considered palindromes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a palindrome number, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
is_palindrome_number(121) True is_palindrome_number(123) False is_palindrome_number(-121) False
Complexity: O(d) where d is the number of digits.
Source code in shortfx/fxNumeric/number_theory_functions.py
is_palindromic_number(n: int) -> bool
¶
Check if n is a palindromic number in base 10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n reads the same forwards and backwards. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
is_palindromic_number(12321) True
Complexity: O(log n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_perfect_number(n: int) -> bool
¶
Check if n is a perfect number (σ₁(n) = 2n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is perfect. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_perfect_number(28) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_perfect_power(n: int) -> bool
¶
Check if n can be expressed as a^b where a, b are integers and b > 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a perfect power, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is less than 1. |
Example
is_perfect_power(8) True is_perfect_power(10) False
Complexity: O(log(n)^2)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_power_of_two(n: int) -> bool
¶
Checks whether a positive integer is a power of two.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a power of two. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
is_power_of_two(64) True
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_powerful_number(n: int) -> bool
¶
Check if n is a powerful number (every prime factor appears ≥ 2 times).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is powerful. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_powerful_number(72) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_prime(n: int) -> bool
¶
Checks if a number is prime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the number is prime, False otherwise. |
Example
is_prime(7) True is_prime(4) False is_prime(2) True is_prime(1) False
Cost: O(sqrt(n)), where n is the number to check.
Source code in shortfx/fxNumeric/number_theory_functions.py
is_pronic_number(n: int) -> bool
¶
Check if n is a pronic (oblong) number: n = k * (k + 1) for some k >= 0.
Pronic numbers: 0, 2, 6, 12, 20, 30, 42, ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is pronic, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
is_pronic_number(6) True is_pronic_number(7) False
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_semiprime(n: int) -> bool
¶
Check if n is a semiprime (product of exactly two primes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer ≥ 2. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a semiprime. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 2. |
Usage Example
is_semiprime(15) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_smith_number(n: int) -> bool
¶
Check if n is a Smith number.
A Smith number is a composite where the digit sum equals the sum of digit sums of its prime factors (with multiplicity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Integer ≥ 2. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a Smith number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 2. |
Usage Example
is_smith_number(22) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_sphenic_number(n: int) -> bool
¶
Check if n is a sphenic number (product of exactly 3 distinct primes).
Examples: 30 = 2·3·5, 66 = 2·3·11.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is sphenic, False otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
is_sphenic_number(30) True is_sphenic_number(12) False
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_square_free(n: int) -> bool
¶
Check if n is square-free (no prime factor appears more than once).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is square-free. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_square_free(30) True
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_square_number(n: int) -> bool
¶
Checks whether a non-negative integer is a perfect square.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a perfect square. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
is_square_number(49) True
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
is_triangular_number(n: int) -> bool
¶
Checks whether a non-negative integer is a triangular number.
T_k = k(k+1)/2. Solves 8n+1 being a perfect square.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is a triangular number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
is_triangular_number(10) True
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
jacobi_symbol(a: int, n: int) -> int
¶
Compute the Jacobi symbol (a/n).
Generalization of the Legendre symbol to odd positive n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Integer. |
required |
n
|
int
|
Odd positive integer ≥ 3. |
required |
Returns:
| Type | Description |
|---|---|
int
|
-1, 0, or 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or n is not an integer. |
ValueError
|
If n < 3 or n is even. |
Usage Example
jacobi_symbol(2, 7) 1
Complexity: O(log(n)²)
Source code in shortfx/fxNumeric/number_theory_functions.py
jacobsthal_number(n: int) -> int
¶
Calculate the n-th Jacobsthal number.
J(0)=0, J(1)=1, J(n)=J(n-1)+2·J(n-2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Jacobsthal number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
jacobsthal_number(6) 21
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
kronecker_symbol(a: int, n: int) -> int
¶
Compute the Kronecker symbol (a|n).
Extension of the Jacobi symbol to all integers n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Integer. |
required |
n
|
int
|
Integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
-1, 0, or 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or n is not an integer. |
Usage Example
kronecker_symbol(2, 7) 1
Complexity: O(log(n)²)
Source code in shortfx/fxNumeric/number_theory_functions.py
legendre_symbol(a: int, p: int) -> int
¶
Compute the Legendre symbol (a/p) for odd prime p.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Integer. |
required |
p
|
int
|
Odd prime. |
required |
Returns:
| Type | Description |
|---|---|
int
|
-1, 0, or 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or p is not an integer. |
ValueError
|
If p < 3 or p is even. |
Usage Example
legendre_symbol(2, 7) 1
Complexity: O(log(p))
Source code in shortfx/fxNumeric/number_theory_functions.py
liouville_lambda(n: int) -> int
¶
Compute the Liouville function λ(n) = (-1)^Ω(n).
Ω(n) is the number of prime factors of n counted with multiplicity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
1 or -1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
liouville_lambda(12) -1
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
lucas_number(n: int) -> int
¶
Calculate the n-th Lucas number.
L(0)=2, L(1)=1, L(n)=L(n-1)+L(n-2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Lucas number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
lucas_number(5) 11
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
lucas_sequence(n: int) -> List[int]
¶
Generates the first n Lucas numbers.
The Lucas sequence starts with 2, 1 and follows the same recurrence as Fibonacci: L(n) = L(n-1) + L(n-2). It is closely related to Fibonacci numbers and appears in various mathematical identities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of Lucas numbers to generate (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
List of the first n Lucas numbers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Example
lucas_sequence(8) [2, 1, 3, 4, 7, 11, 18, 29]
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
mobius_function(n: int) -> int
¶
Compute the Möbius function μ(n).
μ(n) = 1 if n is square-free with even number of prime factors, -1 if square-free with odd number, 0 if not square-free.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
-1, 0, or 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
mobius_function(30) -1
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
modular_exponentiation(base: int, exponent: int, modulus: int) -> int
¶
Computes (base ** exponent) % modulus efficiently.
Uses Python's built-in three-argument pow for fast modular
exponentiation with the square-and-multiply algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
int
|
The base integer. |
required |
exponent
|
int
|
The exponent (must be >= 0). |
required |
modulus
|
int
|
The modulus (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Result of (base ** exponent) mod modulus. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If exponent < 0 or modulus <= 0. |
Example
modular_exponentiation(2, 10, 1000) 24 modular_exponentiation(3, 13, 7) 3
Complexity: O(log(exponent))
Source code in shortfx/fxNumeric/number_theory_functions.py
modular_inverse(a: int, m: int) -> int
¶
Computes the modular multiplicative inverse of a modulo m.
Finds x such that (a * x) % m == 1 using the extended Euclidean algorithm. The inverse exists only when gcd(a, m) == 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
The integer whose inverse is sought. |
required |
m
|
int
|
The modulus (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The modular inverse in the range [0, m). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If m <= 0 or the inverse does not exist. |
Example
modular_inverse(3, 7) 5 modular_inverse(10, 17) 12
Complexity: O(log(min(a, m)))
Source code in shortfx/fxNumeric/number_theory_functions.py
motzkin_number(n: int) -> int
¶
Return the n-th Motzkin number.
Motzkin numbers count the number of different ways of drawing non-intersecting chords among n points on a circle.
Recurrence: (n + 2) · M(n) = (2n + 1) · M(n−1) + 3(n − 1) · M(n−2), with M(0) = M(1) = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Motzkin number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
motzkin_number(5) 21
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
multiplicative_persistence(n: int) -> int
¶
Count the multiplicative persistence of n.
The number of times you must multiply the digits of n to reach a single digit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of multiplication steps. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
multiplicative_persistence(39) 3 multiplicative_persistence(999) 4
Complexity: O(log(n) * steps)
Source code in shortfx/fxNumeric/number_theory_functions.py
narayana_number(n: int, k: int) -> int
¶
Return the Narayana number N(n, k).
N(n, k) = C(n, k) · C(n, k−1) / n, where C is the binomial coefficient. Narayana numbers refine the Catalan numbers and count the number of paths that have exactly k peaks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Row index (must be ≥ 1). |
required |
k
|
int
|
Column index (must be 1 ≤ k ≤ n). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The Narayana number N(n, k). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k is not an integer. |
ValueError
|
If n < 1 or k is outside [1, n]. |
Example
narayana_number(4, 2) 6
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
next_prime(n: int) -> int
¶
Finds the smallest prime number greater than n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Starting integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The next prime after n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
Example
next_prime(10) 11 next_prime(13) 17 next_prime(1) 2
Complexity: O(k * sqrt(k)) where k is the gap to the next prime.
Source code in shortfx/fxNumeric/number_theory_functions.py
nth_fibonacci(n: int) -> int
¶
Returns the n-th Fibonacci number using fast doubling.
F(0) = 0, F(1) = 1, F(k) = F(k-1) + F(k-2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index into the Fibonacci sequence. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Fibonacci number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
nth_fibonacci(10) 55 nth_fibonacci(0) 0
Complexity: O(log n)
Source code in shortfx/fxNumeric/number_theory_functions.py
number_of_distinct_prime_factors(n: int) -> int
¶
Count the number of distinct prime factors of n (ω(n)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer (≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of distinct prime factors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 2. |
Example
number_of_distinct_prime_factors(12) 2 number_of_distinct_prime_factors(30) 3
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
number_of_divisors(n: int) -> int
¶
Compute d(n) = σ₀(n), the number of divisors of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of divisors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
number_of_divisors(12) 6
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
omega_prime(n: int) -> int
¶
Count distinct prime factors ω(n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of distinct prime factors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
omega_prime(12) 2
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
partition_function(n: int) -> int
¶
Computes the number of integer partitions p(n).
p(n) is the number of ways to write n as a sum of positive integers (order doesn't matter).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
p(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
partition_function(5) 7 partition_function(10) 42
Complexity: O(n^2)
Source code in shortfx/fxNumeric/number_theory_functions.py
partition_number(n: int) -> int
¶
Return the number of integer partitions of n.
p(n) counts the number of ways to write n as a sum of positive integers, disregarding order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The partition number p(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
partition_number(5) 7
Complexity: O(n²)
Source code in shortfx/fxNumeric/number_theory_functions.py
pell_number(n: int) -> int
¶
Calculate the n-th Pell number.
P(0)=0, P(1)=1, P(n)=2·P(n-1)+P(n-2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Pell number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
pell_number(6) 70
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
pentagonal_number(n: int) -> int
¶
Return the n-th pentagonal number.
P(n) = n · (3n − 1) / 2. The sequence starts 0, 1, 5, 12, 22, …
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th pentagonal number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
pentagonal_number(5) 35
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
prime_counting_approx(x: float) -> float
¶
Approximate π(x), the number of primes ≤ x, using x/ln(x).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Positive real number > 1. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Approximate prime count. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x ≤ 1. |
Usage Example
round(prime_counting_approx(1000), 1) 144.8
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
prime_factors(n: int) -> list[int]
¶
Returns the prime factorization of n in ascending order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
An integer >= 2. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of prime factors (with repetition). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n < 2. |
Example
prime_factors(60) [2, 2, 3, 5] prime_factors(17) [17]
Complexity: O(sqrt(n))
Source code in shortfx/fxNumeric/number_theory_functions.py
prime_nth_approx(n: int) -> float
¶
Approximate the n-th prime using n·ln(n) + n·ln(ln(n)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer (the index, 1-based). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the n-th prime. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
round(prime_nth_approx(100)) 613
Complexity: O(1)
Source code in shortfx/fxNumeric/number_theory_functions.py
primitive_root(p: int) -> int
¶
Finds the smallest primitive root modulo p (p must be prime).
A primitive root g has order p-1 modulo p, meaning g generates the multiplicative group (Z/pZ)*.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
int
|
Prime number > 1. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Smallest primitive root. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not an integer. |
ValueError
|
If p < 2 or p is not prime. |
Example
primitive_root(7) 3 primitive_root(11) 2
Complexity: O(p * √p) worst case.
Source code in shortfx/fxNumeric/number_theory_functions.py
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 | |
radical(n: int) -> int
¶
Compute the radical of n: product of distinct prime factors.
rad(n) = Π p for each prime p dividing n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Radical of n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
radical(12) 6
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
schroeder_number(n: int) -> int
¶
Return the n-th large Schröder number.
Schröder numbers count the number of lattice paths from (0,0) to (n,n) that do not go above the diagonal, using steps (1,0), (0,1), and (1,1).
Recurrence: S(n) = ((6n − 3) · S(n−1) − (n − 2) · S(n−2)) / (n + 1), with S(0) = 1, S(1) = 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th large Schröder number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
schroeder_number(4) 90
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
squarefree_part(n: int) -> int
¶
Computes the squarefree part of n.
The squarefree part is n divided by the largest perfect square that divides it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Squarefree part. |
Example
squarefree_part(12) 3 squarefree_part(7) 7
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
stern_brocot(n: int) -> List[int]
¶
Generates the first n elements of the Stern-Brocot sequence.
The sequence is: s(0)=0, s(1)=1, s(2n)=s(n), s(2n+1)=s(n)+s(n+1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to generate (>= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
A list of the first n elements. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Example
stern_brocot(10) [0, 1, 1, 2, 1, 3, 2, 3, 1, 4]
Complexity: O(n)
Source code in shortfx/fxNumeric/number_theory_functions.py
stirling_number_second(n: int, k: int) -> int
¶
Return the Stirling number of the second kind S(n, k).
S(n, k) counts the number of ways to partition a set of n elements into k non-empty subsets.
Recurrence: S(n, k) = k · S(n−1, k) + S(n−1, k−1), with S(n, 0) = 0 for n ≥ 1, S(0, 0) = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Set size (must be ≥ 0). |
required |
k
|
int
|
Number of subsets (must be 0 ≤ k ≤ n). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Stirling number of the second kind. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k is not an integer. |
ValueError
|
If n < 0 or k is out of range. |
Example
stirling_number_second(4, 2) 7
Complexity: O(n · k)
Source code in shortfx/fxNumeric/number_theory_functions.py
sum_of_cubes_of_digits(n: int) -> int
¶
Calculate the sum of cubes of the digits of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of cubes of each digit. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
sum_of_cubes_of_digits(123) 36
Complexity: O(d) where d is the number of digits
Source code in shortfx/fxNumeric/number_theory_functions.py
sum_of_divisors(n: int, k: int = 1) -> int
¶
Compute σ_k(n), the sum of k-th powers of divisors of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
k
|
int
|
Power exponent (default 1, giving σ₁ = sum of divisors). |
1
|
Returns:
| Type | Description |
|---|---|
int
|
σ_k(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k is not an integer. |
ValueError
|
If n < 1 or k < 0. |
Usage Example
sum_of_divisors(12) 28
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
sum_of_squares_count(n: int) -> int
¶
Counts the number of representations of n as a sum of two squares.
r2(n) counts ordered pairs (a, b) with a² + b² = n (including signs and order).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of representations r2(n). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
sum_of_squares_count(5) 8 sum_of_squares_count(1) 4
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
sum_of_squares_of_digits(n: int) -> int
¶
Calculate the sum of squares of the digits of n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of squared digits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Example
sum_of_squares_of_digits(19) 82 sum_of_squares_of_digits(0) 0
Complexity: O(d) where d is the number of digits
Source code in shortfx/fxNumeric/number_theory_functions.py
sum_proper_divisors(n: int) -> int
¶
Calculate the sum of proper divisors of n (all divisors except n itself).
Also known as the aliquot sum s(n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of proper divisors. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is not positive. |
Example
sum_proper_divisors(12) 16 sum_proper_divisors(1) 0
Complexity: O(√n)
Source code in shortfx/fxNumeric/number_theory_functions.py
tribonacci_number(n: int) -> int
¶
Calculate the n-th tribonacci number.
T(0)=0, T(1)=0, T(2)=1, T(n)=T(n-1)+T(n-2)+T(n-3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th tribonacci number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Example
tribonacci_number(7) 13
Complexity: O(n)