Jump to content

Factors of an integer: Difference between revisions

added NetRexx
(J: another approach)
(added NetRexx)
Line 1,404:
64 factors .s .clr ( => 1 2 4 8 16 32 64 ) newline
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}}==
2,299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.