Largest proper divisor of n: Difference between revisions

m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Minor tidy)
 
(34 intermediate revisions by 15 users not shown)
Line 503:
</syntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
Line 515 ⟶ 514:
13 46 31 47 19 48 1 49 33 50
 
Largest proper divisor of n 1-100</pre>
</pre>
 
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR I=1 TO 100
Line 539:
13 46 31 47 19 48 1 49 33 50</pre>
 
==={{header|BASIC256ANSI BASIC}}===
{{trans|FreeBASIC}}
{{works with|Decimal BASIC}}
{{works with|IS-BASIC}}
<syntaxhighlight lang="basic">
100 REM Largest proper divisor of n
110 PRINT "The largest proper divisor of n is:"
120 PRINT
130 PRINT USING " ## ##": 1, 1;
140 FOR I = 3 TO 100
150 FOR J = I - 1 TO 1 STEP -1
160 IF MOD(I, J) = 0 THEN
170 PRINT USING "###": J;
180 EXIT FOR
190 END IF
200 NEXT J
210 IF MOD(I, 10) = 0 THEN PRINT
220 NEXT I
230 END
</syntaxhighlight>
{{out}}
<pre>
The largest proper divisor of n is:
 
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
==={{header|Applesoft BASIC}}===
{{works with|Quite BASIC}}
Solution [[#Quite_BASIC|Quite BASIC]] work without changes.
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">print "El mayor divisor propio de n es:\n"
Line 554 ⟶ 594:
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">10 print "El mayor divisor propio de n es:"
20 print chr$(10)+" 1 1";
30 for i = 3 to 100
40 for j = i-1 to 1 step -1
50 if i mod j = 0 then print using "###";j; : exit for
60 next j
70 if i mod 10 = 0 then print
80 next i
90 end</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">print "Largest proper divisor of n is:"
print tab, "1", tab, "1",
 
for i = 3 to 100
 
for j = i - 1 to 1 step -1
 
if i mod j = 0 then
 
print tab, j,
break j
 
endif
 
wait
 
next j
 
if i mod 10 = 0 then
 
print
 
endif
 
next i</syntaxhighlight>
{{out| Output}}<pre>
Largest proper divisor of n is:
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Print !"El mayor divisor propio de n es:\n"
Print " 1 1";
For i As Byte = 3 To 100
For j As Byte = i-1 To 1 Step -1
If i Mod j = 0 Then Print Using "###"; j; : Exit For
Next j
If i Mod 10 = 0 Then Print
Next i
Sleep</syntaxhighlight>
{{out}}
<pre>El mayor divisor propio de n es:
 
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">NSUInteger i, j
 
print " 1 1";
for i = 3 to 100
for j = i - 1 to 1 step - 1
if i mod j = 0 then print using "###"; j; : exit for
next
if i mod 10 = 0 then print
next
 
HandleEvents</syntaxhighlight>
{{output}}
<pre> 1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasic">10 PRINT 1;
20 FOR I = 1 TO 101
30 FOR D = I\2 TO 1 STEP -1
40 IF I MOD D = 0 THEN PRINT D; : GOTO 60
50 NEXT D
60 NEXT I</syntaxhighlight>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Print "El mayor divisor propio de n es:\n"
Print " 1 1";
For i As Byte = 3 To 100
For j As Byte = i - 1 To 1 Step -1
If i Mod j = 0 Then
Print Format$(j, "###");
Break
End If
Next
If i Mod 10 = 0 Then Print
Next
End</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|BASICA}}
{{trans|FreeBASIC}}
{{works with|IS-BASIC}}
<syntaxhighlight lang="qbasic">100 PRINT "El mayor divisor propio de n es:"
110 PRINT
120 PRINT " 1 1 ";
130 FOR i = 3 TO 100
140 FOR j = i-1 TO 1 STEP -1
150 IF i-INT(i/j)*j = 0 THEN 200
160 NEXT j
170 IF i-INT(i/10)*10 = 0 THEN 220
180 NEXT i
190 GOTO 240
200 PRINT j;
210 GOTO 170
220 PRINT
230 GOTO 180
240 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|GW-BASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">10 PRINT "El mayor divisor propio de n es:"
20 PRINT CHR$(10) + " 1 1";
30 FOR I = 3 TO 100
40 FOR J = I-1 TO 1 STEP -1
50 IF I MOD J = 0 THEN PRINT USING "###"; J; : GOTO 70
60 NEXT J
70 IF I MOD 10 = 0 THEN PRINT
80 NEXT I
90 END</syntaxhighlight>
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">
10 REM LARGEST PROPER DIVISOR OF N
20 PRINT "THE LARGEST PROPER DIVISOR OF N IS:"
30 PRINT;PRINT #3,1,1,
40 FOR I=3 TO 100
50 FOR J=I-1 TO 1 STEP -1
60 IF I=(I/J)*J PRINT #3,J,;GOTO 80
70 NEXT J
80 IF I=(I/10)*10 PRINT
90 NEXT I
100 STOP
</syntaxhighlight>
{{out}}
<pre>
THE LARGEST PROPER DIVISOR OF N IS:
 
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.i lpd(v.i)
For i=v/2 To 1 Step -1
If v%i=0 : ProcedureReturn i : EndIf
Next
ProcedureReturn 1
EndProcedure
 
If OpenConsole("")
For i=1 To 100
Print(RSet(Str(lpd(i)),3))
If i%10=0 : PrintN("") : EndIf
Next
Input()
EndIf</syntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
==={{header|Quite BASIC}}===
{{works with|Applesoft BASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 PRINT "El mayor divisor propio de n es:"
110 PRINT : PRINT " 1 1";
120 FOR i = 3 TO 100
130 FOR j = i-1 TO 1 STEP -1
140 LET a = i-INT(i/j)*j
150 IF a = 0 THEN GOTO 210
160 IF a = 0 THEN GOTO 180
170 NEXT j
180 IF i-INT(i/10)*10 = 0 THEN PRINT
190 NEXT i
200 END
210 IF j < 10 THEN PRINT " "; j;
220 IF j >= 10 THEN PRINT " "; j;
230 GOTO 160</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="vbnet">print "Largest proper divisor of n is:"
print chr$(10)+" 1 1";
for i = 3 to 100
for j = i-1 to 1 step -1
if i mod j = 0 then print using("###",j); : goto [exit]
next j
[exit]
if i mod 10 = 0 then print
next i
end</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="qbasic">REM Rosetta Code problem: https://rosettacode.org/wiki/Largest_proper_divisor_of_n
REM by Jjuanhdez, 05/2023
 
REM Largest proper divisor of n
 
PRINT "El mayor divisor propio de n es:"
PRINT ""
PRINT "1"
PRINT "1"
LET I = 3
10 IF I = 101 THEN GOTO 40
LET J = I-1
20 IF J = 0 THEN GOTO 30
LET A = I-(I/J)*J
IF A = 0 THEN PRINT J
IF A = 0 THEN GOTO 30
LET J = J-1
GOTO 20
30 IF I-(I/10)*10 = 0 THEN PRINT ""
LET I = I+1
GOTO 10
40 END
</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">PRINT "El mayor divisor propio de n es:"
PRINT
PRINT " 1 1";
FOR i = 3 To 100
FOR j = i-1 To 1 Step -1
IF remainder(i, j) = 0 Then
PRINT Using$("###", j);
EXIT FOR
END IF
NEXT j
IF remainder(i, 10) = 0 Then PRINT
NEXT i
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">PROGRAM "progname"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
PRINT "El mayor divisor propio de n es:\n"
PRINT " 1 1 ";
FOR i = 3 TO 100
FOR j = i-1 TO 1 STEP -1
IF i MOD j = 0 THEN
PRINT FORMAT$("###", j);
EXIT FOR
END IF
NEXT j
IF i MOD 10 = 0 THEN PRINT
NEXT i
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">print "El mayor divisor propio de n es:\n"
print " 1 1 ";
for i = 3 to 100
for j = i-1 to 1 step -1
If mod(i, j) = 0 then print j using "##"; : break : fi
next j
if mod(i, 10) = 0 then print : fi
next i
print
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|BCPL}}==
Line 708 ⟶ 1,076:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|D}}==
 
{{trans|C}}
 
<syntaxhighlight lang="d">
import std.stdio;
import std.range;
import std.algorithm;
 
uint lpd(uint n) {
if (n <= 1) {
return 1;
}
 
auto divisors = array(iota(1, n).filter!(i => n % i == 0));
 
return divisors.empty ? 1 : divisors[$ - 1];
}
 
void main() {
foreach (i; 1 .. 101) {
writef("%3d", lpd(i));
 
if (i % 10 == 0) {
writeln();
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">
import "dart:io";
 
num largest_proper_divisor(int n) {
assert(n > 0);
if ((n & 1) == 0) return n >> 1;
for (int p = 3; p * p <= n; p += 2) {
if (n % p == 0) return n / p;
}
return 1;
}
 
void main() {
print("El mayor divisor propio de n es:");
for (int n = 1; n < 101; ++n) {
stdout.write(largest_proper_divisor(n));
print(largest_proper_divisor(n) + n % 10 == 0 ? "\n" : " ");
}
}
</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function FindProperDivisor(N: Integer): integer;
{Find the highest proper divisor}
{i.e. The highest number that evenly divides in N}
begin
if N=1 then Result:=1
else for Result:=N-1 downto 1 do
if (N mod Result)=0 then break;
end;
 
 
 
procedure AllProperDivisors(Memo: TMemo);
{Show all proper divisors for number 1..100}
var I: integer;
var S: string;
begin
S:='';
for I:=1 to 100 do
begin
S:=S+Format('%3d',[FindProperDivisor(I)]);
if (I mod 10)=0 then S:=S+#$0D#$0A;
end;
Memo.Text:=S;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
 
=={{header|Draco}}==
Line 739 ⟶ 1,223:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func lpdiv v .
r = 1
for i = 2 to v div 2
if v mod i = 0
r = i
.
.
return r
.
for i = 1 to 100
write lpdiv i & " "
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 888 ⟶ 1,388:
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Print !"El mayor divisor propio de n es:\n"
Print " 1 1";
For i As Byte = 3 To 100
For j As Byte = i-1 To 1 Step -1
If i Mod j = 0 Then Print Using "###"; j; : Exit For
Next j
If i Mod 10 = 0 Then Print
Next i
Sleep</syntaxhighlight>
{{out}}
<pre>El mayor divisor propio de n es:
 
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
Line 972 ⟶ 1,447:
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 PRINT 1;
20 FOR I = 1 TO 101
30 FOR D = I\2 TO 1 STEP -1
40 IF I MOD D = 0 THEN PRINT D; : GOTO 60
50 NEXT D
60 NEXT I</syntaxhighlight>
 
=={{header|Haskell}}==
Line 1,040 ⟶ 1,507:
 
=={{header|J}}==
<syntaxhighlight lang="j"> lpd =: (1 |}.!.1 ])&.q:</syntaxhighlight>
{{out}}
<pre>
lpd 1+>: i.100 5 20
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10
1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 ...</pre>
7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50
</pre>
 
This works by prime factorization of n, removing the smallest prime factor, and then taking the product of this new list of prime factors. In J terms, we work "under" factorization, in analogy to a medical operation where the patient is "under" anesthesia: (put patient to sleep) rearrange guts (wake patient up).
 
The core logic only concerns itself with a list of prime factors; it is never even aware of the original input integer, nor the final integer result. In fact, note that the final multiplication is implicit and never spelled out by the programmer; product is the inverse of factorization, and we requested to work "under" factorization, thus J's algebra knows to apply the inverse of factorization (i.e. taking the product) as the final step.
This works by prime factorization of n, replacing the largest prime factor with 1, and then taking the product of this new list of prime factors. In J terms, we work "under" factorization, in analogy to a medical operation where the patient is "under" anaethesia: (put patient to sleep) rearrange guts (wake patient up).
 
=={{header|Java}}==
The core logic only concerns itself with a list of prime factors; it is never even aware of the original input integer, nor the final integer result. In fact, note that the final multiplication is implicit and never spelled out by the programmer; product is the inverse of factorization, and we requested to work "under" factorization, thus J's algebra knows to apply the inverse of factorization (i.e. taking the product) as the final step.
<syntaxhighlight lang="java">
public final class LargestProperDivisor {
 
public static void main(String[] aArgs) {
for ( int n = 1; n < 101; n++ ) {
System.out.print(String.format("%2d%s", largestProperDivisor(n), ( n % 10 == 0 ? "\n" : " " )));
}
}
private static int largestProperDivisor(int aNumber) {
if ( aNumber < 1 ) {
throw new IllegalArgumentException("Argument must be >= 1: " + aNumber);
}
if ( ( aNumber & 1 ) == 0 ) {
return aNumber >> 1;
}
for ( int p = 3; p * p <= aNumber; p += 2 ) {
if ( aNumber % p == 0 ) {
return aNumber / p;
}
}
return 1;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|jq}}==
Line 1,111 ⟶ 1,625:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|Lua}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="lua">
for n = 1, 100 do -- show the largest proper divisors for n = 1..100
local largestProperDivisor, j = 1, math.floor( n / 2 )
while j >= 2 and largestProperDivisor == 1 do
if n % j == 0 then
largestProperDivisor = j
end
j = j - 1
end
io.write( string.format( "%3d", largestProperDivisor ) )
if n % 10 == 0 then io.write( "\n" ) end
end
</syntaxhighlight>
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
Line 1,147 ⟶ 1,690:
{{out}}
<pre>{1,1,1,2,1,3,1,4,3,5,1,6,1,7,5,8,1,9,1,10,7,11,1,12,5,13,9,14,1,15,1,16,11,17,7,18,1,19,13,20,1,21,1,22,15,23,1,24,7,25,17,26,1,27,11,28,19,29,1,30,1,31,21,32,13,33,1,34,23,35,1,36,1,37,25,38,11,39,1,40,27,41,1,42,17,43,29,44,1,45,13,46,31,47,19,48,1,49,33,50}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
lpd(n):=if n=1 then 1 else listify(divisors(n))[length(divisors(n))-1]$
 
makelist(lpd(i),i,100);
</syntaxhighlight>
{{out}}
<pre>
[1,1,1,2,1,3,1,4,3,5,1,6,1,7,5,8,1,9,1,10,7,11,1,12,5,13,9,14,1,15,1,16,11,17,7,18,1,19,13,20,1,21,1,22,15,23,1,24,7,25,17,26,1,27,11,28,19,29,1,30,1,31,21,32,13,33,1,34,23,35,1,36,1,37,25,38,11,39,1,40,27,41,1,42,17,43,29,44,1,45,13,46,31,47,19,48,1,49,33,50]
</pre>
 
=={{header|Modula-2}}==
Line 1,254 ⟶ 1,808:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
===Alternative (sieve)===
Most solutions use a function that returns the largest proper divisor of an individual integer. This console program, written in Free Pascal, applies a sieve to the integers 1..100 (or other limit).
<syntaxhighlight lang="pascal">
program LPD;
(*
Displays largest proper divisor for each integer in range 1..limit.
Command line:
LPD limit items_per_line
or LPD limit // items_per_line defaults to 10
or LPD // limit defaults to 100
*)
{$mode objfpc}{$H+}
 
uses SysUtils;
var
limit, items_per_line, nr_items, j, p : integer;
a : array of integer;
begin
// Set up defaults
limit := 100;
items_per_line := 10;
// Overwrite defaults with command-line parameters, if present
if ParamCount > 0 then
limit := SysUtils.StrToInt( ParamStr(1));
if ParamCount > 1 then
items_per_line := SysUtils.StrToInt( ParamStr(2));
WriteLn( 'Largest proper divisors 1..', limit);
// Dynamic arrays are 0-based. To keep it simple, we ignore a[0]
// and use a[j] for the integer j, 1 <= j <= limit
SetLength( a, limit + 1);
for j := 1 to limit do a[j] := 1; // stays at 1 if j is 1 or prime
 
// Sieve; if j is composite then a[j] := smallest prime factor of j
p := 2; // p = next prime
while p*p < limit do begin
j := 2*p;
while j <= limit do begin
if a[j] = 1 then a[j] := p;
inc( j, p);
end;
repeat
inc(p);
until (p > limit) or (a[p] = 1);
end;
 
// If j is composite, divide j by its smallest prime factor
for j := 1 to limit do
if a[j] > 1 then a[j] := j div a[j];
 
// Write the array to the console
nr_items := 0;
for j := 1 to limit do begin
Write( a[j]:5);
inc( nr_items);
if nr_items = items_per_line then begin
WriteLn;
nr_items := 0;
end;
end;
if nr_items > 0 then WriteLn;
end.
</syntaxhighlight>
{{out}}
<pre>
Largest proper divisors 1..100
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|Perl}}==
Line 1,413 ⟶ 2,043:
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure.i lpd(v.i)
For i=v/2 To 1 Step -1
If v%i=0 : ProcedureReturn i : EndIf
Next
ProcedureReturn 1
EndProcedure
 
If OpenConsole("")
For i=1 To 100
Print(RSet(Str(lpd(i)),3))
If i%10=0 : PrintN("") : EndIf
Next
Input()
EndIf</syntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,737 ⟶ 2,340:
13 46 31 47 19 48 1 49 33 50
done...</pre>
 
=={{header|RPL}}==
≪ '''IF''' DUP 1 ≠ '''THEN''' DUP '''DO''' 1 - '''UNTIL''' DUP2 MOD NOT '''END''' SWAP DROP '''END''' ≫ '<span style="color:blue">LPROPDIV</span>' STO
≪ { } 1 100 '''FOR''' j j <span style="color:blue">LPROPDIV</span> + '''NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 1 1 1 2 1 3 1 4 3 5 1 6 1 7 5 8 1 9 1 10 7 11 1 12 5 13 9 14 1 15 1 16 11 17 7 18 1 19 13 20 1 21 1 22 15 23 1 24 7 25 17 26 1 27 11 28 19 29 1 30 1 31 21 32 13 33 1 34 23 35 1 36 1 37 25 38 11 39 1 40 27 41 1 42 17 43 29 44 1 45 13 46 31 47 19 48 1 49 33 50 }
</pre>
 
=={{header|Rust}}==
 
{{trans|Go}}
 
<syntaxhighlight lang="rust">
fn largest_proper_divisor(n: i32) -> i32 {
for i in 2..=(n as f64).sqrt() as i32 {
if n % i == 0 {
return n / i;
}
}
}
 
fn main() {
println!("The largest proper divisors for numbers in the interval [1, 100] are:");
print!(" 1 ");
for n in 2..=100 {
if n % 2 == 0 {
print!("{:2} ", n / 2);
} else {
print!("{:2} ", largest_proper_divisor(n));
}
if n % 10 == 0 {
println!();
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
The largest proper divisors for numbers in the interval [1, 100] are:
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
 
{{trans|C}}
 
<syntaxhighlight lang="rust">
fn lpd(n: u32) -> u32 {
if n <= 1 {
1
} else {
(1..n).rev().find(|&i| n % i == 0).unwrap_or(1)
}
}
 
fn main() {
for i in 1..=100 {
print!("{:3}", lpd(i));
 
if i % 10 == 0 {
println!();
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|Ruby}}==
Line 1,806 ⟶ 2,499:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">1..100 -> map {|n| proper_divisors(n).tail \\ 1 }.slices(10).each {|a|
a.map{ '%3s' % _ }.join(' ').say
}</syntaxhighlight>
{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
7 11 1 12 5 13 9 14 1 15
1 16 11 17 7 18 1 19 13 20
1 21 1 22 15 23 1 24 7 25
17 26 1 27 11 28 19 29 1 30
1 31 21 32 13 33 1 34 23 35
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50
</pre>
 
Line 1,848 ⟶ 2,559:
 
 
=={{header|True BASICTI-57}}==
TI-57 calculators could display only one number at a time. The program below pauses almost a second to show the largest proper divisor of each integer between 1 and 100 - which would take 6 or 7 minutes on a genuine machine - then displays <code>0</code> and stops.
{{trans|FreeBASIC}}
{| class="wikitable"
<syntaxhighlight lang="qbasic">PRINT "El mayor divisor propio de n es:"
! Machine code
PRINT
! Comment
PRINT " 1 1";
|-
FOR i = 3 To 100
|
FOR j = i-1 To 1 Step -1
Lbl 0
IF remainder(i, j) = 0 Then
1
PRINT Using$("###", j);
STO 0
EXIT FOR
Lbl 1
END IF
RCL 0
NEXT j
SBR 9
IF remainder(i, 10) = 0 Then PRINT
Pause
NEXT i
1
END</syntaxhighlight>
SUM 0
{{out}}
100
<pre>
x⮂t
Igual que la entrada de FreeBASIC.
RCL 0
</pre>
INV x≥t
GTO 1
CLR
R/S
RST
Lbl 9
x⮂t
1
x⮂t
x=t
INV SBR
STO 1
STO 2
C.t
Lbl 8
1
INV SUM 2
RCL 1
/
RCL 2
-
CE
Int
=
INV x=t
GTO 8
RCL 2
INV SBR
|
program task()
r0 = 1
loop
get a(r0)
display a(r0)
r0 += 1
t = 100
while r0 < t
end loop
clear display
end program
subroutine a(x)
t = 1
if x = t
return x
r1 = x
r2 = x
t = 0
loop
r2 -= 1
get r1
get r1/r2
get int(r1/r2)
get mod(r1,r2)
while mod(r1,r2) <> 0
end loop
return r2
end subroutine
|}
 
=={{header|V (Vlang)}}==
Line 1,912 ⟶ 2,693:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
System.print("The largest proper divisors for numbers in the interval [1, 100] are:")
Line 1,927 ⟶ 2,708:
 
{{out}}
<pre>The largest proper divisors for numbers in the interval [1, 100] are:
<pre>
The largest proper divisors for numbers in the interval [1, 100] are:
1 1 1 2 1 3 1 4 3 5
1 6 1 7 5 8 1 9 1 10
Line 1,938 ⟶ 2,718:
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50 </pre>
</pre>
 
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">print "El mayor divisor propio de n es:\n"
print " 1 1 ";
for i = 3 to 100
for j = i-1 to 1 step -1
If mod(i, j) = 0 then print j using "##"; : break : fi
next j
if mod(i, 10) = 0 then print : fi
next i
print
end</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
 
=={{header|X86 Assembly}}==
9,476

edits