trying to find how many divisors in 120, and printing divisors
div_n = 1
count = 0
n = 120
while div_n <= n:
if n % div_n == 0:
print(div_n)
count += 1
div_n += 1
print('number of divisor that 120 is {}.'.format(count))
the output should be
1
2
3
4
5
6
8
10
12
15
20
24
30
40
60
120
number of divisor that 120 is 16
But the problem I'm trying to solve says the code is not meeting the requirements. If there is nothing wrong, or more information is needed let me know.
You can use this:
def get_factors(n):
ret = []
for i in range(1, n+1):
if not n % i:
ret.append(i)
return ret
factors = get_factors(120)
print(*factors, sep='\n')
print('Total factors:', len(factors))
Output:
1
2
3
4
5
6
8
10
12
15
20
24
30
40
60
120
Total factors: 16