def descompune_in_factori_primi(n):
factori = []
divisor = 2
while n > 1:
count = 0
while n % divisor == 0:
n //= divisor
count += 1
if count > 0:
factori.append((divisor, count))
divisor += 1
# Optimizare: verificăm doar până la √n
if divisor * divisor > n:
if n > 1:
factori.append((n, 1))
break
return factori
def main():
try:
numar = int(input("Introduceti un numar: "))
except ValueError:
print("Va rugam sa introduceti un numar valid!")
return
if numar <= 1:
print("Numarul trebuie sa fie mai mare decat 1!")
return
factori = descompune_in_factori_primi(numar)
print("Descompunerea in factori primi este: ", end="")
for i, (base, exponent) in enumerate(factori):
print(f"{base}^{exponent}", end="")
if i < len(factori) - 1:
print(" * ", end="")
print()
if __name__ == "__main__":
main()
ZGVmIGRlc2NvbXB1bmVfaW5fZmFjdG9yaV9wcmltaShuKToKICAgIGZhY3RvcmkgPSBbXQogICAgZGl2aXNvciA9IDIKCiAgICB3aGlsZSBuID4gMToKICAgICAgICBjb3VudCA9IDAKICAgICAgICB3aGlsZSBuICUgZGl2aXNvciA9PSAwOgogICAgICAgICAgICBuIC8vPSBkaXZpc29yCiAgICAgICAgICAgIGNvdW50ICs9IDEKICAgICAgICBpZiBjb3VudCA+IDA6CiAgICAgICAgICAgIGZhY3RvcmkuYXBwZW5kKChkaXZpc29yLCBjb3VudCkpCiAgICAgICAgZGl2aXNvciArPSAxCiAgICAgICAgIyBPcHRpbWl6YXJlOiB2ZXJpZmljxINtIGRvYXIgcMOibsSDIGxhIOKImm4KICAgICAgICBpZiBkaXZpc29yICogZGl2aXNvciA+IG46CiAgICAgICAgICAgIGlmIG4gPiAxOgogICAgICAgICAgICAgICAgZmFjdG9yaS5hcHBlbmQoKG4sIDEpKQogICAgICAgICAgICBicmVhawoKICAgIHJldHVybiBmYWN0b3JpCgpkZWYgbWFpbigpOgogICAgdHJ5OgogICAgICAgIG51bWFyID0gaW50KGlucHV0KCJJbnRyb2R1Y2V0aSB1biBudW1hcjogIikpCiAgICBleGNlcHQgVmFsdWVFcnJvcjoKICAgICAgICBwcmludCgiVmEgcnVnYW0gc2EgaW50cm9kdWNldGkgdW4gbnVtYXIgdmFsaWQhIikKICAgICAgICByZXR1cm4KCiAgICBpZiBudW1hciA8PSAxOgogICAgICAgIHByaW50KCJOdW1hcnVsIHRyZWJ1aWUgc2EgZmllIG1haSBtYXJlIGRlY2F0IDEhIikKICAgICAgICByZXR1cm4KCiAgICBmYWN0b3JpID0gZGVzY29tcHVuZV9pbl9mYWN0b3JpX3ByaW1pKG51bWFyKQoKICAgIHByaW50KCJEZXNjb21wdW5lcmVhIGluIGZhY3RvcmkgcHJpbWkgZXN0ZTogIiwgZW5kPSIiKQogICAgZm9yIGksIChiYXNlLCBleHBvbmVudCkgaW4gZW51bWVyYXRlKGZhY3RvcmkpOgogICAgICAgIHByaW50KGYie2Jhc2V9XntleHBvbmVudH0iLCBlbmQ9IiIpCiAgICAgICAgaWYgaSA8IGxlbihmYWN0b3JpKSAtIDE6CiAgICAgICAgICAgIHByaW50KCIgKiAiLCBlbmQ9IiIpCiAgICBwcmludCgpCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigp