Question:
[Python] Listing all the Divisors of a Number?
DerpyPandas
2012-09-21 19:49:56 UTC
The question was to show all the divisors of a given "n" on one line. On top of that, if you enter a negative number, it should consistently ask for a valid "n".

This is what I have so far, and I want to know how to do the second part. Running Python 3.2:

example of what it should look like:
Enter N: 10
The divisors of N are: 1 2 5 10

What I have so far:

n = input("Enter N: ")
user_N = int(n)

while(user_N < 0):
print("Error. Please enter positive numbers only.")
input("Enter N: ")
Three answers:
radicalbiscuit
2012-09-21 21:15:07 UTC
That's a good start. It's important to have valid input. You might want to make sure the input is not less than 1 because 0 / 0 is undefined.



Your best friends for this problem are going to be the range() function, the list, the for loop, the modulo operator (%), and the string.join() method.



After you have the input here's the general algorithm:



create an empty list (we'll call it divisors)

for each number in a list containing numbers from 1 to user_N:

    if the user_N % number is equal to 0:

        append str(number) to our divisors list

Print out "The divisors of N are: " + our list of divisors, where each element in the list is joined by a space



Okay. I'm not going to turn that into code for you, but I'll explain all the parts you need.



Lists, as you probably know, are ordered collections of objects. We can use a list to store all our divisors as we find them. You add something to a list by using the list's .append() method. If our list is named divisors and we want to add 5 to it, we say "divisors.append(5)". We have to create an empty list before the loop so that the same list will be accessible to all iterations of the loop.



Python's range() function creates a list of integers for a given range. If you say "range(5)", it creates a list that looks like [0, 1, 2, 3, 4]. You should note that it contains 5 elements, but does not contain 5 itself. It will go all the way up to the number before the number you entered. There are good reasons for this, but I won't go into them.



Another way to do a range function is to give it a starting number then an ending number (but it will end on the number just before the end number you give it). range(1, 5) will give you the list [1, 2, 3, 4] (so we leave out the 0). This is probably what you want, keeping in mind that you'll want to do range(1, user_N + 1) so the list will go all the way up to and include user_N.



For loops in Python are great. You can literally say "for i in range(5):" and it will loop through the list [0, 1, 2, 3, 4], assigning each element of that list to the variable i down below, so you can do something individually with each member of the list. You don't have to use "i". You could use more descriptive names like "number", "possibleDivisor", or "billy_bob".



Modulo is an operation that basically gives you the remainder of dividing the number on the left by the number on the right. So 2 % 10 = 2 since 2 / 10 = 0 remainder 2. 10 % 2 = 0 however since 10 can be evenly divided by 2 (there is no remainder). So, when we're looking for divisors of a number, we find all numbers that, when dividing our larger number, have no remainder.



When you find that a number divides user_N, you can add it to your divisors list. I recommend casting the divisors as strings (using the str() function) before adding them to the divisors list. This is because we're going to use the .join() method on the list and it only likes working with strings.



After the for loop, we've found all our divisors. Now we need to print our string. The .join() method is what we want. Try this: ' '.join(['1', '2', '3']). It will print out "1 2 3". You're saying "Join each element from the list with a space." If you wanted to join them with the words "Hi mom!" you could say 'Hi mom!'.join(['1', '2', '3']) and it would print "1Hi mom!2Hi mom!3". So, you'd want to do something like

' '.join(divisors)



I think you should be able to figure it out from there. Let me know if you have any trouble and good luck!
2016-04-03 10:37:13 UTC
For the best answers, search on this site https://shorturl.im/ayeqJ



Modify your function slightly so that instead of printing i and count, you add the count to a dictionary and return the dictionary at the end. Example: def countDivisors(n): >allCounts = {} >for i in range(1,n+1): >>>count = 0 >>>for possible in range(1, n+1): >>>>>if i % possible == 0: >>>>>>>count = count + 1 >>>allCounts[i] = count >return allCounts And now you can print the number (actually, since there may be several numbers that share the same greatest number of divisors, there will be a list of numbers) with the most divisors in one line: counts = countDivisors(1000) print [number for number in counts if counts[number] == sorted(counts.values())[-1]] This uses a list comprehension to put together all the numbers that share the highest count value. Spoiler: the answer is 840, with 32 divisors!
Anas Imtiaz
2012-09-22 00:00:10 UTC
Try this code:

n = 0

while 1:

n = int(raw_input("Enter N:\t"))

if n > 0:

break

print "\nError. Please enter only positive numbers."



i = 1

print "\nThe divisors of N are:",

while i <= n:

if (n % i) == 0:

print i,

i += 1



For code (with proper indentation) and sample run: http://ideone.com/7Ye1j


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...