Largest proper divisor of n: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a variant which only checks the first two proper divisors (if there are that many)) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 463: | Line 463: | ||
27 41 1 42 17 43 29 44 1 45 |
27 41 1 42 17 43 29 44 1 45 |
||
13 46 31 47 19 48 1 49 33 50</pre> |
13 46 31 47 19 48 1 49 33 50</pre> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program finds the largest proper divisors of all numbers (up to a given limit). */ |
|||
parse arg n cols . /*obtain optional argument from the CL.*/ |
|||
if n=='' | n=="," then n= 101 /*Not specified? Then use the default.*/ |
|||
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. */ |
|||
do k=x%2 by -1 /*start at halfway point and decrease. */ |
|||
if x//k==0 then return k /*No remainder? Got largest proper div*/ |
|||
end /*k*/ |
|||
return 1 /*the number X is a prime. */</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
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 |
|||
───────┴─────────────────────────────────────────────────── |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |