Friday, July 1, 2016

Practice Recognizing Squares and Cubes, Prime Factorization, Number of Factors

Number of Factors, Brute Force
In [1]:
from sympy import factorint
In [2]:
# When using the python console (iPad), uncomment the following lines to get user input
# s=raw_input("? ")
# n=int(s)
# n hardcoded for this notebook to 324 = 18*18
n=18*18

print("factorint output ->", factorint(n))
nhalf = int(n/2)

factorcount=0
for x in range(1,nhalf+1):
    if(n%x == 0):
        print(x)
        factorcount += 1
        
#plus one more, since n is a factor of n
print(n)
factorcount += 1

print(factorcount, " factors")
factorint output -> {2: 2, 3: 4}
1
2
3
4
6
9
12
18
27
36
54
81
108
162
324
15  factors