Largest proper divisor of n: Difference between revisions
Content deleted Content added
→{{header|Python}}: Added a functional variant which reduces the search space and formats more flexibly. |
Added Forth solution |
||
Line 246: | Line 246: | ||
= 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|Forth}}== |
|||
{{works with|Gforth}} |
|||
<lang forth>: largest-proper-divisor { n -- n } |
|||
n 1 and 0= if n 2/ exit then |
|||
3 |
|||
begin |
|||
dup dup * n <= |
|||
while |
|||
dup n swap /mod swap |
|||
0= if nip exit else drop then |
|||
2 + |
|||
repeat drop 1 ; |
|||
: main |
|||
101 1 do |
|||
i largest-proper-divisor 2 .r |
|||
i 10 mod 0= if cr else space then |
|||
loop ; |
|||
main |
|||
bye</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 1 1 2 1 3 1 4 3 5 |
|||
1 6 1 7 5 8 1 9 1 10 |
|||
7 11 1 12 5 13 9 14 1 15 |
|||
1 16 11 17 7 18 1 19 13 20 |
|||
1 21 1 22 15 23 1 24 7 25 |
|||
17 26 1 27 11 28 19 29 1 30 |
|||
1 31 21 32 13 33 1 34 23 35 |
|||
1 36 1 37 25 38 11 39 1 40 |
|||
27 41 1 42 17 43 29 44 1 45 |
|||
13 46 31 47 19 48 1 49 33 50 |
|||
</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |