fork download
  1. def descompune_in_factori_primi(n):
  2. factori = []
  3. divisor = 2
  4.  
  5. while n > 1:
  6. count = 0
  7. while n % divisor == 0:
  8. n //= divisor
  9. count += 1
  10. if count > 0:
  11. factori.append((divisor, count))
  12. divisor += 1
  13. # Optimizare: verificăm doar până la √n
  14. if divisor * divisor > n:
  15. if n > 1:
  16. factori.append((n, 1))
  17. break
  18.  
  19. return factori
  20.  
  21. def main():
  22. try:
  23. numar = int(input("Introduceti un numar: "))
  24. except ValueError:
  25. print("Va rugam sa introduceti un numar valid!")
  26. return
  27.  
  28. if numar <= 1:
  29. print("Numarul trebuie sa fie mai mare decat 1!")
  30. return
  31.  
  32. factori = descompune_in_factori_primi(numar)
  33.  
  34. print("Descompunerea in factori primi este: ", end="")
  35. for i, (base, exponent) in enumerate(factori):
  36. print(f"{base}^{exponent}", end="")
  37. if i < len(factori) - 1:
  38. print(" * ", end="")
  39. print()
  40.  
  41. if __name__ == "__main__":
  42. main()
Success #stdin #stdout 0.03s 9856KB
stdin
100
stdout
Introduceti un numar: Descompunerea in factori primi este: 2^2 * 5^2