Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
I need to know the efficient way to find all the factors of a number in python 2.7?

Can anyone please help me with my doubt?

1 Answer

0 votes
by (26.4k points)

Try the following code, which will actually returns all the factors, efficiently and very quickly of a number "n".

from functools import reduce

def factors(n):    

    return set(reduce(list.__add__, 

                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

Looking for a good python tutorial page? Come and join the python certification course and get certified.
Do check out the below video tutorial for more information...

Browse Categories

...