2017-11-18 15:29:34 +00:00
|
|
|
"""
|
|
|
|
Created on Thu Oct 5 16:44:23 2017
|
|
|
|
|
|
|
|
@author: Christian Bender
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
This Python library contains some useful functions to deal with
|
2017-11-18 15:29:34 +00:00
|
|
|
prime numbers and whole numbers.
|
|
|
|
|
|
|
|
Overview:
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
is_prime(number)
|
|
|
|
sieve_er(N)
|
|
|
|
get_prime_numbers(N)
|
|
|
|
prime_factorization(number)
|
|
|
|
greatest_prime_factor(number)
|
|
|
|
smallest_prime_factor(number)
|
|
|
|
get_prime(n)
|
|
|
|
get_primes_between(pNumber1, pNumber2)
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
----
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
is_even(number)
|
|
|
|
is_odd(number)
|
2017-11-18 15:29:34 +00:00
|
|
|
gcd(number1, number2) // greatest common divisor
|
2022-10-12 22:54:20 +00:00
|
|
|
kg_v(number1, number2) // least common multiple
|
|
|
|
get_divisors(number) // all divisors of 'number' inclusive 1, number
|
|
|
|
is_perfect_number(number)
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
NEW-FUNCTIONS
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
simplify_fraction(numerator, denominator)
|
2017-11-18 15:29:34 +00:00
|
|
|
factorial (n) // n!
|
|
|
|
fib (n) // calculate the n-th fibonacci term.
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
|
|
|
goldbach(number) // Goldbach's assumption
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-07-25 07:49:00 +00:00
|
|
|
from math import sqrt
|
|
|
|
|
|
|
|
|
2022-07-11 14:36:57 +00:00
|
|
|
def is_prime(number: int) -> bool:
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'number'
|
|
|
|
returns true if 'number' is prime otherwise false.
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number, int) and (
|
|
|
|
number >= 0
|
|
|
|
), "'number' must been an int and positive"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
status = True
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
# 0 and 1 are none primes.
|
2017-11-18 15:29:34 +00:00
|
|
|
if number <= 1:
|
|
|
|
status = False
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for divisor in range(2, int(round(sqrt(number))) + 1):
|
2017-11-18 15:29:34 +00:00
|
|
|
# if 'number' divisible by 'divisor' then sets 'status'
|
2019-07-25 07:49:00 +00:00
|
|
|
# of false and break up the loop.
|
2017-11-18 15:29:34 +00:00
|
|
|
if number % divisor == 0:
|
|
|
|
status = False
|
|
|
|
break
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(status, bool), "'status' must been from type bool"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return status
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ------------------------------------------
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def sieve_er(n):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'N' > 2
|
|
|
|
returns a list of prime numbers from 2 up to N.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2020-09-10 08:31:26 +00:00
|
|
|
This function implements the algorithm called
|
|
|
|
sieve of erathostenes.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2022-10-12 22:54:20 +00:00
|
|
|
assert isinstance(n, int) and (n > 2), "'N' must been an int and > 2"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
# beginList: contains all natural numbers from 2 up to N
|
2022-10-15 17:29:42 +00:00
|
|
|
begin_list = list(range(2, n + 1))
|
2017-11-18 15:29:34 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = [] # this list will be returns.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# actual sieve of erathostenes
|
2022-10-12 22:54:20 +00:00
|
|
|
for i in range(len(begin_list)):
|
|
|
|
for j in range(i + 1, len(begin_list)):
|
|
|
|
if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0):
|
|
|
|
begin_list[j] = 0
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
# filters actual prime numbers.
|
2022-10-12 22:54:20 +00:00
|
|
|
ans = [x for x in begin_list if x != 0]
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, list), "'ans' must been from type list"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# --------------------------------
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def get_prime_numbers(n):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'N' > 2
|
|
|
|
returns a list of prime numbers from 2 up to N (inclusive)
|
|
|
|
This function is more efficient as function 'sieveEr(...)'
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2022-10-12 22:54:20 +00:00
|
|
|
assert isinstance(n, int) and (n > 2), "'N' must been an int and > 2"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
ans = []
|
|
|
|
|
|
|
|
# iterates over all numbers between 2 up to N+1
|
2017-11-18 15:29:34 +00:00
|
|
|
# if a number is prime then appends to list 'ans'
|
2022-10-12 22:54:20 +00:00
|
|
|
for number in range(2, n + 1):
|
2022-07-11 14:36:57 +00:00
|
|
|
if is_prime(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans.append(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, list), "'ans' must been from type list"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def prime_factorization(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'number'
|
|
|
|
returns a list of the prime number factors of 'number'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number, int) and number >= 0, "'number' must been an int and >= 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = [] # this list will be returns of the function.
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# potential prime number factors.
|
|
|
|
|
2019-07-25 07:49:00 +00:00
|
|
|
factor = 2
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
quotient = number
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
if number == 0 or number == 1:
|
|
|
|
ans.append(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
# if 'number' not prime then builds the prime factorization of 'number'
|
2022-07-11 14:36:57 +00:00
|
|
|
elif not is_prime(number):
|
2019-10-05 05:14:13 +00:00
|
|
|
while quotient != 1:
|
2022-07-11 14:36:57 +00:00
|
|
|
if is_prime(factor) and (quotient % factor == 0):
|
2019-10-05 05:14:13 +00:00
|
|
|
ans.append(factor)
|
|
|
|
quotient /= factor
|
2017-11-18 15:29:34 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
factor += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
else:
|
|
|
|
ans.append(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, list), "'ans' must been from type list"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# -----------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def greatest_prime_factor(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'number' >= 0
|
|
|
|
returns the greatest prime number factor of 'number'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number, int) and (
|
|
|
|
number >= 0
|
|
|
|
), "'number' bust been an int and >= 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
ans = 0
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# prime factorization of 'number'
|
2022-10-12 22:54:20 +00:00
|
|
|
prime_factors = prime_factorization(number)
|
2017-11-18 15:29:34 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
ans = max(prime_factors)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, int), "'ans' must been from type int"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# ----------------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def smallest_prime_factor(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: integer 'number' >= 0
|
|
|
|
returns the smallest prime number factor of 'number'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number, int) and (
|
|
|
|
number >= 0
|
|
|
|
), "'number' bust been an int and >= 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
ans = 0
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# prime factorization of 'number'
|
2022-10-12 22:54:20 +00:00
|
|
|
prime_factors = prime_factorization(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
ans = min(prime_factors)
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, int), "'ans' must been from type int"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ----------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def is_even(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: integer 'number'
|
|
|
|
returns true if 'number' is even, otherwise false.
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# precondition
|
2019-07-25 07:49:00 +00:00
|
|
|
assert isinstance(number, int), "'number' must been an int"
|
2017-11-18 15:29:34 +00:00
|
|
|
assert isinstance(number % 2 == 0, bool), "compare bust been from type bool"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return number % 2 == 0
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def is_odd(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: integer 'number'
|
|
|
|
returns true if 'number' is odd, otherwise false.
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# precondition
|
2019-07-25 07:49:00 +00:00
|
|
|
assert isinstance(number, int), "'number' must been an int"
|
2017-11-18 15:29:34 +00:00
|
|
|
assert isinstance(number % 2 != 0, bool), "compare bust been from type bool"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return number % 2 != 0
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
def goldbach(number):
|
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Goldbach's assumption
|
|
|
|
input: a even positive integer 'number' > 2
|
|
|
|
returns a list of two prime numbers whose sum is equal to 'number'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
2022-10-12 22:54:20 +00:00
|
|
|
isinstance(number, int) and (number > 2) and is_even(number)
|
2019-10-05 05:14:13 +00:00
|
|
|
), "'number' must been an int, even and > 2"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = [] # this list will returned
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# creates a list of prime numbers between 2 up to 'number'
|
2022-10-12 22:54:20 +00:00
|
|
|
prime_numbers = get_prime_numbers(number)
|
|
|
|
len_pn = len(prime_numbers)
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# run variable for while-loops.
|
|
|
|
i = 0
|
2019-06-30 04:41:26 +00:00
|
|
|
j = None
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# exit variable. for break up the loops
|
|
|
|
loop = True
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
while i < len_pn and loop:
|
2019-10-05 05:14:13 +00:00
|
|
|
j = i + 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
while j < len_pn and loop:
|
|
|
|
if prime_numbers[i] + prime_numbers[j] == number:
|
2017-11-18 15:29:34 +00:00
|
|
|
loop = False
|
2022-10-12 22:54:20 +00:00
|
|
|
ans.append(prime_numbers[i])
|
|
|
|
ans.append(prime_numbers[j])
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2018-10-17 21:28:57 +00:00
|
|
|
j += 1
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
i += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
|
|
|
isinstance(ans, list)
|
|
|
|
and (len(ans) == 2)
|
|
|
|
and (ans[0] + ans[1] == number)
|
2022-07-11 14:36:57 +00:00
|
|
|
and is_prime(ans[0])
|
|
|
|
and is_prime(ans[1])
|
2019-10-05 05:14:13 +00:00
|
|
|
), "'ans' must contains two primes. And sum of elements must been eq 'number'"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ----------------------------------------------
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
def gcd(number1, number2):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Greatest common divisor
|
|
|
|
input: two positive integer 'number1' and 'number2'
|
|
|
|
returns the greatest common divisor of 'number1' and 'number2'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
|
|
|
isinstance(number1, int)
|
|
|
|
and isinstance(number2, int)
|
|
|
|
and (number1 >= 0)
|
|
|
|
and (number2 >= 0)
|
|
|
|
), "'number1' and 'number2' must been positive integer."
|
2017-11-18 15:29:34 +00:00
|
|
|
|
2019-07-25 07:49:00 +00:00
|
|
|
rest = 0
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
while number2 != 0:
|
|
|
|
rest = number1 % number2
|
|
|
|
number1 = number2
|
|
|
|
number2 = rest
|
|
|
|
|
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number1, int) and (
|
|
|
|
number1 >= 0
|
|
|
|
), "'number' must been from type int and positive"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return number1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ----------------------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def kg_v(number1, number2):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Least common multiple
|
|
|
|
input: two positive integer 'number1' and 'number2'
|
|
|
|
returns the least common multiple of 'number1' and 'number2'
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
|
|
|
isinstance(number1, int)
|
|
|
|
and isinstance(number2, int)
|
|
|
|
and (number1 >= 1)
|
|
|
|
and (number2 >= 1)
|
|
|
|
), "'number1' and 'number2' must been positive integer."
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = 1 # actual answer that will be return.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# for kgV (x,1)
|
|
|
|
if number1 > 1 and number2 > 1:
|
|
|
|
# builds the prime factorization of 'number1' and 'number2'
|
2022-10-12 22:54:20 +00:00
|
|
|
prime_fac_1 = prime_factorization(number1)
|
|
|
|
prime_fac_2 = prime_factorization(number2)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
elif number1 == 1 or number2 == 1:
|
2022-10-12 22:54:20 +00:00
|
|
|
prime_fac_1 = []
|
|
|
|
prime_fac_2 = []
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = max(number1, number2)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
count1 = 0
|
|
|
|
count2 = 0
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
done = [] # captured numbers int both 'primeFac1' and 'primeFac2'
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# iterates through primeFac1
|
2022-10-12 22:54:20 +00:00
|
|
|
for n in prime_fac_1:
|
2017-11-18 15:29:34 +00:00
|
|
|
if n not in done:
|
2022-10-12 22:54:20 +00:00
|
|
|
if n in prime_fac_2:
|
|
|
|
count1 = prime_fac_1.count(n)
|
|
|
|
count2 = prime_fac_2.count(n)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(max(count1, count2)):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans *= n
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
else:
|
2022-10-12 22:54:20 +00:00
|
|
|
count1 = prime_fac_1.count(n)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(count1):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans *= n
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
done.append(n)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# iterates through primeFac2
|
2022-10-12 22:54:20 +00:00
|
|
|
for n in prime_fac_2:
|
2017-11-18 15:29:34 +00:00
|
|
|
if n not in done:
|
2022-10-12 22:54:20 +00:00
|
|
|
count2 = prime_fac_2.count(n)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(count2):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans *= n
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
done.append(n)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(ans, int) and (
|
|
|
|
ans >= 0
|
|
|
|
), "'ans' must been from type int and positive"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ----------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def get_prime(n):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Gets the n-th prime number.
|
|
|
|
input: positive integer 'n' >= 0
|
|
|
|
returns the n-th prime number, beginning at index 0
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(n, int) and (n >= 0), "'number' must been a positive int"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
index = 0
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = 2 # this variable holds the answer
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
while index < n:
|
|
|
|
index += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans += 1 # counts to the next number
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# if ans not prime then
|
2019-07-25 07:49:00 +00:00
|
|
|
# runs to the next prime number.
|
2022-07-11 14:36:57 +00:00
|
|
|
while not is_prime(ans):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2022-07-11 14:36:57 +00:00
|
|
|
assert isinstance(ans, int) and is_prime(
|
2019-10-05 05:14:13 +00:00
|
|
|
ans
|
|
|
|
), "'ans' must been a prime number and from type int"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ---------------------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def get_primes_between(p_number_1, p_number_2):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: prime numbers 'pNumber1' and 'pNumber2'
|
|
|
|
pNumber1 < pNumber2
|
|
|
|
returns a list of all prime numbers between 'pNumber1' (exclusive)
|
|
|
|
and 'pNumber2' (exclusive)
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
2022-10-12 22:54:20 +00:00
|
|
|
is_prime(p_number_1) and is_prime(p_number_2) and (p_number_1 < p_number_2)
|
2019-10-05 05:14:13 +00:00
|
|
|
), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
number = p_number_1 + 1 # jump to the next number
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = [] # this list will be returns.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# if number is not prime then
|
2019-07-25 07:49:00 +00:00
|
|
|
# fetch the next prime number.
|
2022-07-11 14:36:57 +00:00
|
|
|
while not is_prime(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
number += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
while number < p_number_2:
|
2017-11-18 15:29:34 +00:00
|
|
|
ans.append(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
number += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
|
|
|
# fetch the next prime number.
|
2022-07-11 14:36:57 +00:00
|
|
|
while not is_prime(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
number += 1
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
2022-10-12 22:54:20 +00:00
|
|
|
isinstance(ans, list)
|
|
|
|
and ans[0] != p_number_1
|
|
|
|
and ans[len(ans) - 1] != p_number_2
|
2019-10-05 05:14:13 +00:00
|
|
|
), "'ans' must been a list without the arguments"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# 'ans' contains not 'pNumber1' and 'pNumber2' !
|
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ----------------------------------------------------
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def get_divisors(n):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'n' >= 1
|
|
|
|
returns all divisors of n (inclusive 1 and 'n')
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(n, int) and (n >= 1), "'n' must been int and >= 1"
|
2017-11-18 15:29:34 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = [] # will be returned.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for divisor in range(1, n + 1):
|
2017-11-18 15:29:34 +00:00
|
|
|
if n % divisor == 0:
|
|
|
|
ans.append(divisor)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# precondition
|
|
|
|
assert ans[0] == 1 and ans[len(ans) - 1] == n, "Error in function getDivisiors(...)"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------
|
|
|
|
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def is_perfect_number(number):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'number' > 1
|
|
|
|
returns true if 'number' is a perfect number otherwise false.
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(number, int) and (
|
|
|
|
number > 1
|
|
|
|
), "'number' must been an int and >= 1"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
divisors = get_divisors(number)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
|
|
|
isinstance(divisors, list)
|
|
|
|
and (divisors[0] == 1)
|
|
|
|
and (divisors[len(divisors) - 1] == number)
|
|
|
|
), "Error in help-function getDivisiors(...)"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# summed all divisors up to 'number' (exclusive), hence [:-1]
|
|
|
|
return sum(divisors[:-1]) == number
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# ------------------------------------------------------------
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def simplify_fraction(numerator, denominator):
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: two integer 'numerator' and 'denominator'
|
|
|
|
assumes: 'denominator' != 0
|
|
|
|
returns: a tuple with simplify numerator and denominator.
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
|
|
|
isinstance(numerator, int)
|
|
|
|
and isinstance(denominator, int)
|
|
|
|
and (denominator != 0)
|
|
|
|
), "The arguments must been from type int and 'denominator' != 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# build the greatest common divisor of numerator and denominator.
|
2022-10-12 22:54:20 +00:00
|
|
|
gcd_of_fraction = gcd(abs(numerator), abs(denominator))
|
2017-11-18 15:29:34 +00:00
|
|
|
|
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert (
|
2022-10-12 22:54:20 +00:00
|
|
|
isinstance(gcd_of_fraction, int)
|
|
|
|
and (numerator % gcd_of_fraction == 0)
|
|
|
|
and (denominator % gcd_of_fraction == 0)
|
2019-10-05 05:14:13 +00:00
|
|
|
), "Error in function gcd(...,...)"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
return (numerator // gcd_of_fraction, denominator // gcd_of_fraction)
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# -----------------------------------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
def factorial(n):
|
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'n'
|
|
|
|
returns the factorial of 'n' (n!)
|
2017-11-18 15:29:34 +00:00
|
|
|
"""
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
2019-10-05 05:14:13 +00:00
|
|
|
assert isinstance(n, int) and (n >= 0), "'n' must been a int and >= 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = 1 # this will be return.
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for factor in range(1, n + 1):
|
2017-11-18 15:29:34 +00:00
|
|
|
ans *= factor
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# -------------------------------------------------------------------
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
def fib(n):
|
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
input: positive integer 'n'
|
|
|
|
returns the n-th fibonacci term , indexing by 0
|
2019-07-25 07:49:00 +00:00
|
|
|
"""
|
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
# precondition
|
|
|
|
assert isinstance(n, int) and (n >= 0), "'n' must been an int and >= 0"
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
tmp = 0
|
|
|
|
fib1 = 1
|
2019-10-05 05:14:13 +00:00
|
|
|
ans = 1 # this will be return
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(n - 1):
|
2017-11-18 15:29:34 +00:00
|
|
|
tmp = ans
|
|
|
|
ans += fib1
|
|
|
|
fib1 = tmp
|
2019-07-25 07:49:00 +00:00
|
|
|
|
2017-11-18 15:29:34 +00:00
|
|
|
return ans
|