Created
August 1, 2015 15:40
-
-
Save viveksyngh/2b850df23790497571b9 to your computer and use it in GitHub Desktop.
Finds all the factors of an integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
#This function returns all the factors of an integer in sorted order | |
def allFactors(A): | |
""" | |
:param: A positive integer | |
:return: All the factors of the integer in sorted order. | |
""" | |
res = [] | |
firstHalf = [] | |
secondHalf = [] | |
for i in range(1, int(A ** 0.5) + 1) : | |
if A%i == 0 : | |
firstHalf.append(i) | |
if i != A ** 0.5 : | |
secondHalf.insert(0, A/i) | |
firstHalf.extend(secondHalf) | |
return firstHalf | |
print(allFactors(36)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment