Pywayne Maths
by @wangyendt
Mathematical utility functions for factorization, digit counting, and large integer multiplication using Karatsuba algorithm. Use when solving number theory...
clawhub install mathsπ About This Skill
name: pywayne-maths description: Mathematical utility functions for factorization, digit counting, and large integer multiplication using Karatsuba algorithm. Use when solving number theory problems, computing factors, counting digit occurrences, or performing optimized large integer multiplication.
Pywayne Maths
Mathematical utility functions for number theory, digit analysis, and optimized integer operations.
Quick Start
from pywayne.maths import get_all_factors, digitCount, karatsuba_multiplicationGet all factors of a number
factors = get_all_factors(28)
print(factors) # [1, 2, 4, 7, 14, 28]Count digit occurrences
count = digitCount(100, 1)
print(count) # 21 (digit 1 appears 21 times in 1-100)Large integer multiplication
product = karatsuba_multiplication(1234, 5678)
print(product) # 7006652
Functions
get_all_factors
Return all factors of a positive integer.
get_all_factors(n: int) -> list
Parameters:
n - Positive integer to factorizeReturns:
nUse Cases:
Example:
from pywayne.maths import get_all_factorsfactors = get_all_factors(36)
print(factors) # [1, 2, 3, 4, 6, 9, 12, 18, 36]
Check if number is prime
n = 17
factors = get_all_factors(n)
if len(factors) == 2: # Only 1 and itself
print(f"{n} is prime")
else:
print(f"{n} is not prime")
digitCount
Count occurrences of digit k from 1 to n.
digitCount(n, k) -> int
Parameters:
n - Positive integer, upper bound of counting rangek - Digit to count (0-9)Returns:
k in range [1, n]Special Case:
k = 0, counts all numbers with trailing zeros after nUse Cases:
Example:
from pywayne.maths import digitCountCount digit 1 from 1 to 100
count = digitCount(100, 1)
print(count) # 21Count each digit 0-9 in range 1-1000
for k in range(10):
count = digitCount(1000, k)
print(f"Digit {k}: {count} times")
karatsuba_multiplication
Multiply two integers using Karatsuba's divide-and-conquer algorithm.
karatsuba_multiplication(x: int, y: int) -> int
Parameters:
x - Integer multipliery - Integer multiplicandReturns:
x and yAlgorithm:
Use Cases:
Example:
from pywayne.maths import karatsuba_multiplicationCompare with standard multiplication
a, b = 123456789, 987654321
result = karatsuba_multiplication(a, b)
print(result) # 121932631112635269Verify
assert result == a * b
Common Applications
Prime Number Detection
from pywayne.maths import get_all_factorsdef is_prime(n):
factors = get_all_factors(n)
return len(factors) == 2 and factors == [1, n]
print(is_prime(17)) # True
print(is_prime(18)) # False
Greatest Common Divisor (GCD)
from pywayne.maths import get_all_factorsdef gcd(a, b):
factors_a = set(get_all_factors(a))
factors_b = set(get_all_factors(b))
common = factors_a & factors_b
return max(common)
print(gcd(24, 36)) # 12
Digit Frequency Analysis
from pywayne.maths import digitCountdef digit_frequency(n):
frequency = {}
for k in range(10):
frequency[k] = digitCount(n, k)
return frequency
print(digit_frequency(1000))
{0: 189, 1: 301, 2: 300, 3: 300, ...}
Large Number Calculations
from pywayne.maths import karatsuba_multiplicationVery large numbers
x = 123456789012345678901234567890
y = 9876543210987654321098765432109876Use Karatsuba for efficiency
product = karatsuba_multiplication(x, y)
Notes
get_all_factors returns sorted unique factorsdigitCount counts from 1 to n inclusivekaratsuba_multiplication is optimized for large integers (hundreds+ of digits)* may be faster due to overheadπ‘ Examples
from pywayne.maths import get_all_factors, digitCount, karatsuba_multiplicationGet all factors of a number
factors = get_all_factors(28)
print(factors) # [1, 2, 4, 7, 14, 28]Count digit occurrences
count = digitCount(100, 1)
print(count) # 21 (digit 1 appears 21 times in 1-100)Large integer multiplication
product = karatsuba_multiplication(1234, 5678)
print(product) # 7006652
π Tips & Best Practices
get_all_factors returns sorted unique factorsdigitCount counts from 1 to n inclusivekaratsuba_multiplication is optimized for large integers (hundreds+ of digits)* may be faster due to overhead