Largest proper divisor of n: Difference between revisions

m
(→‎{{header|Python}}: Added a functional variant which reduces the search space and formats more flexibly.)
m (→‎{{header|Wren}}: Minor tidy)
 
(87 intermediate revisions by 46 users not shown)
Line 1:
{{Draft taskTask}}
 
;Task:a(1) = 1; for n > 1, a(n) = '''largest''' proper divisor of n, where '''n < 101 '''.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F lpd(n)
L(i) (n - 1 .< 0).step(-1)
I n % i == 0
R i
R 1
 
L(i) 1..100
print(‘#3’.format(lpd(i)), end' I i % 10 == 0 {"\n"} E ‘’)</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|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
 
procedure Main is
subtype param_type is Integer range 1 .. 100;
function lpd (n : in param_type) return param_type is
result : param_type := 1;
begin
for divisor in reverse 1 .. n / 2 loop
if n rem divisor = 0 then
result := divisor;
exit;
end if;
end loop;
return result;
end lpd;
 
begin
for I in param_type loop
Put (Item => lpd (I), Width => 3);
if I rem 10 = 0 then
New_Line;
end if;
end loop;
end Main;</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|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">FOR n TO 100 DO # show the largest proper divisors for n = 1..100 #
INT largest proper divisor := 1;
FOR j FROM ( n OVER 2 ) BY -1 TO 2 WHILE largest proper divisor = 1 DO
Line 15 ⟶ 81:
IF n MOD 10 = 0 THEN print( ( newline ) ) FI
OD
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 31 ⟶ 97:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">for n := 1 until 100 do begin % show the largest proper divisors for n = 1..100 %
for j := n div 2 step -1 until 2 do begin
if n rem j = 0 then begin
Line 41 ⟶ 107:
foundLargestProperDivisor:
if n rem 10 = 0 then write()
end for_n.</langsyntaxhighlight>
{{out}}
<pre>
Line 58 ⟶ 124:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">(⌈/1,(⍸0=¯1↓⍳|⊢))¨10 10⍴⍳100</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 70 ⟶ 136:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|AppleScript}}==
Most of this code is just to prepare the output for display. :D
 
<syntaxhighlight lang="applescript">on largestProperDivisor(n)
if (n mod 2 = 0) then return n div 2
if (n mod 3 = 0) then return n div 3
if (n < 5) then return missing value
repeat with i from 5 to (n ^ 0.5 div 1) by 6
if (n mod i = 0) then return n div i
if (n mod (i + 2) = 0) then return n div (i + 2)
end repeat
return 1
end largestProperDivisor
 
on task(max)
script o
property LPDs : {}
property output : {}
end script
set w to (count (max as text)) * 2 + 3
set padding to text 1 thru (w - 2) of " "
set end of o's LPDs to "1:1" & padding
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set c to 1
repeat with n from 2 to max
set end of o's LPDs to text 1 thru w of ((n as text) & ":" & largestProperDivisor(n) & padding)
set c to c + 1
if (c mod 10 is 0) then
set end of o's output to o's LPDs as text
set o's LPDs to {}
end if
end repeat
if (c mod 10 > 0) then set end of o's output to o's LPDs as text
set AppleScript's text item delimiters to linefeed
set o's output to o's output as text
set AppleScript's text item delimiters to astid
return o's output
end task
 
task(100)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"1:1 2:1 3:1 4:2 5:1 6:3 7:1 8:4 9:3 10:5
11:1 12:6 13:1 14:7 15:5 16:8 17:1 18:9 19:1 20:10
21:7 22:11 23:1 24:12 25:5 26:13 27:9 28:14 29:1 30:15
31:1 32:16 33:11 34:17 35:7 36:18 37:1 38:19 39:13 40:20
41:1 42:21 43:1 44:22 45:15 46:23 47:1 48:24 49:7 50:25
51:17 52:26 53:1 54:27 55:11 56:28 57:19 58:29 59:1 60:30
61:1 62:31 63:21 64:32 65:13 66:33 67:1 68:34 69:23 70:35
71:1 72:36 73:1 74:37 75:25 76:38 77:11 78:39 79:1 80:40
81:27 82:41 83:1 84:42 85:17 86:43 87:29 88:44 89:1 90:45
91:13 92:46 93:31 94:47 95:19 96:48 97:1 98:49 99:33 100:50 "</syntaxhighlight>
 
 
===Functional===
 
Composing functionally, for rapid drafting and refactoring, with higher levels of code reuse:
 
<syntaxhighlight lang="applescript">use framework "Foundation"
 
 
--------------- LARGEST PROPER DIVISOR OF N --------------
 
-- maxProperDivisors :: Int -> Int
on maxProperDivisors(n)
script p
on |λ|(x)
0 = n mod x
end |λ|
end script
set mb to find(p, enumFromTo(2, sqrt(n)))
if missing value is mb then
1
else
n div mb
end if
end maxProperDivisors
 
 
--------------------------- TEST -------------------------
on run
set xs to map(maxProperDivisors, enumFromTo(1, 100))
set w to length of (maximum(xs) as string)
unlines(map(unwords, ¬
chunksOf(10, ¬
map(compose(justifyRight(w, space), str), xs))))
end run
 
 
------------------------- GENERIC ------------------------
 
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(k, xs)
script
on go(ys)
set ab to splitAt(k, ys)
set a to item 1 of ab
if {} ≠ a then
{a} & go(item 2 of ab)
else
a
end if
end go
end script
result's go(xs)
end chunksOf
 
 
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
on |λ|(x)
mf's |λ|(mg's |λ|(x))
end |λ|
end script
end compose
 
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
lst
else
{}
end if
end enumFromTo
 
 
-- find :: (a -> Bool) -> [a] -> (a | missing value)
on find(p, xs)
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return item i of xs
end repeat
missing value
end tell
end find
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
script
on |λ|(strText)
if n > length of strText then
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
else
strText
end if
end |λ|
end script
end justifyRight
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- maximum :: Ord a => [a] -> a
on maximum(xs)
set ca to current application
unwrap((ca's NSArray's arrayWithArray:(xs))'s ¬
valueForKeyPath:"@max.self")
end maximum
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> String -> String
on replicate(n, s)
-- Egyptian multiplication - progressively doubling a list,
-- appending stages of doubling to an accumulator where needed
-- for binary assembly of a target length
script p
on |λ|({n})
n ≤ 1
end |λ|
end script
script f
on |λ|({n, dbl, out})
if (n mod 2) > 0 then
set d to out & dbl
else
set d to out
end if
{n div 2, dbl & dbl, d}
end |λ|
end script
set xs to |until|(p, f, {n, s, ""})
item 2 of xs & item 3 of xs
end replicate
 
 
-- splitAt :: Int -> [a] -> ([a], [a])
on splitAt(n, xs)
if n > 0 and n < length of xs then
if class of xs is text then
{items 1 thru n of xs as text, ¬
items (n + 1) thru -1 of xs as text}
else
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
end if
else
if n < 1 then
{{}, xs}
else
{xs, {}}
end if
end if
end splitAt
 
 
-- sqrt :: Num -> (Num | missing value)
on sqrt(n)
if 0 ≤ n then
n ^ (1 / 2)
else
missing value
end if
end sqrt
 
 
-- str :: a -> String
on str(x)
x as string
end str
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set v to x
set mp to mReturn(p)
set mf to mReturn(f)
repeat until mp's |λ|(v)
set v to mf's |λ|(v)
end repeat
v
end |until|
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords
 
 
-- unwrap :: NSValue -> a
on unwrap(nsValue)
if nsValue is missing value then
missing value
else
set ca to current application
item 1 of ((ca's NSArray's arrayWithObject:nsValue) as list)
end if
end unwrap</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|Arturo}}==
 
<syntaxhighlight lang="rebol">loop split.every:10 [1] ++ map 2..100 'x -> last chop factors x 'row [
print map to [:string] row 'r -> pad r 5
]</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|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LARGEST_PROPER_DIVISOR_OF_N.AWK
# converted from C
BEGIN {
start = 1
stop = 100
for (i=start; i<=stop; i++) {
printf("%3d%1s",largest_proper_divisor(i),++count%10?"":"\n")
}
printf("\nLargest proper divisor of n %d-%d\n",start,stop)
exit(0)
}
function largest_proper_divisor(n, i) {
if (n <= 1) {
return(1)
}
for (i=n-1; i>0; i--) {
if (n % i == 0) {
return(i)
}
}
}
</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
 
Largest proper divisor of n 1-100</pre>
 
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
<lang basic>10 DEFINT A-Z
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR I=1 TO 100
30 IF I=1 THEN PRINT " 1";: GOTO 70
Line 79 ⟶ 526:
60 NEXT J
70 IF I MOD 10=0 THEN PRINT
80 NEXT I</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 91 ⟶ 538:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
==={{header|ANSI 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"
print " 1 1 ";
for i = 3 to 100
for j = i-1 to 1 step -1
If i % j = 0 then
print " "; j; " ";
exit for
end if
next j
if i % 10 = 0 then print
next i
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}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let lpd(n) = valof
Line 103 ⟶ 934:
$( writed(lpd(i), 3)
if i rem 10=0 then wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 115 ⟶ 946:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">(1⌈´↕×0=↕|⊢)¨ ∘‿10⥊1+↕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|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
unsigned int lpd(unsigned int n) {
Line 133 ⟶ 980:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 145 ⟶ 992:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cassert>
#include <iomanip>
#include <iostream>
 
int 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;
}
 
int main() {
for (int n = 1; n < 101; ++n) {
std::cout << std::setw(2) << largest_proper_divisor(n)
<< (n % 10 == 0 ? '\n' : ' ');
}
}</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|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub print3(n: uint8) is
Line 180 ⟶ 1,064:
end if;
i := i + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 192 ⟶ 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}}==
<syntaxhighlight lang="draco">proc nonrec lpd(word n) word:
word d;
if n=1 then
1
else
d := n-1;
while n % d /= 0 do d := d-1 od;
d
fi
corp
 
proc nonrec main() void:
word n;
for n from 1 upto 100 do
write(lpd(n):3);
if n%10 = 0 then writeln() fi
od
corp</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|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#}}==
<syntaxhighlight lang="fsharp">
// Largest proper divisor of n: Nigel Galloway. June 2nd., 2021
let fN g=let rec fN n=let i=Seq.head n in match(g/i,g%i) with (1,_)->1 |(n,0)->n |_->fN(Seq.tail n) in fN(Seq.initInfinite((+)2))
seq{yield 1; yield! seq{2..100}|>Seq.map fN}|>Seq.iter(printf "%d "); printfn ""
</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
Real: 00:00:00.015
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math math.bitwise math.functions
math.ranges prettyprint sequences ;
 
Line 204 ⟶ 1,263:
: largest ( m -- n ) dup odd? [ odd ] [ 2/ ] if ;
 
100 [1,b] [ largest ] map 10 group simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 216 ⟶ 1,275:
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|Fermat}}==
<syntaxhighlight lang="fermat">Func Lpd(n) =
if n = 1 then Return(1) fi;
for i = n\2 to 1 by -1 do
if n|i = 0 then Return(i) fi;
od.;
 
for m = 1 to 100 do
!(Lpd(m):4);
if m|10=0 then ! fi;
od;</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|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 F N=1,100;D 2;D 3
01.20 Q
 
Line 234 ⟶ 1,317:
03.20 S A=N/10
03.30 I (FITR(A)-A)3.4;T !
03.40 R</langsyntaxhighlight>
{{out}}
<pre>= 1= 1= 1= 2= 1= 3= 1= 4= 3= 5
Line 246 ⟶ 1,329:
= 27= 41= 1= 42= 17= 43= 29= 44= 1= 45
= 13= 46= 31= 47= 19= 48= 1= 49= 33= 50</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">: largest-proper-divisor { n -- n }
n 1 and 0= if n 2/ exit then
3
begin
dup dup * n <=
while
dup n swap /mod swap
0= if nip exit else drop then
2 +
repeat drop 1 ;
 
: main
101 1 do
i largest-proper-divisor 2 .r
i 10 mod 0= if cr else space then
loop ;
 
main
bye</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|Fortran}}==
<langsyntaxhighlight lang="fortran"> program LargestProperDivisors
implicit none
integer i, lpd
Line 266 ⟶ 1,385:
20 lpd = i
end if
end function</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 278 ⟶ 1,397:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|Frink}}==
Frink contains efficient routines for factoring numbers. It uses trial division, wheel factoring, and Pollard rho factoring.
 
<syntaxhighlight lang="frink">for n = 1 to 100
println[last[allFactors[n,true,false]]]</syntaxhighlight>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import "fmt"
 
func largestProperDivisor(n int) int {
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return n / i
}
}
return 1
}
 
func main() {
fmt.Println("The largest proper divisors for numbers in the interval [1, 100] are:")
fmt.Print(" 1 ")
for n := 2; n <= 100; n++ {
if n%2 == 0 {
fmt.Printf("%2d ", n/2)
} else {
fmt.Printf("%2d ", largestProperDivisor(n))
}
if n%10 == 0 {
fmt.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>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
import Text.Printf (printf)
 
Line 290 ⟶ 1,459:
main =
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . lpd <$> [1 .. 100]</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 309 ⟶ 1,478:
(Otherwise, the largest proper divisor will be 1 itself).
 
<langsyntaxhighlight lang="haskell">import Data.List (find)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
Line 324 ⟶ 1,493:
main =
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . maxProperDivisors <$> [1 .. 100]</langsyntaxhighlight>
 
<pre> 1 1 1 2 1 3 1 4 3 5
Line 336 ⟶ 1,505:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">lpd=: }.&.q:</syntaxhighlight>
{{out}}
<pre>
lpd >: i. 5 20
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>
 
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.
 
=={{header|Java}}==
<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}}==
'''Works with jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
Naive version:
<syntaxhighlight lang="jq"># (1|largestpd) is 1 as per the task definition
def largestpd:
if . == 1 then 1
else . as $n
| first( range( ($n - ($n % 2)) /2; 0; -1) | (select($n % . == 0) ))
end;</syntaxhighlight>
 
Slightly less naive:
<syntaxhighlight lang="jq">def largestpd:
if . == 1 then 1
else . as $n
| (first(2,3,5,7 | select($n % . == 0)) // null) as $div
| if $div then $n/$div
else first( range( ($n - ($n % 11)) /11; 0; -1) | (select($n % . == 0) ))
end
end;</syntaxhighlight>
<syntaxhighlight lang="jq"># For neatness
def lpad($len):
tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def nwise($n):
def w: if length <= $n then . else .[:$n], (.[$n:]|w) end;
w;
### The task
[range(1; 101) | largestpd]
| nwise(10) | map(lpad(2)) | join(" ")</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|Julia}}==
<langsyntaxhighlight lang="julia">largestpd(n) = (for i in n÷2:-1:1 (n % i == 0) && return i; end; 1)
 
foreach(n -> print(rpad(largestpd(n), 3), n % 10 == 0 ? "\n" : ""), 1:100)
</langsyntaxhighlight>{{out}}
<pre>
1 1 1 2 1 3 1 4 3 5
Line 353 ⟶ 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>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
Line 372 ⟶ 1,673:
VECTOR VALUES TABLE = $10(I3)*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 384 ⟶ 1,685:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Last[Prepend[DeleteCases[Divisors[#], #], 1]] & /@ Range[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|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}}==
<syntaxhighlight lang="modula2">MODULE LargestProperDivisor;
FROM InOut IMPORT WriteCard, WriteLn;
 
VAR i: CARDINAL;
 
PROCEDURE lpd(n: CARDINAL): CARDINAL;
VAR i: CARDINAL;
BEGIN
IF n=1 THEN
RETURN 1;
END;
 
FOR i := n DIV 2 TO 1 BY -1 DO
IF n MOD i = 0 THEN
RETURN i;
END;
END;
END lpd;
 
BEGIN
FOR i := 1 TO 100 DO
WriteCard(lpd(i), 3);
IF i MOD 10 = 0 THEN
WriteLn();
END;
END;
END LargestProperDivisor.</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|Nim}}==
<syntaxhighlight lang="nim">import math, strutils
 
func largestProperDivisor(n: Positive): int =
for d in 2..sqrt(float(n)).int:
if n mod d == 0: return n div d
result = 1
 
for n in 1..100:
stdout.write ($n.largestProperDivisor).align(2), if n mod 10 == 0: '\n' else: ' '</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|Pascal}}==
<langsyntaxhighlight lang="pascal">
program LarPropDiv;
 
Line 415 ⟶ 1,795:
Writeln;
end;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 428 ⟶ 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}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'divisors';
use List::AllUtils <max natatime>;
 
sub proper_divisors {
my $n = shift;
return 1 if $n == 0;
my @d = divisors($n);
pop @d;
@d;
}
 
my @range = 1 .. 100;
print "GPD for $range[0] through $range[-1]:\n";
my $iter = natatime 10, @range;
while( my @batch = $iter->() ) {
printf '%3d', $_ == 1 ? 1 : max proper_divisors($_) for @batch; print "\n";
}</syntaxhighlight>
{{out}}
<pre>GPD for 1 through 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|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}),</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 445 ⟶ 1,936:
13 46 31 47 19 48 1 49 33 50
</pre>
Alternative, same output, optimised: obviously checking for a factor from 2 up is going to be significantly faster than n-1 down... or of course as above collecting all of them and extracting/discarding all but the last, at least on much larger numbers, that is, and I suppose you could (perhaps) improve even further on this by only checking primes.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
Line 462 ⟶ 1,953:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
foreach(I in 1..100)
printf("%2d%s",a(I), cond(I mod 10 == 0,"\n", " "))
end.
 
a(1) = 1.
a(N) = Div =>
a(N,N//2,Div).
a(N,I,I) :-
N mod I == 0.
a(N,I,Div) :-
a(N,I-1,Div).</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|PL/I}}==
<syntaxhighlight lang="pli">largestProperDivisor: procedure options(main);
lpd: procedure(n) returns(fixed);
declare (n, i) fixed;
if n <= 1 then return(1);
do i=n-1 repeat(i-1) while(i>=1);
if mod(n,i)=0 then return(i);
end;
end lpd;
declare i fixed;
do i=1 to 100;
put edit(lpd(i)) (F(3));
if mod(i,10)=0 then put skip;
end;
end largestProperDivisor;</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|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 496 ⟶ 2,042:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 510 ⟶ 2,056:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def lpd(n):
for i in range(n-1,0,-1):
if n%i==0: return i
Line 516 ⟶ 2,062:
 
for i in range(1,101):
print("{:3}".format(lpd(i)), end=i%10==0 and '\n' or '')</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 532 ⟶ 2,078:
Or, reducing the search space, formatting more flexibly (to allow for experiments with larger ranges) and composing functionally:
 
<langsyntaxhighlight lang="python">'''Largest proper divisor of'''
 
from math import isqrt
Line 603 ⟶ 2,149:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 1 1 1 2 1 3 1 4 3 5
Line 615 ⟶ 2,161:
27 41 1 42 17 43 29 44 1 45
13 46 31 47 19 48 1 49 33 50</pre>
 
=={{header|Quackery}}==
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
 
<syntaxhighlight lang="quackery">' [ 1 ] 99 times
[ i^ 2 + factors
-2 peek join ]
echo</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|R}}==
<syntaxhighlight lang="r">largest_proper_divisor <- function(n){
if(n == 1) return(1)
lpd = 1
for(i in seq(1, n-1, 1)){
if(n %% i == 0)
lpd = i
}
message(paste0("The largest proper divisor of ", n, " is ", lpd))
return(lpd)
}
 
#Verify
for (i in 1:100){ #about 10 seconds to calculate until 10000
largest_proper_divisor(i)
}
</syntaxhighlight>
 
{{out}}
<pre>
The largest proper divisor of 2 is 1
The largest proper divisor of 3 is 1
The largest proper divisor of 4 is 2
The largest proper divisor of 5 is 1
The largest proper divisor of 6 is 3
The largest proper divisor of 7 is 1
The largest proper divisor of 8 is 4
The largest proper divisor of 9 is 3
.
. blah blah blah
.
The largest proper divisor of 94 is 47
The largest proper divisor of 95 is 19
The largest proper divisor of 96 is 48
The largest proper divisor of 97 is 1
The largest proper divisor of 98 is 49
The largest proper divisor of 99 is 33
The largest proper divisor of 100 is 50
</pre>
 
=={{header|Raku}}==
Line 620 ⟶ 2,221:
 
Note: this example is ridiculously overpowered (and so, somewhat inefficient) for a range of 1 to 100, but will easily handle large numbers without modification.
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
for 0, 2**67 - 1 -> $add {
Line 629 ⟶ 2,230:
say "GPD for {$range.min} through {$range.max}:";
 
say ( $range.hyper(:18batch14batch).map: {$_ == 1 ?? 1 !! $_ %% 2 ?? $_ / 2 !! .&proper-divisors.sort.tailmax} )\
.batch(10)».fmt("%{$add.chars + 1}d").join: "\n";
 
say (now - $start).fmt("%0.3f seconds\n");
}</langsyntaxhighlight>
{{out}}
<pre>GPD for 1 through 100:
Line 662 ⟶ 2,263:
 
=={{header|REXX}}==
Logic was added to the &nbsp; '''LPDIV''' &nbsp; function so that for &nbsp; odd &nbsp; numbers, &nbsp; only odd divisors were used.
<lang rexx>/*REXX program finds the largest proper divisors of all numbers (up to a given limit). */
 
This addition made it about &nbsp; '''75%''' &nbsp; faster.
<syntaxhighlight lang="rexx">/*REXX program finds the largest proper divisors of all numbers (up to a given limit). */
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 101 /*Not specified? Then use the default.*/
Line 683 ⟶ 2,287:
/*──────────────────────────────────────────────────────────────────────────────────────*/
LPDIV: procedure; parse arg x; if x<4 then return 1 /*obtain X; test if X < 4. */
odd= x // 2; do kbeg= x % 2 by -1 /*startuse atBEG halfwayas pointthe andfirst decreasedivisor. */
if odd then if xbeg//k2==0 then returnbeg= kbeg - 1 /*NoIs X remainderodd? GotThen largestmake properBEG divodd.*/
end do k=beg by -1-odd /*kbegin at halfway point and decrease. */
if x//k==0 then return 1 k /*theRemainder=0? number Got Xlargest proper is a primediv. */</lang>
end /*k*/
return 1 /*If we get here, then X is a prime.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 705 ⟶ 2,311:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">? "working..."
<lang ring>
see "working..." + nl
see "Largest proper divisor of n are:" + nl
see "1 "
row = 1
limit = 100
? "Largest proper divisor up to " + limit + " are:"
 
see " 1 "
col = 1
for n = 2 to limit
for m = 1 to n-1 >> 1
if n % m = 0 div = m ok
div = m
ok
next
rowif =div row< +10 1see " " ok
see "" + div + " "
if rowcol++ % 10 = 0 ? "" ok
see nl
ok
next
? "done..."</syntaxhighlight>
{{out}}
<pre>working...
Largest proper divisor up to 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
done...</pre>
 
=={{header|RPL}}==
see "done..." + nl
≪ '''IF''' DUP 1 ≠ '''THEN''' DUP '''DO''' 1 - '''UNTIL''' DUP2 MOD NOT '''END''' SWAP DROP '''END''' ≫ '<span style="color:blue">LPROPDIV</span>' STO
</lang>
≪ { } 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 }
working...
</pre>
Largest proper divisor of n are:
 
1 1 1 2 1 3 1 4 3 5
=={{header|Rust}}==
1 6 1 7 5 8 1 9 1 10
 
7 11 1 12 5 13 9 14 1 15
{{trans|Go}}
1 16 11 17 7 18 1 19 13 20
 
1 21 1 22 15 23 1 24 7 25
<syntaxhighlight lang="rust">
17 26 1 27 11 28 19 29 1 30
fn largest_proper_divisor(n: i32) -> i32 {
1 31 21 32 13 33 1 34 23 35
for i in 2..=(n as f64).sqrt() as i32 {
1 36 1 37 25 38 11 39 1 40
27 41 1 42 17 43 29 44 1if 45n % i == 0 {
13 46 31 47 19 48 1 49 33 50 return n / i;
}
done...
}
}
 
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}}==
<syntaxhighlight lang="ruby">require 'prime'
 
def a(n)
return 1 if n == 1 || n.prime?
(n/2).downto(1).detect{|d| n.remainder(d) == 0}
end
 
(1..100).map{|n| a(n).to_s.rjust(3)}.each_slice(10){|slice| puts slice.join}
</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|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func integer: largestProperDivisor (in integer: number) is func
result
var integer: divisor is 0;
begin
if not odd(number) then
divisor := number >> 1;
else
divisor := number div 3 - 1;
if odd(divisor) then
divisor +:= 2;
else
incr(divisor);
end if;
while number rem divisor <> 0 do
divisor -:= 2;
end while;
end if;
end func;
 
const proc: main is func
local
var integer: n is 0;
begin
for n range 1 to 100 do
write(largestProperDivisor(n) lpad 3);
if n rem 10 = 0 then
writeln;
end if;
end for;
end func;</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|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>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func largestProperDivisor(_ n : Int) -> Int? {
guard n > 0 else {
return nil
}
if (n & 1) == 0 {
return n >> 1
}
var p = 3
while p * p <= n {
if n % p == 0 {
return n / p
}
p += 2
}
return 1
}
 
for n in (1..<101) {
print(String(format: "%2d", largestProperDivisor(n)!),
terminator: n % 10 == 0 ? "\n" : " ")
}</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|TI-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.
{| class="wikitable"
! Machine code
! Comment
|-
|
Lbl 0
1
STO 0
Lbl 1
RCL 0
SBR 9
Pause
1
SUM 0
100
x⮂t
RCL 0
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)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn largest_proper_divisor(n int) int {
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return n / i
}
}
return 1
}
fn main() {
println("The largest proper divisors for numbers in the interval [1, 100] are:")
print(" 1 ")
for n := 2; n <= 100; n++ {
if n%2 == 0 {
print("${n/2:2} ")
} else {
print("${largest_proper_divisor(n):2} ")
}
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>
 
Line 747 ⟶ 2,693:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
System.print("The largest proper divisors for numbers in the interval [1, 100] are:")
Line 759 ⟶ 2,705:
}
if (n % 10 == 0) System.print()
}</langsyntaxhighlight>
 
{{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 773 ⟶ 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|X86 Assembly}}==
<langsyntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
Line 830 ⟶ 2,774:
52
53 end start
</syntaxhighlight>
</lang>
 
{{out}}
Line 847 ⟶ 2,791:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int N, D;
[for N:= 1 to 100 do
[D:= if N=1 then 1 else N/2;
Line 855 ⟶ 2,799:
if rem(N/10) = 0 then CrLf(0) else ChOut(0, ^ );
];
]</langsyntaxhighlight>
 
{{out}}
9,482

edits