Largest proper divisor of n
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
- a(1) = 1; for n > 1, a(n) = largest proper divisor of n, where n < 101 .
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 ‘’)
- Output:
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
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;
- Output:
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
ALGOL 68
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
IF n MOD j = 0 THEN
largest proper divisor := j
FI
OD;
print( ( whole( largest proper divisor, -3 ) ) );
IF n MOD 10 = 0 THEN print( ( newline ) ) FI
OD
- Output:
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
ALGOL W
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
writeon( i_w := 3, s_w := 0, j );
goto foundLargestProperDivisor
end if_n_rem_j_eq_0
end for_j;
writeon( i_w := 3, s_w := 0, 1 );
foundLargestProperDivisor:
if n rem 10 = 0 then write()
end for_n.
- Output:
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
APL
(⌈/1,(⍸0=¯1↓⍳|⊢))¨10 10⍴⍳100
- Output:
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
AppleScript
Most of this code is just to prepare the output for display. :D
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)
- Output:
"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 "
Functional
Composing functionally, for rapid drafting and refactoring, with higher levels of code reuse:
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
- Output:
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
Arturo
loop split.every:10 [1] ++ map 2..100 'x -> last chop factors x 'row [
print map to [:string] row 'r -> pad r 5
]
- Output:
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
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)
}
}
}
- Output:
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
BASIC
10 DEFINT A-Z
20 FOR I=1 TO 100
30 IF I=1 THEN PRINT " 1";: GOTO 70
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
- Output:
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
ANSI 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
- Output:
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
Applesoft BASIC
Solution Quite BASIC work without changes.
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
Chipmunk Basic
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
Craft 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
- Output:
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
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
- Output:
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
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
- Output:
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
GW-BASIC
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
Gambas
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
Minimal BASIC
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
MSX Basic
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
Palo Alto Tiny 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
- Output:
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
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
- Output:
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
Quite BASIC
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
Run BASIC
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
Tiny BASIC
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
True BASIC
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
- Output:
Same as FreeBASIC entry.
XBasic
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
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
- Output:
Same as FreeBASIC entry.
BCPL
get "libhdr"
let lpd(n) = valof
for i = n<=1 -> 1, n-1 to 1 by -1
if n rem i=0 resultis i
let start() be
for i=1 to 100
$( writed(lpd(i), 3)
if i rem 10=0 then wrch('*N')
$)
- Output:
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
BQN
(1⌈´↕×0=↕|⊢)¨ ∘‿10⥊1+↕100
- Output:
┌─ ╵ 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 ┘
C
#include <stdio.h>
unsigned int lpd(unsigned int n) {
if (n<=1) return 1;
int i;
for (i=n-1; i>0; i--)
if (n%i == 0) return i;
}
int main() {
int i;
for (i=1; i<=100; i++) {
printf("%3d", lpd(i));
if (i % 10 == 0) printf("\n");
}
return 0;
}
- Output:
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
C++
#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' : ' ');
}
}
- Output:
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
Cowgol
include "cowgol.coh";
sub print3(n: uint8) is
print_char(' ');
if n>9 then
print_char('0' + n/10);
else
print_char(' ');
end if;
print_char('0' + n%10);
end sub;
sub lpd(n: uint8): (r: uint8) is
if n <= 1 then
r := 1;
else
r := n - 1;
while r > 0 loop
if n % r == 0 then
break;
end if;
r := r - 1;
end loop;
end if;
end sub;
var i: uint8 := 1;
while i <= 100 loop
print3(lpd(i));
if i%10 == 0 then
print_nl();
end if;
i := i + 1;
end loop;
- Output:
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
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();
}
}
}
- Output:
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
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" : " ");
}
}
- Output:
Same as FreeBASIC entry.
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;
- Output:
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
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
- Output:
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
EasyLang
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 & " "
.
F#
// 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 ""
- Output:
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
Factor
USING: grouping kernel math math.bitwise math.functions
math.ranges prettyprint sequences ;
: odd ( m -- n )
dup 3 /i 1 - next-odd 1 -2 <range>
[ divisor? ] with find nip ;
: largest ( m -- n ) dup odd? [ odd ] [ 2/ ] if ;
100 [1,b] [ largest ] map 10 group simple-table.
- Output:
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
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;
- Output:
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
FOCAL
01.10 F N=1,100;D 2;D 3
01.20 Q
02.10 S V=1
02.20 I (1-N)2.3;R
02.30 S V=N-1
02.40 S A=N/V
02.50 I (FITR(A)-A)2.6;R
02.60 S V=V-1
02.70 G 2.4
03.10 T %2,V
03.20 S A=N/10
03.30 I (FITR(A)-A)3.4;T !
03.40 R
- Output:
= 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
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
- Output:
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
Fortran
program LargestProperDivisors
implicit none
integer i, lpd
do 10 i=1, 100
write (*,'(I3)',advance='no') lpd(i)
10 if (i/10*10 .eq. i) write (*,*)
end program
integer function lpd(n)
implicit none
integer n, i
if (n .le. 1) then
lpd = 1
else
do 10 i=n-1, 1, -1
10 if (n/i*i .eq. n) goto 20
20 lpd = i
end if
end function
- Output:
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
Frink
Frink contains efficient routines for factoring numbers. It uses trial division, wheel factoring, and Pollard rho factoring.
for n = 1 to 100
println[last[allFactors[n,true,false]]]
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()
}
}
}
- Output:
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
Haskell
import Data.List.Split (chunksOf)
import Text.Printf (printf)
lpd :: Int -> Int
lpd 1 = 1
lpd n = head [x | x <- [n -1, n -2 .. 1], n `mod` x == 0]
main :: IO ()
main =
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . lpd <$> [1 .. 100]
- Output:
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
Or, considering the two smallest proper divisors:
(If there are two, then the largest proper divisor will be N divided by the first proper divisor that is not 1)
(Otherwise, the largest proper divisor will be 1 itself).
import Data.List (find)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
maxProperDivisors :: Int -> Int
maxProperDivisors n =
case find ((0 ==) . rem n) [2 .. root] of
Nothing -> 1
Just x -> quot n x
where
root = (floor . sqrt) (fromIntegral n :: Double)
main :: IO ()
main =
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . maxProperDivisors <$> [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
J
lpd=: }.&.q:
- Output:
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
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.
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;
}
}
- Output:
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
jq
Works with jq
Works with gojq, the Go implementation of jq
Naive version:
# (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;
Slightly less naive:
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;
# 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(" ")
- Output:
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
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)
- Output:
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
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
- Output:
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
MAD
NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
ENTRY TO LPD.
WHENEVER N.LE.1, FUNCTION RETURN 1
THROUGH TEST, FOR D=N-1, -1, D.L.1
TEST WHENEVER N/D*D.E.N, FUNCTION RETURN D
END OF FUNCTION
THROUGH SHOW, FOR I=1, 10, I.GE.100
SHOW PRINT FORMAT TABLE,
0 LPD.(I), LPD.(I+1), LPD.(I+2), LPD.(I+3),
1 LPD.(I+4), LPD.(I+5), LPD.(I+6), LPD.(I+7),
2 LPD.(I+8), LPD.(I+9)
VECTOR VALUES TABLE = $10(I3)*$
END OF PROGRAM
- Output:
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
Mathematica / Wolfram Language
Last[Prepend[DeleteCases[Divisors[#], #], 1]] & /@ Range[100]
- Output:
{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}
Maxima
lpd(n):=if n=1 then 1 else listify(divisors(n))[length(divisors(n))-1]$
makelist(lpd(i),i,100);
- Output:
[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]
Modula-2
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.
- Output:
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
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: ' '
- Output:
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
Pascal
program LarPropDiv;
function LargestProperDivisor(n:NativeInt):NativeInt;
//searching upwards to save time for example 100
//2..sqrt(n) aka 1..10 instead downwards n..sqrt(n) 100..10
var
i,j: NativeInt;
Begin
i := 2;
repeat
If n Mod i = 0 then
Begin
LargestProperDivisor := n DIV i;
EXIT;
end;
inc(i);
until i*i > n;
LargestProperDivisor := 1;
end;
var
n : Uint32;
begin
for n := 1 to 100 do
Begin
write(LargestProperDivisor(n):4);
if n mod 10 = 0 then
Writeln;
end;
end.
- Output:
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
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).
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.
- Output:
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
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";
}
- Output:
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
Phix
puts(1,join_by(apply(true,sprintf,{{"%3d"},vslice(apply(apply(true,factors,{tagset(100),-1}),reverse),1)}),1,10,""))
- Output:
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
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.
with javascript_semantics for n=1 to 100 do integer p = 2, lim = floor(sqrt(n)), hf = 1 while p<=lim do if remainder(n,p)=0 then hf = n/p exit end if p += 1 end while printf(1,"%3d",hf) if remainder(n,10)=0 then puts(1,"\n") end if end for
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).
- Output:
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
PL/I
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;
- Output:
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
PL/M
100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PUT$CHAR: PROCEDURE (CH); DECLARE CH BYTE; CALL BDOS(2,CH); END PUT$CHAR;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
PRINT$3: PROCEDURE (N);
DECLARE N BYTE;
CALL PUT$CHAR(' ');
IF N>9
THEN CALL PUT$CHAR('0' + N/10);
ELSE CALL PUT$CHAR(' ');
CALL PUT$CHAR('0' + N MOD 10);
END PRINT$3;
LPD: PROCEDURE (N) BYTE;
DECLARE (N, I) BYTE;
IF N <= 1 THEN RETURN 1;
I = N-1;
DO WHILE I >= 1;
IF N MOD I = 0 THEN RETURN I;
I = I - 1;
END;
END LPD;
DECLARE I BYTE;
DO I=1 TO 100;
CALL PRINT$3(LPD(I));
IF I MOD 10 = 0 THEN CALL PRINT(.(13,10,'$'));
END;
CALL EXIT;
EOF
- Output:
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
Python
def lpd(n):
for i in range(n-1,0,-1):
if n%i==0: return i
return 1
for i in range(1,101):
print("{:3}".format(lpd(i)), end=i%10==0 and '\n' or '')
- Output:
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
Or, reducing the search space, formatting more flexibly (to allow for experiments with larger ranges) and composing functionally:
'''Largest proper divisor of'''
from math import isqrt
# maxProperDivisors :: Int -> Int
def maxProperDivisors(n):
'''The largest proper divisor of n.
'''
secondDivisor = find(
lambda x: 0 == (n % x)
)(
range(2, 1 + isqrt(n))
)
return 1 if None is secondDivisor else (
n // secondDivisor
)
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Test for values of n in [1..100]'''
xs = [
maxProperDivisors(n) for n
in range(1, 1 + 100)
]
colWidth = 1 + len(str(max(xs)))
print(
'\n'.join([
''.join(row) for row in
chunksOf(10)([
str(x).rjust(colWidth, " ") for x in xs
])
])
)
# ----------------------- GENERIC ------------------------
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divisible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
# find :: (a -> Bool) -> [a] -> (a | None)
def find(p):
'''Just the first element in the list that matches p,
or None if no elements match.
'''
def go(xs):
try:
return next(x for x in xs if p(x))
except StopIteration:
return None
return go
# MAIN ---
if __name__ == '__main__':
main()
- Output:
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
Quackery
factors
is defined at Factors of an integer.
' [ 1 ] 99 times
[ i^ 2 + factors
-2 peek join ]
echo
- Output:
[ 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 ]
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)
}
- Output:
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
Raku
A little odd to special case a(1) == 1 as technically, 1 doesn't have any proper divisors... but it matches OEIS A032742 so whatever.
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.
use Prime::Factor;
for 0, 2**67 - 1 -> $add {
my $start = now;
my $range = $add + 1 .. $add + 100;
say "GPD for {$range.min} through {$range.max}:";
say ( $range.hyper(:14batch).map: {$_ == 1 ?? 1 !! $_ %% 2 ?? $_ / 2 !! .&proper-divisors.max} )\
.batch(10)».fmt("%{$add.chars + 1}d").join: "\n";
say (now - $start).fmt("%0.3f seconds\n");
}
- Output:
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 0.071 seconds GPD for 147573952589676412928 through 147573952589676413027: 73786976294838206464 49191317529892137643 73786976294838206465 1 73786976294838206466 21081993227096630419 73786976294838206467 49191317529892137645 73786976294838206468 8680820740569200761 73786976294838206469 5088756985850910791 73786976294838206470 49191317529892137647 73786976294838206471 13415813871788764813 73786976294838206472 29514790517935282589 73786976294838206473 49191317529892137649 73786976294838206474 6416258808246800563 73786976294838206475 977310944302492801 73786976294838206476 49191317529892137651 73786976294838206477 29514790517935282591 73786976294838206478 87167130885810049 73786976294838206479 49191317529892137653 73786976294838206480 21081993227096630423 73786976294838206481 7767050136298758577 73786976294838206482 49191317529892137655 73786976294838206483 2784414199805215339 73786976294838206484 11351842506898185613 73786976294838206485 49191317529892137657 73786976294838206486 939961481462907089 73786976294838206487 29514790517935282595 73786976294838206488 49191317529892137659 73786976294838206489 82581954443019817 73786976294838206490 147258377885867 73786976294838206491 49191317529892137661 73786976294838206492 29514790517935282597 73786976294838206493 13415813871788764817 73786976294838206494 49191317529892137663 73786976294838206495 1 73786976294838206496 2202596307308603179 73786976294838206497 49191317529892137665 73786976294838206498 5088756985850910793 73786976294838206499 1 73786976294838206500 49191317529892137667 73786976294838206501 21081993227096630429 73786976294838206502 29514790517935282601 73786976294838206503 49191317529892137669 73786976294838206504 13415813871788764819 73786976294838206505 103270785577100359 73786976294838206506 49191317529892137671 73786976294838206507 29514790517935282603 73786976294838206508 21081993227096630431 73786976294838206509 49191317529892137673 73786976294838206510 11351842506898185617 73786976294838206511 1 73786976294838206512 49191317529892137675 73786976294838206513 1305964182209525779 0.440 seconds
REXX
Logic was added to the LPDIV function so that for odd numbers, only odd divisors were used.
This addition made it about 75% faster.
/*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.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= length(n) + 1 /*W: the length of any output column. */
@LPD = "largest proper divisor of N, where N < " n
idx = 1 /*initialize the index (output counter)*/
say ' index │'center(@LPD, 1 + cols*(w+1) ) /*display the title for the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " " sep " " " */
$= /*a list of largest proper divs so far.*/
do j=1 for n-1; $= $ right(LPDIV(j), w) /*add a largest proper divisor ──► list*/
if j//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot separator. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
LPDIV: procedure; parse arg x; if x<4 then return 1 /*obtain X; test if X < 4. */
odd= x // 2; beg= x % 2 /*use BEG as the first divisor.*/
if odd then if beg//2==0 then beg= beg - 1 /*Is X odd? Then make BEG odd.*/
do k=beg by -1-odd /*begin at halfway point and decrease. */
if x//k==0 then return k /*Remainder=0? Got largest proper div.*/
end /*k*/
return 1 /*If we get here, then X is a prime.*/
- output when using the default inputs:
index │ largest proper divisor of N, where N < 101 ───────┼─────────────────────────────────────────────────── 1 │ 1 1 1 2 1 3 1 4 3 5 11 │ 1 6 1 7 5 8 1 9 1 10 21 │ 7 11 1 12 5 13 9 14 1 15 31 │ 1 16 11 17 7 18 1 19 13 20 41 │ 1 21 1 22 15 23 1 24 7 25 51 │ 17 26 1 27 11 28 19 29 1 30 61 │ 1 31 21 32 13 33 1 34 23 35 71 │ 1 36 1 37 25 38 11 39 1 40 81 │ 27 41 1 42 17 43 29 44 1 45 91 │ 13 46 31 47 19 48 1 49 33 50 ───────┴───────────────────────────────────────────────────
Ring
? "working..."
limit = 100
? "Largest proper divisor up to " + limit + " are:"
see " 1 "
col = 1
for n = 2 to limit
for m = 1 to n >> 1
if n % m = 0 div = m ok
next
if div < 10 see " " ok
see "" + div + " "
if col++ % 10 = 0 ? "" ok
next
? "done..."
- Output:
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...
RPL
≪ IF DUP 1 ≠ THEN DUP DO 1 - UNTIL DUP2 MOD NOT END SWAP DROP END ≫ 'LPROPDIV' STO ≪ { } 1 100 FOR j j LPROPDIV + NEXT ≫ EVAL
- Output:
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 }
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!();
}
}
}
- Output:
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
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!();
}
}
}
- Output:
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
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}
- Output:
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
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;
- Output:
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
SETL
program largest_proper_divisor;
loop for n in [1..100] do
nprint(lpad(str lpd(n), 3));
if (col +:= 1) mod 10 = 0 then
print;
end if;
end loop;
proc lpd(n);
return if exists d in [n-1, n-2..2] | n mod d = 0 then d else 1 end;
end proc;
end program;
- Output:
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
Sidef
1..100 -> map {|n| proper_divisors(n).tail \\ 1 }.slices(10).each {|a|
a.map{ '%3s' % _ }.join(' ').say
}
- Output:
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
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" : " ")
}
- Output:
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
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 0
and stops.
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 |
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('')
}
}
}
- Output:
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
Wren
import "./math" for Int
import "./fmt" for Fmt
System.print("The largest proper divisors for numbers in the interval [1, 100] are:")
System.write(" 1 ")
for (n in 2..100) {
if (n % 2 == 0) {
Fmt.write("$2d ", n / 2)
} else {
Fmt.write("$2d ", Int.properDivisors(n)[-1])
}
if (n % 10 == 0) System.print()
}
- Output:
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
X86 Assembly
1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
4 org 100h ;.com program starts here
5
6 ;bl = D = divisor
7 ;bh = N
8
9 0100 B7 01 start: mov bh, 1 ;for N:= 1 to 100 do
10 0102 8A DF d10: mov bl, bh ;D:= if N=1 then 1 else N/2
11 0104 D0 EB shr bl, 1
12 0106 75 02 jne d15
13 0108 FE C3 inc bl
14 010A d15:
15 010A 8A C7 d20: mov al, bh ;while rem(N/D) # 0 do
16 010C 98 cbw ; ah:= 0 (extend sign of al into ah)
17 010D F6 FB idiv bl ; al:= ax/bl; ah:= remainder
18 010F 84 E4 test ah, ah
19 0111 74 04 je d30
20 0113 FE CB dec bl ; D--
21 0115 EB F3 jmp d20
22 0117 d30:
23 0117 8A C3 mov al, bl ;output number in D
24 0119 D4 0A aam 10 ;ah:= al/10; al:= remainder
25 011B 50 push ax ;save low digit in remainder
26 011C 8A C4 mov al, ah ;get high digit
27 011E 04 20 add al, 20h ;if zero make it a space char
28 0120 84 E4 test ah, ah
29 0122 74 02 je d50
30 0124 04 10 add al, 10h ;else make it into ASCII digit
31 0126 CD 29 d50: int 29h ;output high digit or space
32 0128 58 pop ax ;get low digit
33 0129 04 30 add al, 30h ;make it ASCII
34 012B CD 29 int 29h ;output low digit
35
36 012D B0 20 mov al, 20h ;output space char
37 012F CD 29 int 29h
38
39 0131 8A C7 mov al, bh ;if remainder(N/10) = 0 then CR LF
40 0133 D4 0A aam 10 ;ah:= al/10; al:= remainder
41 0135 3C 00 cmp al, 0
42 0137 75 08 jne next
43 0139 B0 0D mov al, 0Dh ;CR
44 013B CD 29 int 29h
45 013D B0 0A mov al, 0Ah ;LF
46 013F CD 29 int 29h
47
48 0141 FE C7 next: inc bh ;next N
49 0143 80 FF 64 cmp bh, 100
50 0146 7E BA jle d10
51 0148 C3 ret
52
53 end start
- Output:
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
XPL0
int N, D;
[for N:= 1 to 100 do
[D:= if N=1 then 1 else N/2;
while rem(N/D) do D:= D-1;
if D<10 then ChOut(0, ^ );
IntOut(0, D);
if rem(N/10) = 0 then CrLf(0) else ChOut(0, ^ );
];
]
- Output:
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
- Programming Tasks
- Solutions by Programming Task
- 11l
- Ada
- ALGOL 68
- ALGOL W
- APL
- AppleScript
- Arturo
- AWK
- BASIC
- ANSI BASIC
- Applesoft BASIC
- BASIC256
- Chipmunk Basic
- Craft Basic
- FreeBASIC
- FutureBasic
- GW-BASIC
- Gambas
- Minimal BASIC
- MSX Basic
- Palo Alto Tiny BASIC
- PureBasic
- Quite BASIC
- Run BASIC
- Tiny BASIC
- True BASIC
- XBasic
- Yabasic
- BCPL
- BQN
- C
- C++
- Cowgol
- D
- Dart
- Delphi
- SysUtils,StdCtrls
- Draco
- EasyLang
- F Sharp
- Factor
- Fermat
- FOCAL
- Forth
- Fortran
- Frink
- Go
- Haskell
- J
- Java
- Jq
- Julia
- Lua
- MAD
- Mathematica
- Wolfram Language
- Maxima
- Modula-2
- Nim
- Pascal
- Perl
- Ntheory
- Phix
- Picat
- PL/I
- PL/M
- Python
- Quackery
- R
- Raku
- REXX
- Ring
- RPL
- Rust
- Ruby
- Seed7
- SETL
- Sidef
- Swift
- TI-57
- V (Vlang)
- Wren
- Wren-math
- Wren-fmt
- X86 Assembly
- XPL0