A
A
alex_6432020-06-10 13:17:51
Python
alex_643, 2020-06-10 13:17:51

Finding the divisors of a number?

Hello! There is a factorization algorithm (I heard that you can find divisors of a number through it):

def Factor(n):
    Ans = []
    d = 2
    while d * d <= n:
        if n % d == 0:
            Ans.append(d)
            n //= d
        else:
            d += 1
    if n > 1:
        Ans.append(n)
    return Ans

In short, we need an efficient algorithm for finding the number of divisors of a number. tell me how to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick, 2020-06-11
@alex_643

Hello! There are ideas for such an algorithm, you can go to the root of the number and check whether the number is divisible, if it is divisible, then immediately add 2 (because if 24 is a multiple of 4, then it is a multiple of 24/4), you just need to check if the divisor * divisor is not a number.
Here is the
while implementation

def fact(a):
  i = 1
  o = 0
  while i * i <= a:
     if a % i == 0 and i * i != a: o += 2
     elif a % i == 0 and i * i == a: o += 1
     i += 1
  return o

B
baknaryn, 2020-11-19
@baknaryn

n=int(input("n="))
flag=True
m=[]
i=2

while flag:
     if n%i==0:
            m.append(i)
            m.append(n//i)
     
     if n//i==i+1:
             flag=False
     i+=1
print(m)
If we consider the number 58, then we immediately find two divisors 2 and 28, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question