All Factors Upto 10^12..

you are give a number upto range 10^12
How to find all possible factors of it ?
Ex-for 18 factors are -> 1,2,3,6,9,18

1 Like

In Python I would do it like this:

number = input('Your number: ')
results = []
for i in range(1,number+1) :
    if number % i == 0 :
        results.append(i)
print results

But I don’t think that my code will work up to 10^12

you just have to loop through sqrt(n) .

number= input()

result=[]

   for i in range(1,math.sqrt(number)+1):
        if number%i == 0 :
           if(number/i != i):
              result.append(i)
              result.append(number/i)
           else:
              result.append(i)
print result
1 Like

It would work till 10**7 in 1 sec… It wont work for over that…

Some simple optimizations : If number is odd then you can skip all the even numbers. You could also use some prime number test to avoid looping for prime numbers

//