Home python Explain the algorithm for finding prime divisors of

Explain the algorithm for finding prime divisors of

Author

Date

Category

Can you explain the algorithm of this code. This code finds prime divisors of a number.

def simpleDividers (n):
  answer = []
  d = 2
  while d * d & lt; = n:
    if n% d == 0:
      answer.append (d)
      n // = d
    else:
      d + = 1
  if n & gt; 1:
    answer.append (n)
  return answer

Answer 1, authority 100%

def simpleDividers (n):
  answer = [] # empty list
  d = 2 # initial value of the checked divisor
  while d * d & lt; = n: # check divisors to the root of n
    if n% d == 0: # if modulo = 0
      answer.append (d) # then add to the answer
      n // = d # divide n by d
    else:
      d + = 1 # check the next divisor
  if n & gt; 1: # if eventually n & gt; 1
    answer.append (n) # add this n to the answer
  return answer # return

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions