Proper divisors: Difference between revisions
m (Paddy3118 moved page Proper Divisors to Proper divisors: capitalisation.) |
(+Java) |
||
Line 9: | Line 9: | ||
# Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has. |
# Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has. |
||
Show all output here. |
Show all output here. |
||
=={{header|Java}}== |
|||
{{works with|Java|1.5+}} |
|||
<lang java5>import java.util.Collections; |
|||
import java.util.LinkedList; |
|||
import java.util.List; |
|||
public class Proper{ |
|||
public static List<Integer> properDivs(int n){ |
|||
List<Integer> divs = new LinkedList<Integer>(); |
|||
divs.add(1); |
|||
for(int x = 2; x < n; x++){ |
|||
if(n % x == 0) divs.add(x); |
|||
} |
|||
Collections.sort(divs); |
|||
return divs; |
|||
} |
|||
public static void main(String[] args){ |
|||
for(int x = 1; x <= 10; x++){ |
|||
System.out.println(x + ": " + properDivs(x)); |
|||
} |
|||
int x = 0, count = 0; |
|||
for(int n = 1; n <= 20000; n++){ |
|||
if(properDivs(n).size() > count){ |
|||
x = n; |
|||
count = properDivs(n).size(); |
|||
} |
|||
} |
|||
System.out.println(x + ": " + count); |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>1: [1] |
|||
2: [1] |
|||
3: [1] |
|||
4: [1, 2] |
|||
5: [1] |
|||
6: [1, 2, 3] |
|||
7: [1] |
|||
8: [1, 2, 4] |
|||
9: [1, 3] |
|||
10: [1, 2, 5] |
|||
15120: 79</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
A very literal interpretation |
A very literal interpretation |
Revision as of 02:07, 16 December 2014
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder, and always include 1.
For example the proper divisors of 6 are 1, 2, and 3. The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
- Task
- Create a routine to generate all the proper divisors of a number.
- use it to show the proper divisors of the numbers 1 to 10 inclusive.
- Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has.
Show all output here.
Java
<lang java5>import java.util.Collections; import java.util.LinkedList; import java.util.List;
public class Proper{
public static List<Integer> properDivs(int n){ List<Integer> divs = new LinkedList<Integer>(); divs.add(1); for(int x = 2; x < n; x++){ if(n % x == 0) divs.add(x); } Collections.sort(divs); return divs; } public static void main(String[] args){ for(int x = 1; x <= 10; x++){ System.out.println(x + ": " + properDivs(x)); } int x = 0, count = 0; for(int n = 1; n <= 20000; n++){ if(properDivs(n).size() > count){ x = n; count = properDivs(n).size(); } } System.out.println(x + ": " + count); }
}</lang>
- Output:
1: [1] 2: [1] 3: [1] 4: [1, 2] 5: [1] 6: [1, 2, 3] 7: [1] 8: [1, 2, 4] 9: [1, 3] 10: [1, 2, 5] 15120: 79
Python
A very literal interpretation <lang python>>>> def proper_divs2(n): ... return {x for x in range(1, (n + 1) // 2 + 1) if n % x == 0} ... >>> [proper_divs2(n) for n in range(1, 11)] [{1}, {1}, {1}, {1, 2}, {1}, {1, 2, 3}, {1}, {1, 2, 4}, {1, 3}, {1, 2, 5}] >>> >>> n, length = max(((n, len(proper_divs2(n))) for n in range(1, 20001)), key=lambda pd: pd[1]) >>> n 15120 >>> length 79 >>> </lang>