Largest proper divisor of n: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 7: Line 7:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F lpd(n)
<syntaxhighlight lang="11l">F lpd(n)
L(i) (n - 1 .< 0).step(-1)
L(i) (n - 1 .< 0).step(-1)
I n % i == 0
I n % i == 0
Line 14: Line 14:


L(i) 1..100
L(i) 1..100
print(‘#3’.format(lpd(i)), end' I i % 10 == 0 {"\n"} E ‘’)</lang>
print(‘#3’.format(lpd(i)), end' I i % 10 == 0 {"\n"} E ‘’)</syntaxhighlight>


{{out}}
{{out}}
Line 31: Line 31:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;


Line 55: Line 55:
end if;
end if;
end loop;
end loop;
end Main;</lang>
end Main;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 71: Line 71:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>FOR n TO 100 DO # show the largest proper divisors for n = 1..100 #
<syntaxhighlight lang="algol68">FOR n TO 100 DO # show the largest proper divisors for n = 1..100 #
INT largest proper divisor := 1;
INT largest proper divisor := 1;
FOR j FROM ( n OVER 2 ) BY -1 TO 2 WHILE largest proper divisor = 1 DO
FOR j FROM ( n OVER 2 ) BY -1 TO 2 WHILE largest proper divisor = 1 DO
Line 81: Line 81:
IF n MOD 10 = 0 THEN print( ( newline ) ) FI
IF n MOD 10 = 0 THEN print( ( newline ) ) FI
OD
OD
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 97: Line 97:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>for n := 1 until 100 do begin % show the largest proper divisors for n = 1..100 %
<syntaxhighlight 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
for j := n div 2 step -1 until 2 do begin
if n rem j = 0 then begin
if n rem j = 0 then begin
Line 107: Line 107:
foundLargestProperDivisor:
foundLargestProperDivisor:
if n rem 10 = 0 then write()
if n rem 10 = 0 then write()
end for_n.</lang>
end for_n.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 124: Line 124:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<lang apl>(⌈/1,(⍸0=¯1↓⍳|⊢))¨10 10⍴⍳100</lang>
<syntaxhighlight lang="apl">(⌈/1,(⍸0=¯1↓⍳|⊢))¨10 10⍴⍳100</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 140: Line 140:
Most of this code is just to prepare the output for display. :D
Most of this code is just to prepare the output for display. :D


<lang applescript>on largestProperDivisor(n)
<syntaxhighlight lang="applescript">on largestProperDivisor(n)
if (n mod 2 = 0) then return n div 2
if (n mod 2 = 0) then return n div 2
if (n mod 3 = 0) then return n div 3
if (n mod 3 = 0) then return n div 3
Line 179: Line 179:
end task
end task


task(100)</lang>
task(100)</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"1:1 2:1 3:1 4:2 5:1 6:3 7:1 8:4 9:3 10:5
<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
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
21:7 22:11 23:1 24:12 25:5 26:13 27:9 28:14 29:1 30:15
Line 191: Line 191:
71:1 72:36 73:1 74:37 75:25 76:38 77:11 78:39 79:1 80:40
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
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 "</lang>
91:13 92:46 93:31 94:47 95:19 96:48 97:1 98:49 99:33 100:50 "</syntaxhighlight>




Line 198: Line 198:
Composing functionally, for rapid drafting and refactoring, with higher levels of code reuse:
Composing functionally, for rapid drafting and refactoring, with higher levels of code reuse:


<lang applescript>use framework "Foundation"
<syntaxhighlight lang="applescript">use framework "Foundation"




Line 446: Line 446:
item 1 of ((ca's NSArray's arrayWithObject:nsValue) as list)
item 1 of ((ca's NSArray's arrayWithObject:nsValue) as list)
end if
end if
end unwrap</lang>
end unwrap</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 461: Line 461:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>loop split.every:10 [1] ++ map 2..100 'x -> last chop factors x 'row [
<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
print map to [:string] row 'r -> pad r 5
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 479: Line 479:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LARGEST_PROPER_DIVISOR_OF_N.AWK
# syntax: GAWK -f LARGEST_PROPER_DIVISOR_OF_N.AWK
# converted from C
# converted from C
Line 501: Line 501:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 519: Line 519:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang basic>10 DEFINT A-Z
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 FOR I=1 TO 100
20 FOR I=1 TO 100
30 IF I=1 THEN PRINT " 1";: GOTO 70
30 IF I=1 THEN PRINT " 1";: GOTO 70
Line 526: Line 526:
60 NEXT J
60 NEXT J
70 IF I MOD 10=0 THEN PRINT
70 IF I MOD 10=0 THEN PRINT
80 NEXT I</lang>
80 NEXT I</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 541: Line 541:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>print "El mayor divisor propio de n es:\n"
<syntaxhighlight lang="basic256">print "El mayor divisor propio de n es:\n"
print " 1 1 ";
print " 1 1 ";
for i = 3 to 100
for i = 3 to 100
Line 552: Line 552:
if i % 10 = 0 then print
if i % 10 = 0 then print
next i
next i
end</lang>
end</syntaxhighlight>




=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let lpd(n) = valof
let lpd(n) = valof
Line 566: Line 566:
$( writed(lpd(i), 3)
$( writed(lpd(i), 3)
if i rem 10=0 then wrch('*N')
if i rem 10=0 then wrch('*N')
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 580: Line 580:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>(1⌈´↕×0=↕|⊢)¨ ∘‿10⥊1+↕100</lang>
<syntaxhighlight lang="bqn">(1⌈´↕×0=↕|⊢)¨ ∘‿10⥊1+↕100</syntaxhighlight>
{{out}}
{{out}}
<pre>┌─
<pre>┌─
Line 596: Line 596:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


unsigned int lpd(unsigned int n) {
unsigned int lpd(unsigned int n) {
Line 612: Line 612:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 626: Line 626:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <cassert>
<syntaxhighlight lang="cpp">#include <cassert>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 646: Line 646:
<< (n % 10 == 0 ? '\n' : ' ');
<< (n % 10 == 0 ? '\n' : ' ');
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 663: Line 663:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub print3(n: uint8) is
sub print3(n: uint8) is
Line 696: Line 696:
end if;
end if;
i := i + 1;
i := i + 1;
end loop;</lang>
end loop;</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 710: Line 710:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec lpd(word n) word:
<syntaxhighlight lang="draco">proc nonrec lpd(word n) word:
word d;
word d;
if n=1 then
if n=1 then
Line 727: Line 727:
if n%10 = 0 then writeln() fi
if n%10 = 0 then writeln() fi
od
od
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 741: Line 741:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Largest proper divisor of n: Nigel Galloway. June 2nd., 2021
// 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))
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 ""
seq{yield 1; yield! seq{2..100}|>Seq.map fN}|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 754: Line 754:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: grouping kernel math math.bitwise math.functions
<syntaxhighlight lang="factor">USING: grouping kernel math math.bitwise math.functions
math.ranges prettyprint sequences ;
math.ranges prettyprint sequences ;


Line 763: Line 763:
: largest ( m -- n ) dup odd? [ odd ] [ 2/ ] if ;
: largest ( m -- n ) dup odd? [ odd ] [ 2/ ] if ;


100 [1,b] [ largest ] map 10 group simple-table.</lang>
100 [1,b] [ largest ] map 10 group simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 779: Line 779:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Func Lpd(n) =
<syntaxhighlight lang="fermat">Func Lpd(n) =
if n = 1 then Return(1) fi;
if n = 1 then Return(1) fi;
for i = n\2 to 1 by -1 do
for i = n\2 to 1 by -1 do
Line 788: Line 788:
!(Lpd(m):4);
!(Lpd(m):4);
if m|10=0 then ! fi;
if m|10=0 then ! fi;
od;</lang>
od;</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
1 1 1 2 1 3 1 4 3 5
1 1 1 2 1 3 1 4 3 5
Line 803: Line 803:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang focal>01.10 F N=1,100;D 2;D 3
<syntaxhighlight lang="focal">01.10 F N=1,100;D 2;D 3
01.20 Q
01.20 Q


Line 817: Line 817:
03.20 S A=N/10
03.20 S A=N/10
03.30 I (FITR(A)-A)3.4;T !
03.30 I (FITR(A)-A)3.4;T !
03.40 R</lang>
03.40 R</syntaxhighlight>
{{out}}
{{out}}
<pre>= 1= 1= 1= 2= 1= 3= 1= 4= 3= 5
<pre>= 1= 1= 1= 2= 1= 3= 1= 4= 3= 5
Line 832: Line 832:
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|Gforth}}
{{works with|Gforth}}
<lang forth>: largest-proper-divisor { n -- n }
<syntaxhighlight lang="forth">: largest-proper-divisor { n -- n }
n 1 and 0= if n 2/ exit then
n 1 and 0= if n 2/ exit then
3
3
Line 850: Line 850:


main
main
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 867: Line 867:


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran> program LargestProperDivisors
<syntaxhighlight lang="fortran"> program LargestProperDivisors
implicit none
implicit none
integer i, lpd
integer i, lpd
Line 885: Line 885:
20 lpd = i
20 lpd = i
end if
end if
end function</lang>
end function</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 900: Line 900:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>Print !"El mayor divisor propio de n es:\n"
<syntaxhighlight lang="freebasic">Print !"El mayor divisor propio de n es:\n"
Print " 1 1";
Print " 1 1";
For i As Byte = 3 To 100
For i As Byte = 3 To 100
Line 908: Line 908:
If i Mod 10 = 0 Then Print
If i Mod 10 = 0 Then Print
Next i
Next i
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>El mayor divisor propio de n es:
<pre>El mayor divisor propio de n es:
Line 926: Line 926:
Frink contains efficient routines for factoring numbers. It uses trial division, wheel factoring, and Pollard rho factoring.
Frink contains efficient routines for factoring numbers. It uses trial division, wheel factoring, and Pollard rho factoring.


<lang frink>for n = 1 to 100
<syntaxhighlight lang="frink">for n = 1 to 100
println[last[allFactors[n,true,false]]]</lang>
println[last[allFactors[n,true,false]]]</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 956: Line 956:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 974: Line 974:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>10 PRINT 1;
<syntaxhighlight lang="gwbasic">10 PRINT 1;
20 FOR I = 1 TO 101
20 FOR I = 1 TO 101
30 FOR D = I\2 TO 1 STEP -1
30 FOR D = I\2 TO 1 STEP -1
40 IF I MOD D = 0 THEN PRINT D; : GOTO 60
40 IF I MOD D = 0 THEN PRINT D; : GOTO 60
50 NEXT D
50 NEXT D
60 NEXT I</lang>
60 NEXT I</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List.Split (chunksOf)
<syntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
import Text.Printf (printf)
import Text.Printf (printf)


Line 992: Line 992:
main =
main =
(putStr . unlines . map concat . chunksOf 10) $
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . lpd <$> [1 .. 100]</lang>
printf "%3d" . lpd <$> [1 .. 100]</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,011: Line 1,011:
(Otherwise, the largest proper divisor will be 1 itself).
(Otherwise, the largest proper divisor will be 1 itself).


<lang haskell>import Data.List (find)
<syntaxhighlight lang="haskell">import Data.List (find)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
import Text.Printf (printf)
Line 1,026: Line 1,026:
main =
main =
(putStr . unlines . map concat . chunksOf 10) $
(putStr . unlines . map concat . chunksOf 10) $
printf "%3d" . maxProperDivisors <$> [1 .. 100]</lang>
printf "%3d" . maxProperDivisors <$> [1 .. 100]</syntaxhighlight>


<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,040: Line 1,040:


=={{header|J}}==
=={{header|J}}==
<lang j> lpd =: (1 |.!.1 ])&.q:</lang>
<syntaxhighlight lang="j"> lpd =: (1 |.!.1 ])&.q:</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,056: Line 1,056:


Naive version:
Naive version:
<lang jq># (1|largestpd) is 1 as per the task definition
<syntaxhighlight lang="jq"># (1|largestpd) is 1 as per the task definition
def largestpd:
def largestpd:
if . == 1 then 1
if . == 1 then 1
else . as $n
else . as $n
| first( range( ($n - ($n % 2)) /2; 0; -1) | (select($n % . == 0) ))
| first( range( ($n - ($n % 2)) /2; 0; -1) | (select($n % . == 0) ))
end;</lang>
end;</syntaxhighlight>


Slightly less naive:
Slightly less naive:
<lang jq>def largestpd:
<syntaxhighlight lang="jq">def largestpd:
if . == 1 then 1
if . == 1 then 1
else . as $n
else . as $n
Line 1,071: Line 1,071:
else first( range( ($n - ($n % 11)) /11; 0; -1) | (select($n % . == 0) ))
else first( range( ($n - ($n % 11)) /11; 0; -1) | (select($n % . == 0) ))
end
end
end;</lang>
end;</syntaxhighlight>
<lang jq># For neatness
<syntaxhighlight lang="jq"># For neatness
def lpad($len):
def lpad($len):
tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
Line 1,082: Line 1,082:
### The task
### The task
[range(1; 101) | largestpd]
[range(1; 101) | largestpd]
| nwise(10) | map(lpad(2)) | join(" ")</lang>
| nwise(10) | map(lpad(2)) | join(" ")</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,096: Line 1,096:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>largestpd(n) = (for i in n÷2:-1:1 (n % i == 0) && return i; end; 1)
<syntaxhighlight 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)
foreach(n -> print(rpad(largestpd(n), 3), n % 10 == 0 ? "\n" : ""), 1:100)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 1 1 2 1 3 1 4 3 5
1 1 1 2 1 3 1 4 3 5
Line 1,114: Line 1,114:


=={{header|MAD}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
INTERNAL FUNCTION(N)
Line 1,130: Line 1,130:
VECTOR VALUES TABLE = $10(I3)*$
VECTOR VALUES TABLE = $10(I3)*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,144: Line 1,144:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>Last[Prepend[DeleteCases[Divisors[#], #], 1]] & /@ Range[100]</lang>
<syntaxhighlight lang="mathematica">Last[Prepend[DeleteCases[Divisors[#], #], 1]] & /@ Range[100]</syntaxhighlight>
{{out}}
{{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>
<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}}==
=={{header|Modula-2}}==
<lang modula2>MODULE LargestProperDivisor;
<syntaxhighlight lang="modula2">MODULE LargestProperDivisor;
FROM InOut IMPORT WriteCard, WriteLn;
FROM InOut IMPORT WriteCard, WriteLn;


Line 1,175: Line 1,175:
END;
END;
END;
END;
END LargestProperDivisor.</lang>
END LargestProperDivisor.</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,189: Line 1,189:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strutils
<syntaxhighlight lang="nim">import math, strutils


func largestProperDivisor(n: Positive): int =
func largestProperDivisor(n: Positive): int =
Line 1,197: Line 1,197:


for n in 1..100:
for n in 1..100:
stdout.write ($n.largestProperDivisor).align(2), if n mod 10 == 0: '\n' else: ' '</lang>
stdout.write ($n.largestProperDivisor).align(2), if n mod 10 == 0: '\n' else: ' '</syntaxhighlight>


{{out}}
{{out}}
Line 1,212: Line 1,212:


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>
<syntaxhighlight lang="pascal">
program LarPropDiv;
program LarPropDiv;


Line 1,241: Line 1,241:
Writeln;
Writeln;
end;
end;
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,257: Line 1,257:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory 'divisors';
use ntheory 'divisors';
Line 1,275: Line 1,275:
while( my @batch = $iter->() ) {
while( my @batch = $iter->() ) {
printf '%3d', $_ == 1 ? 1 : max proper_divisors($_) for @batch; print "\n";
printf '%3d', $_ == 1 ? 1 : max proper_divisors($_) for @batch; print "\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>GPD for 1 through 100:
<pre>GPD for 1 through 100:
Line 1,290: Line 1,290:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,307: Line 1,307:
</pre>
</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.
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.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 1,323: Line 1,323:
<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;">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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
foreach(I in 1..100)
foreach(I in 1..100)
printf("%2d%s",a(I), cond(I mod 10 == 0,"\n", " "))
printf("%2d%s",a(I), cond(I mod 10 == 0,"\n", " "))
Line 1,337: Line 1,337:
N mod I == 0.
N mod I == 0.
a(N,I,Div) :-
a(N,I,Div) :-
a(N,I-1,Div).</lang>
a(N,I-1,Div).</syntaxhighlight>


{{out}}
{{out}}
Line 1,353: Line 1,353:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>largestProperDivisor: procedure options(main);
<syntaxhighlight lang="pli">largestProperDivisor: procedure options(main);
lpd: procedure(n) returns(fixed);
lpd: procedure(n) returns(fixed);
declare (n, i) fixed;
declare (n, i) fixed;
Line 1,367: Line 1,367:
if mod(i,10)=0 then put skip;
if mod(i,10)=0 then put skip;
end;
end;
end largestProperDivisor;</lang>
end largestProperDivisor;</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,381: Line 1,381:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H:
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,412: Line 1,412:
END;
END;
CALL EXIT;
CALL EXIT;
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,426: Line 1,426:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure.i lpd(v.i)
<syntaxhighlight lang="purebasic">Procedure.i lpd(v.i)
For i=v/2 To 1 Step -1
For i=v/2 To 1 Step -1
If v%i=0 : ProcedureReturn i : EndIf
If v%i=0 : ProcedureReturn i : EndIf
Line 1,439: Line 1,439:
Next
Next
Input()
Input()
EndIf</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,453: Line 1,453:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def lpd(n):
<syntaxhighlight lang="python">def lpd(n):
for i in range(n-1,0,-1):
for i in range(n-1,0,-1):
if n%i==0: return i
if n%i==0: return i
Line 1,459: Line 1,459:


for i in range(1,101):
for i in range(1,101):
print("{:3}".format(lpd(i)), end=i%10==0 and '\n' or '')</lang>
print("{:3}".format(lpd(i)), end=i%10==0 and '\n' or '')</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,475: Line 1,475:
Or, reducing the search space, formatting more flexibly (to allow for experiments with larger ranges) and composing functionally:
Or, reducing the search space, formatting more flexibly (to allow for experiments with larger ranges) and composing functionally:


<lang python>'''Largest proper divisor of'''
<syntaxhighlight lang="python">'''Largest proper divisor of'''


from math import isqrt
from math import isqrt
Line 1,546: Line 1,546:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,562: Line 1,562:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].


<lang Quackery>' [ 1 ] 99 times
<syntaxhighlight lang="quackery">' [ 1 ] 99 times
[ i^ 2 + factors
[ i^ 2 + factors
-2 peek join ]
-2 peek join ]
echo</lang>
echo</syntaxhighlight>


{{out}}
{{out}}
Line 1,572: Line 1,572:


=={{header|R}}==
=={{header|R}}==
<lang R>largest_proper_divisor <- function(n){
<syntaxhighlight lang="r">largest_proper_divisor <- function(n){
if(n == 1) return(1)
if(n == 1) return(1)
Line 1,590: Line 1,590:
largest_proper_divisor(i)
largest_proper_divisor(i)
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,618: Line 1,618:


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.
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.
<lang perl6>use Prime::Factor;
<syntaxhighlight lang="raku" line>use Prime::Factor;


for 0, 2**67 - 1 -> $add {
for 0, 2**67 - 1 -> $add {
Line 1,631: Line 1,631:


say (now - $start).fmt("%0.3f seconds\n");
say (now - $start).fmt("%0.3f seconds\n");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>GPD for 1 through 100:
<pre>GPD for 1 through 100:
Line 1,663: Line 1,663:


This addition made it about &nbsp; '''75%''' &nbsp; faster.
This addition made it about &nbsp; '''75%''' &nbsp; faster.
<lang rexx>/*REXX program finds the largest proper divisors of all numbers (up to a given limit). */
<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.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 101 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 101 /*Not specified? Then use the default.*/
Line 1,689: Line 1,689:
if x//k==0 then return k /*Remainder=0? Got largest proper div.*/
if x//k==0 then return k /*Remainder=0? Got largest proper div.*/
end /*k*/
end /*k*/
return 1 /*If we get here, then X is a prime.*/</lang>
return 1 /*If we get here, then X is a prime.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,708: Line 1,708:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>? "working..."
<syntaxhighlight lang="ring">? "working..."
limit = 100
limit = 100
? "Largest proper divisor up to " + limit + " are:"
? "Largest proper divisor up to " + limit + " are:"
Line 1,722: Line 1,722:
if col++ % 10 = 0 ? "" ok
if col++ % 10 = 0 ? "" ok
next
next
? "done..."</lang>
? "done..."</syntaxhighlight>
{{out}}
{{out}}
<pre>working...
<pre>working...
Line 1,739: Line 1,739:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'


def a(n)
def a(n)
Line 1,747: Line 1,747:


(1..100).map{|n| a(n).to_s.rjust(3)}.each_slice(10){|slice| puts slice.join}
(1..100).map{|n| a(n).to_s.rjust(3)}.each_slice(10){|slice| puts slice.join}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> 1 1 1 2 1 3 1 4 3 5
<pre> 1 1 1 2 1 3 1 4 3 5
Line 1,762: Line 1,762:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func integer: largestProperDivisor (in integer: number) is func
const func integer: largestProperDivisor (in integer: number) is func
Line 1,793: Line 1,793:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,809: Line 1,809:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func largestProperDivisor(_ n : Int) -> Int? {
func largestProperDivisor(_ n : Int) -> Int? {
Line 1,831: Line 1,831:
print(String(format: "%2d", largestProperDivisor(n)!),
print(String(format: "%2d", largestProperDivisor(n)!),
terminator: n % 10 == 0 ? "\n" : " ")
terminator: n % 10 == 0 ? "\n" : " ")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,850: Line 1,850:
=={{header|True BASIC}}==
=={{header|True BASIC}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang qbasic>PRINT "El mayor divisor propio de n es:"
<syntaxhighlight lang="qbasic">PRINT "El mayor divisor propio de n es:"
PRINT
PRINT
PRINT " 1 1";
PRINT " 1 1";
Line 1,862: Line 1,862:
IF remainder(i, 10) = 0 Then PRINT
IF remainder(i, 10) = 0 Then PRINT
NEXT i
NEXT i
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,870: Line 1,870:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>fn largest_proper_divisor(n int) int {
<syntaxhighlight lang="vlang">fn largest_proper_divisor(n int) int {
for i := 2; i*i <= n; i++ {
for i := 2; i*i <= n; i++ {
if n%i == 0 {
if n%i == 0 {
Line 1,892: Line 1,892:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,913: Line 1,913:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,925: Line 1,925:
}
}
if (n % 10 == 0) System.print()
if (n % 10 == 0) System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,945: Line 1,945:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang yabasic>print "El mayor divisor propio de n es:\n"
<syntaxhighlight lang="yabasic">print "El mayor divisor propio de n es:\n"
print " 1 1 ";
print " 1 1 ";
for i = 3 to 100
for i = 3 to 100
Line 1,954: Line 1,954:
next i
next i
print
print
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,962: Line 1,962:


=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
<lang asm> 1 ;Assemble with: tasm, tlink /t
<syntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
2 0000 .model tiny
3 0000 .code
3 0000 .code
Line 2,015: Line 2,015:
52
52
53 end start
53 end start
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,032: Line 2,032:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int N, D;
<syntaxhighlight lang="xpl0">int N, D;
[for N:= 1 to 100 do
[for N:= 1 to 100 do
[D:= if N=1 then 1 else N/2;
[D:= if N=1 then 1 else N/2;
Line 2,040: Line 2,040:
if rem(N/10) = 0 then CrLf(0) else ChOut(0, ^ );
if rem(N/10) = 0 then CrLf(0) else ChOut(0, ^ );
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}