Factors of an integer: Difference between revisions
Content added Content deleted
(J: another approach) |
Walterpachl (talk | contribs) (added NetRexx) |
||
Line 1,404: | Line 1,404: | ||
64 factors .s .clr ( => 1 2 4 8 16 32 64 ) newline |
64 factors .s .clr ( => 1 2 4 8 16 32 64 ) newline |
||
12 factors .s .clr ( => 1 2 3 4 6 12 ) </lang> |
12 factors .s .clr ( => 1 2 3 4 6 12 ) </lang> |
||
=={{header|NetRexx}}== |
|||
{{trans|REXX}} |
|||
<lang NetRexx>/* NetRexx *********************************************************** |
|||
* 21.04.2013 Walter Pachl |
|||
*********************************************************************/ |
|||
options replace format comments java crossref symbols nobinary |
|||
a=1 |
|||
b=64 |
|||
loop x=a To b |
|||
say x.right(2) '->' divs(x) |
|||
End |
|||
method divs(x) public static returns Rexx |
|||
if x==1 then return 1 /*handle special case of 1 */ |
|||
lo=1 |
|||
hi=x |
|||
odd=x//2 /* 1 if x is odd */ |
|||
loop j=2+odd By 1+odd While j*j<x /*divide by numbers<sqrt(x) */ |
|||
if x//j==0 then Do /*Divisible? Add two divisors:*/ |
|||
lo=lo j /* list low divisors */ |
|||
hi=x%j hi /* list high divisors */ |
|||
End |
|||
End |
|||
If j*j=x Then /*for a square number as input */ |
|||
lo=lo j /* add its square root */ |
|||
return lo hi /* return both lists */</lang> |
|||
Output: |
|||
<pre> 1 -> 1 |
|||
2 -> 1 2 |
|||
3 -> 1 3 |
|||
4 -> 1 2 4 |
|||
5 -> 1 5 |
|||
6 -> 1 2 3 6 |
|||
7 -> 1 7 |
|||
8 -> 1 2 4 8 |
|||
9 -> 1 3 9 |
|||
10 -> 1 2 5 10 |
|||
etc.</pre> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |