Python Forum
Program that displays the number with the greatest amount of factors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program that displays the number with the greatest amount of factors
#1
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:

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
Reply
#2
Hello! You could simply add a variable which stores the current number with most factors.
On next iteration, if new number has same or more factors than previous "most factors" one, assign new number to the "most factors" variable. That way you don't even need to worry about the highest number, if there are more with same (highest) number of factors.
It will take some rearranging of your code, but you pretty much have all you need already coded.
Reply
#3
Thanks so much for the answer j.crater!
I modified the last repetitive structure:

for z in range(1, n+1):
    maximum = 0
    if factor_counter(z) >= factor_counter(n):
        maximum= z

print ("The number with most factors is  {0} ({1} factors)".format(maximum, factor_counter(maximum)))
So every time the program stumbles upon a nmber with a greater number of factors, the variable maximum would update, as you suggested. However, it gives wrong numbers for some inputs:

if factor_counter(z) >= factor_counter(n):

The purpose of this line is to compare the number of factors of a number z in between 1 and n and if it has more factors than the input number itself the variable would update. This comparison seems to be wrong and I'm having trouble fixing it...
Reply
#4
That code just gives you largest number with at least as many factors as n. And since z goes up to n, it's always going to be n. You want to check against factor_counter(maximum), not factor_counter(n).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,651 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Python Program to Find the Factorial of a Number elisahill 2 1,424 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,609 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Python prediction on historical data and/or external factors. pyrooky 1 1,709 Sep-03-2020, 03:19 PM
Last Post: DPaul
  Print the frequency of each coin for the combinations that sum to the amount N Pranav 3 2,527 May-19-2020, 06:16 AM
Last Post: Pranav
  traverses directory recursively and displays files Prani05 7 3,338 Apr-30-2020, 10:25 AM
Last Post: snippsat
  Unexpected change to a list in a small amount of self-contained code Johno 5 2,848 Mar-15-2020, 05:06 PM
Last Post: jefsummers
  Factors ERROR AdamJae 2 1,983 Oct-22-2019, 05:58 PM
Last Post: AdamJae
  Python Finding Prime Factors foxman322 1 2,302 Jan-11-2019, 04:33 PM
Last Post: ichabod801
  Creating a variable that displays time groovydingo 3 2,895 Apr-17-2018, 04:46 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020