0
How can i count the divisors
5 Respostas
+ 6
what is the aim ouf counting the divisors? if you take 12 as the dividend and divide it by each value supplied by the range (= divisor), the number of divisors is also 12. But as you are using a modulo division to detect even numbers, i suppose you want to get the factors of the input numbers. factors of 12 are 1,2,3,4,6,12.
+ 4
Here are 2 code samples.
(1) using a for loop
(2) using a comprehension
- for both vesions, we check if it is a factor of the number by using modulo division.
- if modulo dividion gives 0, number is a factor and will be stored in a list.
- after loop is finished, wie count the number of elements in the result list by using len()
num = int(input("Enter the number : "))
res = []
for i in range(1, num + 1):
if num % i == 0:
res.append(i)
print(f'number of factors is: {len(res)}')
#print(f'factors of {num} are: {res}') # optional
# getting number of factors with a comprehension: replaces line 2 - 6
num = int(input("Enter the number : "))
print(len([i for i in range(1, num + 1) if num % i == 0]))
+ 1
remove the else statement and it should work
+ 1
num=int(input("Your number: "))
print(num)
print("divisors: ",end="")
result=0
count=0
for i in range(2,num):
if i==num:
print(i,end=" ")
elif num%i==0:
print(i,end=", ")
0
I like to count the number.just example 6 has 4 divisors....1,2,3,6....how can i calculate that 4