Library of number theory related functions inspired by Project Euler.
A generator function that yields prime numbers using the Sieve of Eratosthenes algorithm.
Note
This function is based on the erat2a function which can be found here. Variable names were changed and comments added for clarity.
A generator function that yields prime numbers using the wheel factorized Sieve of Eratosthenes.
Note
This function is based on the erat3 function which can be found here. Variable names were changed and comments added for clarity.
Returns a list of prime numbers. Uses the eulerlib.prime_numbers.prime_gen() generator function.
Parameters: | num – The upper limit for prime numbers list (pn < num) |
---|---|
Returns: | List of prime numbers [p1,p2,...pn] such that pn < num. |
Uses the prime_gen function that uses Sieve of Eratosthanes.
Returns a list of prime numbers. Uses the eulerlib.prime_numbers.prime_wheel_fact_gen() generator function.
Parameters: | num – The upper limit for prime numbers list (pn < num) |
---|---|
Returns: | List of prime numbers [p1,p2,...pn] such that pn < num. |
Uses the prime_wheel_fact_gen function that uses Sieve of Eratosthanes with wheel factorization based on the first 3 primes: 2, 3 and 5.
Determines if a positive integer num is the perfect square.
Parameters: | num – Integer to be checked |
---|---|
Returns: | A tuple (is_square,root) |
Note
Uses Digit-by-digit algorithm in base 10.
Calculates Greatest Common Divisor of two integers.
Parameters: |
|
---|---|
Returns: | Greatest Common Divisor (GCD) of a and b |
Uses Euclid’s algorithm.
Calculates the Least Common Multiple (LCM) of two integers.
Parameters: |
|
---|---|
Returns: | Least Common Multiple (LCM) of a and b |
Calculate the Least Common Multiple of a list of integers.
Parameters: | num_list – A list of integers [i1,i2,i3,...,in] |
---|---|
Returns: | LCM of the integers in num_list |
Uses the associative property LCM(a,b,c) = LCM(LCM(a,b),c)
Calculate ways of selecting r members out of a collection of n members.
Parameters: |
|
---|---|
Returns: | n!/[r!(n-r)!] |
Note
n C r is typically read as n combination r. Order of members is not important in the combination. E.g. There are 4 C 2 = 6 ways of selecting two members out of a collection (A, B, C, D) => AB, AC, AD, BC, BD, CD.
Calculate number of permutations of length r out of a collection of n members (No repeated members).
Parameters: |
|
---|---|
Returns: | n!/[(n-r)!] |
Note
n P r is typically read as n permutation r. Order of members is important in the permutation. E.g. There are 4 P 2 = 12 permutations of length 2 out of a collection (A, B, C, D) => AB, AC, AD, BA, BC, BD, CA, CB, CD, DA, DB, DC.
Calculate the digital sum of a number num in base 10
Parameters: | num – Number for which digital sum is to be calculated. num should be in base 10. |
---|---|
Returns: | Digital sum of num in base 10. |
Calculate the digital root of a number num in base 10.
Parameters: | num – Number for which digital root is to be calculated. num should be in base 10. |
---|---|
Returns: | Digital root of num in base 10. |
Implements methods related to prime factors and divisors.
Parameters: | maxnum – Upper limit for the list of primes. (default = 1000) |
---|
Returns a list of ALL divisors of num (including 1 and num).
Parameters: | num – An integer for which divisors are needed. |
---|---|
Returns: | A list [d1,d2,...dn] of divisors of num |
Returns the number of totatives of num
Parameters: | num – Integer for which number of totatives are needed. |
---|---|
Returns: | Number of totatives of num |
Note
A totative of an integer num is any integer i such that, 0 < i < n and GCD(i,num) == 1.
Uses Euler’s totient function.
Returns the prime factors pf i of num and the maximum power a i for each prime factor pf i.
Parameters: | num – An integer for which prime factors are needed |
---|---|
Returns: | A list of tuples [(pf1,a1),...(pfi,ai)] |
Note
num = (pf1**a1)*(pf2**a2)..*(pfi**ai)
Returns the prime factors pf i of num.
Parameters: | num – An integer for which prime factors are needed |
---|---|
Returns: | A list [pf1,pf2,...pfi] of prime factors of num |
Calculates the divisor functions (sigma functions).
Parameters: | num – Integer for which sigma functions are needed. |
---|---|
Returns: | A tuple (sigma0,sigma1,s(n)) |
Note
A generator function that yields the Fibonacci sequence.
Parameters: | start – Starting digit of Fibonacci sequence (0 or 1, default=1) |
---|
Get first n numbers in the Fibonacci sequence.
Parameters: |
|
---|---|
Returns: | A list [f1,f2,...,fn] where f1 = 1 if start = 1 |
Get the Fibonacci sequence [f1,f2,...fi] such that fi < n.
Parameters: |
|
---|---|
Returns: | A list [f1,f2,...,fi] such that fi < n |
Get the Fibonacci sequence [f1,f2,...fi] such that fi is the first Fibonacci number to have num digits.
Parameters: |
|
---|---|
Returns: | A list [f1,f2,..fi] such that fi is the first Fibonacci number to have num digits |
A generator function that yields primitive Pythagorean triples.
Returns n primitive Pythagorean triples.
Parameters: | n – Maximum number of primitive triples desired. |
---|---|
Returns: | A list of tuples [(a1,b1,c1),(a2,b2,c2),...,(an,bn,cn)] |
Converts num from decimal to base b
Parameters: |
|
---|---|
Returns: | String that represents decimal num in base b |
For example:
>>> decimal_to_base(8,2)
'1000'
Returns True if decimal number num is a Palindrome number in base.
Parameters: |
|
---|---|
Returns: | True if num is palindromic in base |
For example:
>>> is_palidrome(3875783) #Test in default base = 10
>>> True
>>> is_palidrome(9,2) # 9 in base 2 is 1001
>>> True
Returns True if num is Pandigital.
Parameters: |
|
---|
For example:
>>> is_pandigital(2134, 1, 4)
True
Returns a list of digits in num (most significant digit first).
Returns the sum of numerical value of each letter in the word based on its position in the alphabet.
Return the written English language string representation of num
Parameters: | num – An integer number |
---|
For example:
>>> write_num(132)
>>> 'one hundred and thirty-two'
Function to collapse two lists into a single list based on comparison and path functions.
Parameters: |
|
---|---|
Returns: | A list with length = len(list2) |
Note
For example: To calculate maximum sum of path values - path function => sum, comparison function => max:
>>> list1 = [12,37,53,46]
>>> list2 = [23,34,47]
>>> compf = max
>>> pathf = sum
>>> collapse_lists(list1,list2,compf,pathf)
>>> [60, 87, 100]