Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
1.0 kB
5
Indexable
Never
from math import sqrt

def prime(num, i=2):
    if num <= 1:
        return False
    elif num % i == 0:
        return False
    elif i > sqrt(num):
        return True
    else:
        return prime(num, i + 1)

def factorizationtree(num, divisor=2, indentation=0):
    if prime(num):
        print(num) # Prints the inputted number if the inputted number is prime
        return
    if num <= 1:
        return
    if num % divisor == 0:
        print(f"{' ' * indentation}|-{divisor}")
        print(f"{' ' * indentation}|-{num // divisor}")
        if prime(num // divisor) and num % divisor == 0: # Prevents the occurrence of 1
            return
        factorizationtree(num // divisor, divisor, indentation + 2) # Increases the space
    else:
        factorizationtree(num, divisor + 1, indentation)

def evaluate(array, function):
    element = array.pop(0)
    function(element)
    if array:
        return evaluate(array, function)

evaluate([18, 900, 7], factorizationtree)
Leave a Comment