Nov-01-2018, 03:21 PM
Hi everyone!
I'm trying to make the code of a program that, given a certain number N, prints the number in between 1 and N with the greatest amount of factors.
For instance, the program should look similar to this:
Type a number: 10
The number with most factors is 10 (4)
I made this so far:
Type a number: 10
Number 1 has 1 factors
Number 2 has 2 factors
Number 3 has 2 factors
Number 4 has 3 factors
Number 5 has 2 factors
Number 6 has 4 factors
Number 7 has 2 factors
Number 8 has 4 factors
Number 9 has 3 factors
Number 10 has 4 factors
I added a few lines more than necessary so that I can check how many factors does every number have.
Therefore, I need to get the program to display that the number with most factors is 10 (Number 6 also has 4 factors but provided that number 10 is greater, the program should display number 10 as the one with most factors). However, I'm not sure where to create a variable to store the maximum factor.
Note: The factor_counter function can only return the number of factors, nothing else (requirement).
Thanks so much in advance and have a great day
I'm trying to make the code of a program that, given a certain number N, prints the number in between 1 and N with the greatest amount of factors.
For instance, the program should look similar to this:
Type a number: 10
The number with most factors is 10 (4)
I made this so far:
def factor_counter(n): counter = 0 for factor in range(1, n + 1): if n % factor== 0: counter += 1 return counter #Main program: n = int(input('Type a number: ')) for z in range (1, n+1): print ('Number {0} has {1} factors'.format(z,factor_counter(z)))This code above outputs the following lines:
Type a number: 10
Number 1 has 1 factors
Number 2 has 2 factors
Number 3 has 2 factors
Number 4 has 3 factors
Number 5 has 2 factors
Number 6 has 4 factors
Number 7 has 2 factors
Number 8 has 4 factors
Number 9 has 3 factors
Number 10 has 4 factors
I added a few lines more than necessary so that I can check how many factors does every number have.
Therefore, I need to get the program to display that the number with most factors is 10 (Number 6 also has 4 factors but provided that number 10 is greater, the program should display number 10 as the one with most factors). However, I'm not sure where to create a variable to store the maximum factor.
Note: The factor_counter function can only return the number of factors, nothing else (requirement).
Thanks so much in advance and have a great day