202305741_me13.py

mail@pastecode.io avatar
unknown
python
2 months ago
1.2 kB
2
Indexable
Never
# To involve square roots
import math

# Global variables for spacing out branches
space_count = 0
space_indic = "  "


# Factorization
def comp_tree(p, q):
    global space_count
    global space_indic
    if p == 1:
        print(1)
        return comp_tree(p, q + 1)
    elif p == q:
        if space_count == 0:
            print(p)
            pass
        else:
            pass
    elif p % q == 0:
        r = p // q
        print(str(space_indic * space_count) + "|-" + str(q))
        print(str(space_indic * space_count) + "|-" + str(r))
        space_count += 1
        if r % q == 0:
            return comp_tree(r, q)
        elif r % q != 0:
            return comp_tree(r, q + 1)
        else:
            return comp_tree(r, q + 1)
    elif p % q != 0:
        if q > math.sqrt(p):
            return comp_tree(p, p)
        else:
            return comp_tree(p, q + 1)


# While Loop
def while_loop(w):
    if w < 1:
        pass
    else:
        global q
        global space_count
        q = 2
        space_count = 0
        comp_tree(int(input()), 2)
        w -= 1
        return while_loop(w)


num_c = while_loop(int(input()))
Leave a Comment