basic maths Algorithm

A basic math algorithm is a step-by-step procedure used to perform calculations that range from simple arithmetic operations, such as addition, subtraction, multiplication, and division, to more complex procedures like solving equations or finding the greatest common divisor. These algorithms provide a systematic approach to solving a wide variety of mathematical problems, ensuring accuracy and efficiency in calculations. They are the building blocks of mathematics, and understanding them is essential for developing a strong foundation in the subject. There are several well-known basic math algorithms that are taught in primary education, including the standard algorithms for addition, subtraction, multiplication, and division. For example, the standard addition algorithm involves aligning numbers vertically by place value and adding the digits in each column, starting from the rightmost column (ones place) and moving to the left. If a sum in a column is greater than nine, the excess is carried over to the next column on the left. This process is repeated for each column until the final sum is obtained. Similarly, the standard algorithms for subtraction, multiplication, and division follow structured procedures to obtain the result. As students progress in their mathematical education, they are introduced to more advanced algorithms and techniques, which enable them to solve increasingly complex problems.
"""Implementation of Basic Math in Python."""
import math


def prime_factors(n: int) -> list:
    """Find Prime Factors.
    >>> prime_factors(100)
    [2, 2, 5, 5]
    """
    pf = []
    while n % 2 == 0:
        pf.append(2)
        n = int(n / 2)
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        while n % i == 0:
            pf.append(i)
            n = int(n / i)
    if n > 2:
        pf.append(n)
    return pf


def number_of_divisors(n: int) -> int:
    """Calculate Number of Divisors of an Integer.
    >>> number_of_divisors(100)
    9
    """
    div = 1
    temp = 1
    while n % 2 == 0:
        temp += 1
        n = int(n / 2)
    div *= temp
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        temp = 1
        while n % i == 0:
            temp += 1
            n = int(n / i)
        div *= temp
    return div


def sum_of_divisors(n: int) -> int:
    """Calculate Sum of Divisors.
    >>> sum_of_divisors(100)
    217
    """
    s = 1
    temp = 1
    while n % 2 == 0:
        temp += 1
        n = int(n / 2)
    if temp > 1:
        s *= (2 ** temp - 1) / (2 - 1)
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        temp = 1
        while n % i == 0:
            temp += 1
            n = int(n / i)
        if temp > 1:
            s *= (i ** temp - 1) / (i - 1)
    return int(s)


def euler_phi(n: int) -> int:
    """Calculate Euler's Phi Function.
    >>> euler_phi(100)
    40
    """
    s = n
    for x in set(prime_factors(n)):
        s *= (x - 1) / x
    return int(s)


if __name__ == "__main__":
    print(prime_factors(100))
    print(number_of_divisors(100))
    print(sum_of_divisors(100))
    print(euler_phi(100))

LANGUAGE:

DARK MODE: