Proper divisors: Difference between revisions
Content added Content deleted
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 |