Jump to content

Proper divisors: Difference between revisions

Line 4,383:
Numbers with most divisors: 15120, 18480.
They have 79 divisors.</pre>
 
=={{header|Picat}}==
<lang Picat>go =>
println(11111=proper_divisors(11111)),
nl,
foreach(N in 1..10)
println(N=proper_divisors(N))
end,
nl,
find_most_divisors(20_000),
nl.
 
% Proper divisors of number N
proper_divisors(N) = Divisors =>
Div1 = [ I : I in 1..ceiling(sqrt(N)), N mod I == 0],
Divisors = (Div1 ++ [N div I : I in Div1]).sort_remove_dups().delete(N).
 
 
% Find the number(s) with the most proper divisors below Limit
find_most_divisors(Limit) =>
MaxN = [],
MaxNumDivisors = [],
MaxLen = 1,
 
foreach(N in 1..Limit, not prime(N))
D = proper_divisors(N),
Len = D.len,
% Get all numbers with most proper divisors
if Len = MaxLen then
MaxN := MaxN ++ [N],
MaxNumDivisors := MaxNumDivisors ++ [[N=D]]
elseif Len > MaxLen then
MaxLen := Len,
MaxN := [N],
MaxNumDivisors := [N=D]
end
end,
 
println(maxN=MaxN),
println(maxLen=MaxLen),
nl.</lang>
 
{{out}}
<pre>11111 = [1,41,271]
 
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]
 
maxN = [15120,18480]
maxLen = 79</pre>
 
 
Some larger tests of most number of divisors:
<lang Picat>go2 =>
time(find_most_divisors(100_000)),
nl,
time(find_most_divisors(1_000_000)),
nl.</lang>
 
{{out}}
<pre>maxN = [83160,98280]
maxLen = 127
 
maxN = [720720,831600,942480,982800,997920]
maxLen = 239</pre>
 
=={{header|PicoLisp}}==
495

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.