Proper divisors: Difference between revisions

m (→‎{{header|langur}}: added langur revision no.)
(15 intermediate revisions by 10 users not shown)
Line 30:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F proper_divs(n)
R Array(Set((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x)))
 
Line 36:
 
V (n, leng) = max(((1..20000).map(n -> (n, proper_divs(n).len))), key' pd -> pd[1])
print(n‘ ’leng)</langsyntaxhighlight>
 
{{out}}
Line 47:
{{trans|Rexx}}
This program uses two ASSIST macros (XDECO, XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang="360asm">* Proper divisors 14/06/2016
PROPDIV CSECT
USING PROPDIV,R13 base register
Line 180:
XDEC DS CL12
YREGS
END PROPDIV</langsyntaxhighlight>
{{out}}
<pre>
Line 198:
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<langsyntaxhighlight Actionlang="action!">BYTE FUNC GetDivisors(INT a INT ARRAY divisors)
INT i,max
BYTE count
Line 256:
OD
PrintF("%I has %I proper divisors%E",ind,max)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Proper_divisors.png Screenshot from Atari 8-bit computer]
Line 283:
[[http://rosettacode.org/wiki/Amicable_pairs#Ada]], we define this routine as a function of a generic package:
 
<langsyntaxhighlight Adalang="ada">generic
type Result_Type (<>) is limited private;
None: Result_Type;
Line 301:
else Process(N, First+1));
end Generic_Divisors;</langsyntaxhighlight>
 
Now we instantiate the ''generic package'' to solve the other two parts of the task. Observe that there are two different instantiations of the package: one to generate a list of proper divisors, another one to count the number of proper divisors without actually generating such a list:
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Containers.Generic_Array_Sort, Generic_Divisors;
 
procedure Proper_Divisors is
Line 363:
Natural'Image(Number_Count) & " proper divisors.");
end;
end Proper_Divisors; </langsyntaxhighlight>
 
{{out}}
Line 382:
===As required by the Task===
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68"># MODE to hold an element of a list of proper divisors #
MODE DIVISORLIST = STRUCT( INT divisor, REF DIVISORLIST next );
 
Line 473:
, " divisors"
, newline
) )</langsyntaxhighlight>
{{out}}
<pre>
Line 492:
Alternative version that uses a sieve-like approach for faster proper divisor counting.
<br><br>Note, in order to run this with Algol 68G under Windows (and possibly Linux) the heap size must be increased, see [[ALGOL_68_Genie#Using_a_Large_Heap]].
<langsyntaxhighlight lang="algol68">BEGIN # count proper divisors using a sieve-like approach #
# find the first/only number in 1 : 20 000 and 1 : 64 000 000 with #
# the most divisors #
Line 529:
max number := 64 000 000
OD
END</langsyntaxhighlight>
 
{{out}}
Line 539:
=={{header|ALGOL-M}}==
Algol-M's maximum allowed integer value of 16,383 prevented searching up to 20,000 for the number with the most divisors, so the code here searches only up to 10,000.
<langsyntaxhighlight lang="algol">
BEGIN
 
Line 611:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 633:
===Functional===
{{Trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">-- PROPER DIVISORS -----------------------------------------------------------
 
-- properDivisors :: Int -> [Int]
Line 762:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{oneToTen:{{num:1, divisors:{1}}, {num:2, divisors:{1}}, {num:3, divisors:{1}},
{num:4, divisors:{1, 2}}, {num:5, divisors:{1}}, {num:6, divisors:{1, 2, 3}},
{num:7, divisors:{1}}, {num:8, divisors:{1, 2, 4}}, {num:9, divisors:{1, 3}},
{num:10, divisors:{1, 2, 5}}},
mostDivisors:{num:18480, divisors:79}}</langsyntaxhighlight>
----
 
===Idiomatic===
 
<langsyntaxhighlight lang="applescript">on properDivisors(n)
set output to {}
Line 819:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"1's proper divisors: {}
2's proper divisors: {1}
3's proper divisors: {1}
Line 834:
 
Largest number of proper divisors for any number from 1 to 20,000: 79
Numbers with this many: 15120, 18480"</langsyntaxhighlight>
 
=={{header|Arc}}==
<syntaxhighlight lang="arc">
<lang Arc>
 
;; Given num, return num and the list of its divisors
Line 884:
(div-lists 20000)
;; This took about 10 minutes on my machine.
</syntaxhighlight>
</lang>
{{Out}}
<syntaxhighlight lang="arc">
<lang Arc>
(1 0)
(2 1)
Line 907:
It is the number 18480.
There are 2 numbers with this trait, and they are (18480 15120)
</syntaxhighlight>
</lang>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program proFactor.s */
Line 1,080:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,110:
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">properDivisors: function [x] ->
(factors x) -- x
 
Line 1,127:
]
 
print ["The number with the most proper divisors (" maxProperDivisors ") is" maxN]</langsyntaxhighlight>
 
{{out}}
Line 1,144:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">proper_divisors(n) {
Array := []
if n = 1
Line 1,157:
Array[i] := true
return Array
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">output := "Number`tDivisors`tCount`n"
loop, 10
{
Line 1,175:
output .= "`nNumber(s) in the range 1 to 20,000 with the most proper divisors:`n" numDiv.Pop() " with " maxDiv " divisors"
MsgBox % output
return</langsyntaxhighlight>
{{out}}
<pre>Number Divisors Count
Line 1,193:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PROPER_DIVISORS.AWK
BEGIN {
Line 1,230:
return
}
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 1,252:
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">FUNCTION CountProperDivisors (number)
IF number < 2 THEN CountProperDivisors = 0
count = 0
Line 1,290:
PRINT
PRINT most; " has the most proper divisors, namely "; maxCount
END</langsyntaxhighlight>
{{out}}
<pre>
Line 1,297:
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">subroutine ListProperDivisors(limit)
if limit < 1 then return
for i = 1 to limit
Line 1,338:
print
print most; " has the most proper divisors, namely "; maxCount
end</langsyntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC o PureBasic.
</pre>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">let m = 1
let l = 10
 
if l >= 1 then
 
for i = 1 to l
 
if i = 1 then
 
print i, " : (None)"
 
else
 
for j = 1 to int(i / 2)
 
if i % j = 0 then
 
print i, " :", j
 
endif
 
next j
 
endif
 
next i
 
endif
 
for n = 2 to 20000
 
let c = 0
 
if n >= 2 then
 
for i = 1 to int(n / 2)
 
if n % i = 0 then
 
let c = c + 1
 
endif
 
next i
 
endif
 
if c > x then
 
let x = c
let m = n
 
endif
 
wait
 
next n
 
print m, " has the most proper divisors", comma, " namely ", x</syntaxhighlight>
{{out| Output}}<pre>
1 : (None)
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 has the most proper divisors, namely 79
</pre>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">FUNCTION CountProperDivisors (number)
IF number < 2 THEN LET CountProperDivisors = 0
LET count = 0
Line 1,382 ⟶ 1,456:
PRINT
PRINT most; "has the most proper divisors, namely"; maxCount
END</langsyntaxhighlight>
{{out}}
<pre>
Line 1,389 ⟶ 1,463:
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub ListProperDivisors(limit)
if limit < 1 then return : fi
for i = 1 to limit
Line 1,429 ⟶ 1,503:
print
print most, " has the most proper divisors, namely ", maxCount
end</langsyntaxhighlight>
{{out}}
<pre>
Line 1,437 ⟶ 1,511:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="qbasic">
FUNCTION ProperDivisor(nr, show)
 
Line 1,468 ⟶ 1,542:
 
PRINT "Most proper divisors for number in the range 1-20000: ", MagicNumber, " with ", MaxDivisors, " divisors."
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,487 ⟶ 1,561:
===Brute Force===
C has tedious boilerplate related to allocating memory for dynamic arrays, so we just skip the problem of storing values altogether.
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdbool.h>
Line 1,530 ⟶ 1,604:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,547 ⟶ 1,621:
===Number Theoretic===
There is no need to go through all the divisors if only the count is needed, this implementation refines the brute force approach by solving the second part of the task via a Number Theory formula. The running time is noticeably faster than the brute force method above. Output is same as the above.
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdbool.h>
Line 1,617 ⟶ 1,691:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">namespace RosettaCode.ProperDivisors
{
using System;
Line 1,651 ⟶ 1,725:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1: {}
Line 1,666 ⟶ 1,740:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <algorithm>
Line 1,704 ⟶ 1,778:
return 0 ;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,731 ⟶ 1,805:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
function divisors(Integer int) =>
Line 1,752 ⟶ 1,826:
print("the number(s) with the most divisors between ``start`` and ``end`` is/are:
``mostDivisors?.item else "nothing"`` with ``mostDivisors?.key else "no"`` divisors");
}</langsyntaxhighlight>
{{out}}
<pre>1 => []
Line 1,768 ⟶ 1,842:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns properdivisors
(:gen-class))
 
Line 1,803 ⟶ 1,877:
(println n " has " (count factors) " divisors"))
 
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,823 ⟶ 1,897:
=={{header|Common Lisp}}==
Ideally, the smallest-divisor function would only try prime numbers instead of odd numbers.
<langsyntaxhighlight lang="lisp">(defun proper-divisors-recursive (product &optional (results '(1)))
"(int,list)->list::Function to find all proper divisors of a +ve integer."
Line 1,858 ⟶ 1,932:
(dotimes (i 10) (format t "~A:~A~%" (1+ i) (proper-divisors-recursive (1+ i))))
(format t "Task 2:Count & list of numbers <=20,000 with the most Proper Divisors:~%~A~%"
(task #'proper-divisors-recursive)))</langsyntaxhighlight>
{{out}}
<pre>CL-USER(10): (main)
Line 1,878 ⟶ 1,952:
=={{header|Component Pascal}}==
{{Works with|Black Box Component Builder}}
<langsyntaxhighlight lang="oberon2">
MODULE RosettaProperDivisor;
IMPORT StdLog;
Line 1,934 ⟶ 2,008:
 
^Q RosettaProperDivisor.Do~
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,955 ⟶ 2,029:
{{trans|Python}}
Currently the lambda of the filter allocates a closure on the GC-managed heap.
<langsyntaxhighlight lang="d">void main() /*@safe*/ {
import std.stdio, std.algorithm, std.range, std.typecons;
 
Line 1,963 ⟶ 2,037:
iota(1, 11).map!properDivs.writeln;
iota(1, 20_001).map!(n => tuple(properDivs(n).count, n)).reduce!max.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[[], [1], [1], [1, 2], [1], [1, 2, 3], [1], [1, 2, 4], [1, 3], [1, 2, 5]]
Line 1,971 ⟶ 2,045:
=={{header|Delphi}}==
{{trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program ProperDivisors;
 
Line 2,038 ⟶ 2,112:
readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,055 ⟶ 2,129:
 
Version with TParallel.For
<syntaxhighlight lang="delphi">
<lang Delphi>
program ProperDivisors;
 
Line 2,123 ⟶ 2,197:
readln;
end.
</syntaxhighlight>
</lang>
 
=={{header|Dyalect}}==
Line 2,129 ⟶ 2,203:
{{trans|Swift}}
 
<langsyntaxhighlight lang="dyalect">func properDivs(n) {
if n == 1 {
yield break
Line 2,153 ⟶ 2,227:
}
print("\(num): \(max)")</langsyntaxhighlight>
 
{{out}}
Line 2,168 ⟶ 2,242:
10: [1, 2, 5]
15120: 79</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc propdivs n . divs[] .
divs[] = [ ]
if n < 2
return
.
divs[] &= 1
sqr = sqrt n
for d = 2 to sqr
if n mod d = 0
divs[] &= d
if d <> sqr
divs[] &= n / d
.
.
.
.
for i to 10
propdivs i d[]
write i & ":"
print d[]
.
for i to 20000
propdivs i d[]
if len d[] > max
max = len d[]
maxi = i
.
.
print maxi & " has " & max & " proper divisors."
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'list) ;; list-delete
 
Line 2,214 ⟶ 2,321:
 
 
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(for ((i (in-range 1 11))) (writeln i (divs i)))
1 null
Line 2,237 ⟶ 2,344:
(lib 'bigint)
(numdivs 95952222101012742144) → 666 ;; 🎩
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 2,293 ⟶ 2,400:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,311 ⟶ 2,418:
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Proper do
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,330 ⟶ 2,437:
IO.puts "#{n}: #{inspect Proper.divisors(n)}"
end)
Proper.most_divisors(20000)</langsyntaxhighlight>
 
{{out}}
Line 2,349 ⟶ 2,456:
=={{header|Erlang}}==
 
<langsyntaxhighlight lang="erlang">-module(properdivs).
-export([divs/1,sumdivs/1,longest/1]).
 
Line 2,375 ⟶ 2,482:
longest(L,Acc,A,Acc+1);
true -> longest(L,Current,CurLeng,Acc+1)
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,396 ⟶ 2,503:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// the simple function with the answer
let propDivs n = [1..n/2] |> List.filter (fun x->n % x = 0)
Line 2,416 ⟶ 2,523:
|> Seq.fold (fun a b ->match a,b with | (_,c1,_),(_,c2,_) when c2 > c1 -> b | _-> a) (0,0,[])
|> fun (n,count,_) -> (n,count,[]) |> show
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,434 ⟶ 2,541:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.functions
math.primes.factors math.ranges prettyprint sequences ;
 
Line 2,443 ⟶ 2,550:
10 [1,b] [ dup pprint bl divisors but-last . ] each
20000 [1,b] [ #divisors ] supremum-by dup #divisors
"%d with %d divisors.\n" printf</langsyntaxhighlight>
 
{{out}}
Line 2,461 ⟶ 2,568:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Divisors(n) =
[d]:=[(1)]; {start divisor list with just 1, which is a divisor of everything}
for i = 2 to n\2 do {loop through possible divisors of n}
Line 2,484 ⟶ 2,591:
od;
 
!!('The number up to 20,000 with the most divisors was ',champ,' with ',record,' divisors.');</langsyntaxhighlight>
 
{{out}}<pre>
Line 2,504 ⟶ 2,611:
{{works with|gforth|0.7.3}}
 
<langsyntaxhighlight lang="forth">: .proper-divisors
dup 1 ?do
dup i mod 0= if i . then
Line 2,531 ⟶ 2,638:
;
 
rosetta-proper-divisors</langsyntaxhighlight>
 
{{out}}
Line 2,551 ⟶ 2,658:
=={{header|Fortran}}==
Compiled using G95 compiler, run on x86 system under Puppy Linux
<syntaxhighlight lang="fortran">
<lang Fortran>
function icntprop(num )
Line 2,582 ⟶ 2,689:
print *,maxj,' has max proper divisors: ',maxcnt
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,620 ⟶ 2,727:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
' FreeBASIC v1.05.0 win64
 
Line 2,668 ⟶ 2,775:
Sleep
End
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,688 ⟶ 2,795:
</pre>
 
=={{header|Free Pascal}}==
<lang pascal>
Program ProperDivisors;
 
Uses fgl;
 
Type
TIntegerList = Specialize TfpgList<longint>;
 
Var list : TintegerList;
 
Function GetProperDivisors(x : longint): longint;
{this function will return the number of proper divisors
and put them in the list}
 
Var i : longint;
Begin
list.clear;
If x = 1 Then {by default 1 has no proper divisors}
GetProperDivisors := 0
Else
Begin
list.add(1); //add 1 as a proper divisor;
i := 2;
While i * i < x Do
Begin
If (x Mod i) = 0 Then //found a proper divisor
Begin
list.add(i); // add divisor
list.add(x Div i); // add result
End;
inc(i);
End;
If i*i=x Then list.add(i); //make sure to capture the sqrt only once
GetProperDivisors := list.count;
End;
End;
 
Var i,j,count,most : longint;
Begin
 
list := TIntegerList.Create;
For i := 1 To 10 Do
Begin
write(i:4,' has ', GetProperDivisors(i),' proper divisors:');
For j := 0 To pred(list.count) Do
write(list[j]:3);
writeln();
End;
count := 0; //store highest number of proper divisors
most := 0; //store number with highest number of proper divisors
For i := 1 To 20000 Do
If GetProperDivisors(i) > count Then
Begin
count := list.count;
most := i;
End;
writeln(most,' has ',count,' proper divisors');
list.free;
End.
</lang>
{{out}}
<pre>
1 has 0 proper divisors:
2 has 1 proper divisors: 1
3 has 1 proper divisors: 1
4 has 2 proper divisors: 1 2
5 has 1 proper divisors: 1
6 has 3 proper divisors: 1 2 3
7 has 1 proper divisors: 1
8 has 3 proper divisors: 1 2 4
9 has 2 proper divisors: 1 3
10 has 3 proper divisors: 1 2 5
15120 has 79 proper divisors
</pre>
 
=={{header|Frink}}==
Frink's built-in factorization routines efficiently find factors of arbitrary-sized integers.
 
<langsyntaxhighlight lang="frink">
for n = 1 to 10
println["$n\t" + join[" ", properDivisors[n]]]
Line 2,784 ⟶ 2,816:
 
properDivisors[n] := allFactors[n, true, false, true]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,799 ⟶ 2,831:
 
[15120, 18480] have 79 factors
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
local fn ProperDivisors( n as long ) as CFArrayRef
CFMutableArrayRef array = fn MutableArrayWithCapacity(0)
if ( n < 2 ) then exit fn
long i
for i = 1 to n - 1
if ( n mod i == 0 )
MutableArrayAddObject( array, @(i) )
end if
next
end fn = array
 
void local fn DoIt
long n, count, num, max = 0
for n = 1 to 10
printf @"%2ld: %@",n,fn ArrayComponentsJoinedByString( fn ProperDivisors( n ), @" " )
next
for n = 1 to 20000
count = len( fn Properdivisors( n ) )
if ( count > max )
max = count
num = n
end if
next
print: print num;@" has the most proper divisors with ";max
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
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 has the most proper divisors with 79
</pre>
 
=={{header|GFA Basic}}==
 
<syntaxhighlight lang="text">
OPENW 1
CLEARW 1
Line 2,879 ⟶ 2,964:
RETURN count%-1
ENDFUNC
</syntaxhighlight>
</lang>
 
Output is:
Line 2,898 ⟶ 2,983:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,959 ⟶ 3,044:
fmt.Println(n)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,983 ⟶ 3,068:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Ord
import Data.List
 
Line 2,995 ⟶ 3,080:
putStrLn "a number with the most divisors within 1 to 20000 (number, count):"
print $ maximumBy (comparing snd)
[(n, length $ divisors n) | n <- [1 .. 20000]]</langsyntaxhighlight>
{{out}}
<pre>divisors of 1 to 10:
Line 3,013 ⟶ 3,098:
For a little more efficiency, we can filter only up to the root – deriving the higher proper divisors from the lower ones, as quotients:
 
<langsyntaxhighlight lang="haskell">import Data.List (maximumBy)
import Data.Ord (comparing)
import Data.Bool (bool)
Line 3,038 ⟶ 3,123:
print $
maximumBy (comparing snd) $
(,) <*> (length . properDivisors) <$> [1 .. 20000]</langsyntaxhighlight>
{{Out}}
<pre>Proper divisors of 1 to 10:
Line 3,059 ⟶ 3,144:
and we can also define properDivisors in terms of primeFactors:
 
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primeFactors)
import Data.List (group, maximumBy, sort)
import Data.Ord (comparing)
Line 3,091 ⟶ 3,176:
w = maximum (length . xShow <$> xs)
in unlines $
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</langsyntaxhighlight>
{{Out}}
<pre>Proper divisors of [1..10]:
Line 3,117 ⟶ 3,202:
So, borrowing from [[Factors of an integer#J|the J implementation]] of that related task:
 
<langsyntaxhighlight Jlang="j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. ]</langsyntaxhighlight>
 
Proper divisors of numbers 1 through 10:
 
<langsyntaxhighlight Jlang="j"> (,&": ' -- ' ,&": properDivisors)&>1+i.10
1 --
2 -- 1
Line 3,132 ⟶ 3,217:
8 -- 1 2 4
9 -- 1 3
10 -- 1 2 5</langsyntaxhighlight>
 
Number(s) not exceeding 20000 with largest number of proper divisors (and the count of those divisors):
 
<langsyntaxhighlight Jlang="j"> (, #@properDivisors)&> 1+I.(= >./) #@properDivisors@> 1+i.20000
15120 79
18480 79</langsyntaxhighlight>
 
Note that it's a bit more efficient to simply count factors here, when selecting the candidate numbers.
 
<langsyntaxhighlight Jlang="j"> (, #@properDivisors)&> 1+I.(= >./) #@factors@> 1+i.20000
15120 79
18480 79</langsyntaxhighlight>
 
We could also arbitrarily toss either 15120 or 18480 (keeping the other number), if it were important that we produce only one result.
Line 3,150 ⟶ 3,235:
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Line 3,182 ⟶ 3,267:
System.out.println(x + ": " + count);
}
}</langsyntaxhighlight>
{{out}}
<pre>1: []
Line 3,200 ⟶ 3,285:
===ES5===
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
 
// Proper divisors
Line 3,268 ⟶ 3,353:
);
 
})();</langsyntaxhighlight>
 
{{out}}
Line 3,303 ⟶ 3,388:
===ES6===
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 3,412 ⟶ 3,497:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Proper divisors of [1..10]:
Line 3,432 ⟶ 3,517:
{{works with|jq|1.4}}
In the following, proper_divisors returns a stream. In order to count the number of items in the stream economically, we first define "count(stream)":
<langsyntaxhighlight lang="jq">def count(stream): reduce stream as $i (0; . + 1);
 
# unordered
Line 3,452 ⟶ 3,537:
( [null, 0];
count( $i | proper_divisors ) as $count
| if $count > .[1] then [$i, $count] else . end);</langsyntaxhighlight>
'''The tasks:'''
<langsyntaxhighlight lang="jq">"The proper divisors of the numbers 1 to 10 inclusive are:",
(range(1;11) as $i | "\($i): \( [ $i | proper_divisors] )"),
"",
Line 3,460 ⟶ 3,545:
"the maximal number proper divisors together with the corresponding",
"count of proper divisors is:",
most_proper_divisors(20000) </langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -n -c -r -f /Users/peter/jq/proper_divisors.jq
The proper divisors of the numbers 1 to 10 inclusive are:
1: []
Line 3,478 ⟶ 3,563:
the maximal number proper divisors together with the corresponding
count of proper divisors is:
[15120,79]</langsyntaxhighlight>
 
=={{header|Julia}}==
Use <code>factor</code> to obtain the prime factorization of the target number. I adopted the argument handling style of <code>factor</code> in my <code>properdivisors</code> function.
<syntaxhighlight lang="julia">
<lang Julia>
using Primes
function properdivisors{T<:Integer}(n::T)
function properdivisors(n::T) where {T<:Integer}
0 < n || throw(ArgumentError("number to be factored must be ≥ 0, got $n"))
1 < n || return T[]
!isprime(n) || return T[one(T), n]
f = factor(n)
d = T[one(T)]
Line 3,522 ⟶ 3,608:
 
println(nlst, " have the maximum proper divisor count of ", maxdiv, ".")
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,543 ⟶ 3,629:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun listProperDivisors(limit: Int) {
Line 3,582 ⟶ 3,668:
println("The following number(s) have the most proper divisors, namely " + maxCount + "\n")
for (n in most) println(n)
}</langsyntaxhighlight>
 
{{out}}
Line 3,608 ⟶ 3,694:
=={{header|langur}}==
{{trans|Go}}
<syntaxhighlight lang="langur">val .getproper = fn(.x) for[=[]] .i of .x \ 2 { if .x div .i: _for ~= [.i] }
{{works with|langur|0.10}}
<lang langur>val .getpropercntproper = ffn(.x) for[=[]0] .i of .x \ 2 { if .x div .i: _for ~+= [.i]1 }
val .cntproper = f(.x) for[=0] .i of .x \ 2 { if .x div .i: _for += 1 }
 
val .listproper = ffn(.x) {
if .x < 1: return null
for[=""] .i of .x {
writeln_for ~= $"\.i:2; -> ", \.getproper(.i);\n"
}
writeln()
}
 
writeln "The proper divisors of the following numbers are :"
writeln .listproper(10)
 
var .max = 0
Line 3,635 ⟶ 3,719:
 
writeln $"The following number(s) <= 20000 have the most proper divisors (\.max;)"
writeln .most</langsyntaxhighlight>
 
{{out}}
Line 3,655 ⟶ 3,739:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return a table of the proper divisors of n
function propDivs (n)
if n < 2 then return {} end
Line 3,686 ⟶ 3,770:
end
end
print(answer .. " has " .. mostDivs .. " proper divisors.")</langsyntaxhighlight>
{{out}}
<pre>1:
Line 3,703 ⟶ 3,787:
 
A Function that yields the proper divisors of an integer n:
<langsyntaxhighlight Mathematicalang="mathematica">ProperDivisors[n_Integer /; n > 0] := Most@Divisors@n;</langsyntaxhighlight>
 
Proper divisors of n from 1 to 10:
<langsyntaxhighlight Mathematicalang="mathematica">Grid@Table[{n, ProperDivisors[n]}, {n, 1, 10}]</langsyntaxhighlight>
{{out}}
<pre>1 {}
Line 3,720 ⟶ 3,804:
 
The number with the most divisors between 1 and 20,000:
<syntaxhighlight lang="mathematica">Fold[
<lang Mathematica>Fold[
Last[SortBy[{#1, {#2, Length@ProperDivisors[#2]}}, Last]] &,
{0, 0},
Range[20000]]</langsyntaxhighlight>
{{out}}
<pre>{18480, 79}</pre>
 
An alternate way to find the number with the most divisors between 1 and 20,000:
<langsyntaxhighlight Mathematicalang="mathematica">Last@SortBy[
Table[
{n, Length@ProperDivisors[n]},
{n, 1, 20000}],
Last]</langsyntaxhighlight>
{{out}}
<pre>{15120, 79}</pre>
 
=={{header|MATLAB}}==
<langsyntaxhighlight lang="matlab">function D=pd(N)
K=1:ceil(N/2);
D=K(~(rem(N, K)));</langsyntaxhighlight>
 
{{out}}
Line 3,779 ⟶ 3,863:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ProperDivisors;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 3,832 ⟶ 3,916:
 
ReadChar
END ProperDivisors.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import strformat
 
proc properDivisors(n: int) =
Line 3,877 ⟶ 3,961:
maxI = i
 
echo fmt"{maxI} with {max} divisors"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,894 ⟶ 3,978:
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">
MODULE ProperDivisors;
IMPORT
Line 3,991 ⟶ 4,075:
END
END ProperDivisors.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,011 ⟶ 4,095:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use Collection;
 
class Proper{
Line 4,054 ⟶ 4,138:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 4,073 ⟶ 4,157:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">Integer method: properDivs self 2 / seq filter(#[ self swap mod 0 == ]) }
 
10 seq apply(#[ dup print " : " print properDivs println ])
20000 seq map(#[ dup properDivs size Pair new ]) reduce(#maxKey) println</langsyntaxhighlight>
{{out}}
<pre>
Line 4,093 ⟶ 4,177:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">proper(n)=if(n==1, [], my(d=divisors(n)); d[2..#d]);
apply(proper, [1..10])
r=at=0; for(n=1,20000, t=numdiv(n); if(t>r, r=t; at=n)); [at, numdiv(t)-1]</langsyntaxhighlight>
{{out}}
<pre>%1 = [[], [2], [3], [2, 4], [5], [2, 3, 6], [7], [2, 4, 8], [3, 9], [2, 5, 10]]
Line 4,103 ⟶ 4,187:
{{works with|Free Pascal}}
Using prime factorisation
<langsyntaxhighlight lang="pascal">{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
Line 4,322 ⟶ 4,406:
j := CntProperDivs(primeDecomp);
PrimeFacOut(primeDecomp);writeln(' ',j:10,' factors'); writeln;
END.</langsyntaxhighlight>{{Output}}<pre>
1 :
2 : 1
Line 4,343 ⟶ 4,427:
===Using a module for divisors===
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/divisors/;
sub proper_divisors {
my $n = shift;
Line 4,365 ⟶ 4,449:
no warnings 'numeric';
say max(map { scalar(proper_divisors($_)) . " $_" } 1..20000);
}</langsyntaxhighlight>
{{out}}
<pre>1:
Line 4,384 ⟶ 4,468:
=={{header|Phix}}==
The factors routine is an auto-include. The actual implementation of it, from builtins\pfactors.e is
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">include1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
Line 4,416 ⟶ 4,500:
<span style="color: #008080;">return</span> <span style="color: #000000;">lfactors</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">hfactors</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</langsyntaxhighlight>-->
The compiler knows where to find that, so the main program is just:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)})</span>
Line 4,437 ⟶ 4,521:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d divisors: %v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">maxd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">candidates</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,455 ⟶ 4,539:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
function ProperDivisors($n) {
yield 1;
Line 4,492 ⟶ 4,576:
echo "They have ", key($divisorsCount), " divisors.\n";
 
</syntaxhighlight>
</lang>
 
Outputs:
Line 4,510 ⟶ 4,594:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
println(11111=proper_divisors(11111)),
nl,
Line 4,549 ⟶ 4,633:
println(maxN=MaxN),
println(maxLen=MaxLen),
nl.</langsyntaxhighlight>
 
{{out}}
Line 4,570 ⟶ 4,654:
===Larger tests===
Some larger tests of most number of divisors:
<langsyntaxhighlight Picatlang="picat">go2 =>
time(find_most_divisors(100_000)),
nl,
time(find_most_divisors(1_000_000)),
nl.</langsyntaxhighlight>
 
{{out}}
Line 4,584 ⟶ 4,668:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp"># Generate all proper divisors.
(de propdiv (N)
(head -1 (filter
Line 4,593 ⟶ 4,677:
(mapcar propdiv (range 1 10))
# Output:
# (NIL (1) (1) (1 2) (1) (1 2 3) (1) (1 2 4) (1 3) (1 2 5))</langsyntaxhighlight>
===Brute-force===
<langsyntaxhighlight PicoLisplang="picolisp">(de propdiv (N)
(cdr
(rot
Line 4,615 ⟶ 4,699:
(maxi
countdiv
(range 1 20000) ) )</langsyntaxhighlight>
===Factorization===
<langsyntaxhighlight PicoLisplang="picolisp">(de accu1 (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 4,640 ⟶ 4,724:
factor
(range 1 20000) )
@@ ) )</langsyntaxhighlight>
Output:
<pre>
Line 4,648 ⟶ 4,732:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source xref;
(subrg):
cpd: Proc Options(main);
Line 4,711 ⟶ 4,795:
End;
 
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 4,731 ⟶ 4,815:
=={{header|PowerShell}}==
===version 1===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function proper-divisor ($n) {
if($n -ge 2) {
Line 4,749 ⟶ 4,833:
"$(proper-divisor 496)"
"$(proper-divisor 2048)"
</syntaxhighlight>
</lang>
 
===version 2===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function proper-divisor ($n) {
if($n -ge 2) {
Line 4,768 ⟶ 4,852:
"$(proper-divisor 496)"
"$(proper-divisor 2048)"
</syntaxhighlight>
</lang>
 
===version 3===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function eratosthenes ($n) {
if($n -gt 1){
Line 4,821 ⟶ 4,905:
"$(proper-divisor 496)"
"$(proper-divisor 2048)"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 4,835 ⟶ 4,919:
Taking a cue from [http://stackoverflow.com/a/171779 an SO answer]:
 
<langsyntaxhighlight lang="prolog">divisor(N, Divisor) :-
UpperBound is round(sqrt(N)),
between(1, UpperBound, D),
Line 4,882 ⟶ 4,966:
proper_divisor_count(N, Count) ),
max(MaxCount, Num) ),
Result = (num(Num)-divisor_count(MaxCount)).</langsyntaxhighlight>
 
Output:
 
<langsyntaxhighlight lang="prolog">?- show_proper_divisors_of_range(1,10).
2:[1]
3:[1]
Line 4,900 ⟶ 4,984:
?- find_most_proper_divisors_in_range(1,20000,Result).
Result = num(15120)-divisor_count(79).
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
<lang PureBasic>
EnableExplicit
 
Line 4,960 ⟶ 5,044:
CloseConsole()
EndIf
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,983 ⟶ 5,067:
===Python: Literal===
A very literal interpretation
<langsyntaxhighlight lang="python">>>> def proper_divs2(n):
... return {x for x in range(1, (n + 1) // 2 + 1) if n % x == 0 and n != x}
...
Line 4,994 ⟶ 5,078:
>>> length
79
>>> </langsyntaxhighlight>
 
 
Line 5,012 ⟶ 5,096:
 
This version is over an order of magnitude faster for generating the proper divisors of the first 20,000 integers; at the expense of simplicity.
<langsyntaxhighlight lang="python">from math import sqrt
from functools import lru_cache, reduce
from collections import Counter
Line 5,056 ⟶ 5,140:
print([proper_divs(n) for n in range(1, 11)])
print(*max(((n, len(proper_divs(n))) for n in range(1, 20001)), key=lambda pd: pd[1]))</langsyntaxhighlight>
 
{{out}}
Line 5,066 ⟶ 5,150:
Defining a list of proper divisors in terms of the prime factorization:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Proper divisors'''
 
from itertools import accumulate, chain, groupby, product
Line 5,179 ⟶ 5,263:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Proper divisors of [1..10]:
Line 5,199 ⟶ 5,283:
=== Python: The Simple Way ===
Not all the code submitters realized that it's a tie for the largest number of factors inside the limit. The task description clearly indicates only one answer is needed. But both numbers are provided for the curious. Also shown is the result for 25000, as there is no tie for that, just to show the program can handle either scenario.
<langsyntaxhighlight lang="python">def pd(num):
factors = []
for divisor in range(1,1+num//2):
Line 5,238 ⟶ 5,322:
print()
showcount(20000)
showcount(25000)</langsyntaxhighlight>
{{out}}
<pre style="white-space: pre-wrap;">There are no proper divisors of 1
Line 5,260 ⟶ 5,344:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ factors -1 split drop ] is properdivisors ( n --> [ )
 
10 times [ i^ 1+ properdivisors echo cr ]
Line 5,271 ⟶ 5,355:
else drop ]
swap echo say " has "
echo say " proper divisors." cr</langsyntaxhighlight>
 
{{out}}
Line 5,291 ⟶ 5,375:
===Package solution===
{{Works with|R|3.3.2 and above}}
<langsyntaxhighlight lang="rsplus"># Proper divisors. 12/10/16 aev
require(numbers);
V <- sapply(1:20000, Sigma, k = 0, proper = TRUE); ind <- which(V==max(V));
cat(" *** max number of divisors:", max(V), "\n"," *** for the following indices:",ind, "\n");</langsyntaxhighlight>
 
{{Output}}
Line 5,303 ⟶ 5,387:
 
===Filter solution===
<langsyntaxhighlight lang="rsplus">#Task 1
properDivisors <- function(N) Filter(function(x) N %% x == 0, seq_len(N %/% 2))
 
Line 5,323 ⟶ 5,407:
" proper divisors.")
}
mostProperDivisors(20000)</langsyntaxhighlight>
 
{{Output}}
Line 5,366 ⟶ 5,450:
=== Short version ===
 
<langsyntaxhighlight lang="racket">#lang racket
(require math)
(define (proper-divisors n) (drop-right (divisors n) 1))
Line 5,376 ⟶ 5,460:
(if (< (length (cdr best)) (length divs)) (cons n divs) best)))
(printf "~a has ~a proper divisors\n"
(car most-under-20000) (length (cdr most-under-20000)))</langsyntaxhighlight>
 
{{out}}
Line 5,395 ⟶ 5,479:
 
The '''main''' module will only be executed when this file is executed. When used as a library, it will not be used.
<langsyntaxhighlight lang="racket">#lang racket/base
(provide fold-divisors ; name as per "Abundant..."
proper-divisors)
Line 5,432 ⟶ 5,516:
#:when [> c C])
(values c i)))
(printf "~a has ~a proper divisors\n" I C))</langsyntaxhighlight>
 
The output is the same as the short version above.
Line 5,442 ⟶ 5,526:
 
There really isn't any point in using concurrency for a limit of 20_000. The setup and bookkeeping drowns out any benefit. Really doesn't start to pay off until the limit is 50_000 and higher. Try swapping in the commented out race map iterator line below for comparison.
<syntaxhighlight lang="raku" perl6line>sub propdiv (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
Line 5,460 ⟶ 5,544:
}
 
say "max = {@candidates - 1}, candidates = {@candidates.tail}";</langsyntaxhighlight>
{{out}}
<pre>1 []
Line 5,476 ⟶ 5,560:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">
/*REXX*/
 
Line 5,525 ⟶ 5,609:
count_proper_divisors: Procedure
Parse Arg n
Return words(proper_divisors(n))</langsyntaxhighlight>
{{out}}
<pre>1 ->
Line 5,551 ⟶ 5,635:
 
With the (function) optimization, it's over &nbsp; '''20''' &nbsp; times faster.
<langsyntaxhighlight lang="rexx">/*REXX program finds proper divisors (and count) of integer ranges; finds the max count.*/
parse arg bot top inc range xtra /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot= 1 /*Not specified? Then use the default.*/
Line 5,592 ⟶ 5,676:
/* [↓] adjust for a square. ___*/
if j*j==x then return a j b /*Was X a square? If so, add √ X */
return a b /*return the divisors (both lists). */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the following input: &nbsp; <tt> 0 &nbsp; 10 &nbsp; 1 &nbsp; &nbsp; &nbsp; 20000 &nbsp; &nbsp; &nbsp; 166320 &nbsp; 1441440 &nbsp; 11796480000 </tt>}}
<pre>
Line 5,623 ⟶ 5,707:
 
It accomplishes a faster speed by incorporating the calculation of an &nbsp; ''integer square root'' &nbsp; of an integer &nbsp; (without using any floating point arithmetic).
<langsyntaxhighlight lang="rexx">/*REXX program finds proper divisors (and count) of integer ranges; finds the max count.*/
parse arg bot top inc range xtra /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot= 1 /*Not specified? Then use the default.*/
Line 5,668 ⟶ 5,752:
/* [↓] adjust for a square. ___*/
if j*j==x then return a j b /*Was X a square? If so, add √ X */
return a b /*return the divisors (both lists). */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 2<sup>nd</sup> REXX version when using the same inputs.}} <br><br>
 
Line 5,675 ⟶ 5,759:
 
For larger numbers, &nbsp; it is about &nbsp; '''7%''' &nbsp; faster.
<langsyntaxhighlight lang="rexx">/*REXX program finds proper divisors (and count) of integer ranges; finds the max count.*/
parse arg bot top inc range xtra /*obtain optional arguments from the CL*/
if bot=='' | bot=="," then bot= 1 /*Not specified? Then use the default.*/
Line 5,727 ⟶ 5,811:
end /*j*/
if r*r==x then return a j b /*Was X a square? If so, add √ X */
return a b /*return proper divisors (both lists).*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 2<sup>nd</sup> REXX version when using the same inputs.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Proper divisors
 
Line 5,748 ⟶ 5,832:
see nl
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 5,761 ⟶ 5,845:
9 -> 1 3
10 -> 1 2 5
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ DIVIS REVLIST TAIL REVLIST <span style="color:grey">@ or DIVIS 1 OVER SIZE 1 - SUB</span>
≫ '<span style="color:blue">PDIVIS</span>' STO
≪ 0 → max n
≪ 0
1 max '''FOR''' j
j <span style="color:blue">PDIVIS</span> SIZE
'''IF''' DUP2 < '''THEN''' SWAP j 'n' STO '''END'''
DROP
'''NEXT'''
DROP n DUP <span style="color:blue">PDIVIS</span> SIZE
≫ ≫ '<span style="color:blue">TASK2</span>' STO
 
≪ n <span style="color:blue">PDIVIS</span> ≫ 'n' 1 10 1 SEQ
20000 <span style="color:blue">TASK2</span>
{{out}}
<pre>
3: {{} {1} {1} {1 2} {1} {1 2 3} {1} {1 2 4} {1 3} {1 2 5}}
2: 15120
1: 39
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
class Integer
Line 5,781 ⟶ 5,889:
select.each do |n|
puts "#{n} has #{size} divisors"
end</langsyntaxhighlight>
 
{{out}}
Line 5,800 ⟶ 5,908:
 
===An Alternative Approach===
<langsyntaxhighlight lang="ruby">#Determine the integer within a range of integers that has the most proper divisors
#Nigel Galloway: December 23rd., 2014
require "prime"
Line 5,806 ⟶ 5,914:
(1..20000).each{|i| e = i.prime_division.inject(1){|n,g| n * (g[1]+1)}
n, g = e, i if e > n}
puts "#{g} has #{n-1} proper divisors"</langsyntaxhighlight>
 
{{out}}
Line 5,821 ⟶ 5,929:
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">trait ProperDivisors {
fn proper_divisors(&self) -> Option<Vec<u64>>;
}
Line 5,859 ⟶ 5,967:
most_divisors.len());
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Proper divisors of 1: []
Line 5,875 ⟶ 5,983:
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="basic">
<lang Basic>
$lines
 
Line 5,939 ⟶ 6,047:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,961 ⟶ 6,069:
 
===Simple proper divisors===
<langsyntaxhighlight Scalalang="scala">def properDivisors(n: Int) = (1 to n/2).filter(i => n % i == 0)
def format(i: Int, divisors: Seq[Int]) = f"$i%5d ${divisors.length}%2d ${divisors mkString " "}"
 
Line 5,973 ⟶ 6,081:
}
 
list.foreach( number => println(f"$number%5d ${properDivisors(number).length}") )</langsyntaxhighlight>
{{out}}
<pre> n cnt PROPER DIVISORS
Line 5,993 ⟶ 6,101:
If ''Long''s are enough to you you can replace every ''BigInt'' with ''Long'' and the one ''BigInt(1)'' with ''1L''
 
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
def factorize(x: BigInt): List[BigInt] = {
Line 6,010 ⟶ 6,118:
val products = (1 until factors.length).flatMap(i => factors.combinations(i).map(_.product).toList).toList
(BigInt(1) :: products).filter(_ < n)
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const proc: writeProperDivisors (in integer: n) is func
Line 6,059 ⟶ 6,167:
end for;
writeln(max_i <& " with " <& max <& " divisors");
end func;</langsyntaxhighlight>
 
{{out}}
Line 6,078 ⟶ 6,186:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func propdiv (n) {
n.divisors.slicefirst(0, -21)
}
 
Line 6,096 ⟶ 6,204:
}
 
say "max = #{max}, candidates = #{candidates}"</langsyntaxhighlight>
{{out}}
<pre>
Line 6,114 ⟶ 6,222:
=={{header|Swift}}==
Simple function:
<langsyntaxhighlight Swiftlang="swift">func properDivs1(n: Int) -> [Int] {
 
return filter (1 ..< n) { n % $0 == 0 }
}</langsyntaxhighlight>
More efficient function:
<langsyntaxhighlight Swiftlang="swift">import func Darwin.sqrt
 
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
Line 6,138 ⟶ 6,246:
return sorted(result)
}</langsyntaxhighlight>
Rest of the task:
<langsyntaxhighlight Swiftlang="swift">for i in 1...10 {
println("\(i): \(properDivs(i))")
}
Line 6,152 ⟶ 6,260:
}
 
println("\(num): \(max)")</langsyntaxhighlight>
{{out}}
<pre>1: []
Line 6,168 ⟶ 6,276:
 
=={{header|tbas}}==
<syntaxhighlight lang="vb">
<lang vb>
dim _proper_divisors(100)
 
Line 6,218 ⟶ 6,326:
print "A maximum at ";
show_proper_divisors(maxindex, false)
</syntaxhighlight>
</lang>
<pre>
>tbas proper_divisors.bas
Line 6,241 ⟶ 6,349:
Note that if a number, <math>k</math>, greater than 1 divides <math>n</math> exactly, both <math>k</math> and <math>n/k</math> are
proper divisors. (The raw answers are not sorted; the pretty-printer code sorts.)
<langsyntaxhighlight lang="tcl">proc properDivisors {n} {
if {$n == 1} return
set divs 1
Line 6,266 ⟶ 6,374:
}
}
puts "max: $maxI => (...$maxC…)"</langsyntaxhighlight>
{{out}}
<pre>
Line 6,284 ⟶ 6,392:
=={{header|uBasic/4tH}}==
{{trans|True BASIC}}
<syntaxhighlight lang="text">LET m = 1
LET c = 0
Line 6,326 ⟶ 6,434:
PRINT
NEXT
RETURN</langsyntaxhighlight>
{{Out}}
<pre>The proper divisors of the following numbers are:
Line 6,345 ⟶ 6,453:
0 OK, 0:415</pre>
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub Proper_Divisor()
Dim t() As Long, i As Long, l As Long, j As Long, c As Long
For i = 1 To 10
Line 6,372 ⟶ 6,480:
End If
S = t
End Function</langsyntaxhighlight>
{{out}}
<pre>Proper divisor of 1 :
Line 6,389 ⟶ 6,497:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function ProperDivisors(number As Integer) As IEnumerable(Of Integer)
Line 6,404 ⟶ 6,512:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>1: {}
Line 6,421 ⟶ 6,529:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
 
for (i in 1..10) System.print("%(Fmt.d(2, i)) -> %(Int.properDivisors(i))")
Line 6,436 ⟶ 6,544:
}
}
System.print("%(number) which has %(maxDivs) proper divisors.")</langsyntaxhighlight>
 
{{out}}
Line 6,456 ⟶ 6,564:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func PropDiv(N, Show); \Count and optionally show proper divisors of N
int N, Show, D, C;
[C:= 0;
Line 6,488 ⟶ 6,596:
];
IntOut(0, SN); ChOut(0, ^ ); IntOut(0, Max); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 6,499 ⟶ 6,607:
{{trans|D}}
This is the simple version :
<langsyntaxhighlight lang="zkl">fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }</langsyntaxhighlight>
This version is MUCH faster (the output isn't ordered however):
<langsyntaxhighlight lang="zkl">fcn properDivs(n){
if(n==1) return(T);
( pd:=[1..(n).toFloat().sqrt()].filter('wrap(x){ n%x==0 }) )
.pump(pd,'wrap(pd){ if(pd!=1 and (y:=n/pd)!=pd ) y else Void.Skip })
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="zkl">[1..10].apply(properDivs).println();
[1..20_001].apply('wrap(n){ T(properDivs(n).len(),n) })
.reduce(fcn([(a,_)]ab, [(c,_)]cd){ a>c and ab or cd },T(0,0))
.println();</langsyntaxhighlight>
{{out}}
<pre>
885

edits