What can be optimized in this program, which finds all prime divisors of a natural number N ?

 import math def pr(r,n): if n % r == 0: m = int(math.sqrt(r)) g = 2 while g <= m: if r % g == 0: return g += 1 print(r, end=' ') return else: return n = int(input()) pr(2, n) for i in range(3, n + 1, 2): pr(i, n) 
  • The code must be presented in the question itself. - Regent
  • And what should your code do ??? - Alex.B
  • 2
    I am afraid that the answer is everything. Here the algorithm itself is not at all optimal. - pavel
  • 2
    What is the purpose of optimization? Execution speed? The beauty and / or brevity of the code? Something else? - MaxU
  • 3
    If you are interested in speed, as well as modern algorithms for finding prime numbers (from the sieve of Eratosthenes to the Atkin algorithm and Wheel Factorization ), I highly recommend watching this chic answer in the English version of SO - MaxU

1 answer 1

 def pr(r, n=2): while n <= r: if r % n: n += 1 else: r //= n yield n print(set(pr(110)))