Factors of an integer: Difference between revisions

Content deleted Content added
Eriksiers (talk | contribs)
m alphabetized list
Line 159: Line 159:
</pre>
</pre>


=={{header|Batch File}}==
Command line version:
<lang dos>@echo off
set res=Factors of %1:
for /L %%i in (1,1,%1) do call :fac %1 %%i
echo %res%
goto :eof

:fac
set /a test = %1 %% %2
if %test% equ 0 set res=%res% %2</lang>

Outputs:
<pre>>factors 32767
Factors of 32767: 1 7 31 151 217 1057 4681 32767

>factors 45
Factors of 45: 1 3 5 9 15 45

>factors 53
Factors of 53: 1 53

>factors 64
Factors of 64: 1 2 4 8 16 32 64

>factors 100
Factors of 100: 1 2 4 5 10 20 25 50 100</pre>

Interactive version:

<lang dos>@echo off
set /p limit=Gimme a number:
set res=Factors of %limit%:
for /L %%i in (1,1,%limit%) do call :fac %limit% %%i
echo %res%
goto :eof

:fac
set /a test = %1 %% %2
if %test% equ 0 set res=%res% %2</lang>

Outputs:

<pre>>factors
Gimme a number:27
Factors of 27: 1 3 9 27

>factors
Gimme a number:102
Factors of 102: 1 2 3 6 17 34 51 102</pre>
=={{header|BASIC}}==
=={{header|BASIC}}==


Line 269: Line 219:
1 , 2 , 3 , 6 , 43 , 86 , 127 , 129 , 254 , 258 , 381 , 762 , 5461 , 10922 ,
1 , 2 , 3 , 6 , 43 , 86 , 127 , 129 , 254 , 258 , 381 , 762 , 5461 , 10922 ,
16383 , 32766
16383 , 32766

=={{header|Batch File}}==
Command line version:
<lang dos>@echo off
set res=Factors of %1:
for /L %%i in (1,1,%1) do call :fac %1 %%i
echo %res%
goto :eof

:fac
set /a test = %1 %% %2
if %test% equ 0 set res=%res% %2</lang>

Outputs:
<pre>>factors 32767
Factors of 32767: 1 7 31 151 217 1057 4681 32767

>factors 45
Factors of 45: 1 3 5 9 15 45

>factors 53
Factors of 53: 1 53

>factors 64
Factors of 64: 1 2 4 8 16 32 64

>factors 100
Factors of 100: 1 2 4 5 10 20 25 50 100</pre>

Interactive version:

<lang dos>@echo off
set /p limit=Gimme a number:
set res=Factors of %limit%:
for /L %%i in (1,1,%limit%) do call :fac %limit% %%i
echo %res%
goto :eof

:fac
set /a test = %1 %% %2
if %test% equ 0 set res=%res% %2</lang>

Outputs:

<pre>>factors
Gimme a number:27
Factors of 27: 1 3 9 27

>factors
Gimme a number:102
Factors of 102: 1 2 3 6 17 34 51 102</pre>


=={{header|C}}==
=={{header|C}}==
Line 927: Line 928:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang>divisors(n)</lang>
<lang>divisors(n)</lang>

=={{header|Perl}}==
<lang perl>sub factors
{
my($n) = @_;
return grep { $n % $_ == 0 }(1 .. $n);
}
print join ' ',factors(64);</lang>


=={{header|Perl 6}}==
=={{header|Perl 6}}==
Line 951: Line 960:
sort($factors);
sort($factors);
return $factors;
return $factors;
}</lang>
}

=={{header|PicoLisp}}==
<lang PicoLisp>(de factors (N)
(filter
'((D) (=0 (% N D)))
(range 1 N) ) )</lang>


</lang>
=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>
<lang PL/I>
Line 976: Line 990:
Here the range of integers is only taken up to the square root of the number, the same filtering applies. Afterwards the corresponding larger factors are calculated and sent down the pipeline along with the small ones found earlier.
Here the range of integers is only taken up to the square root of the number, the same filtering applies. Afterwards the corresponding larger factors are calculated and sent down the pipeline along with the small ones found earlier.


=={{header|Perl}}==
<lang perl>sub factors
{
my($n) = @_;
return grep { $n % $_ == 0 }(1 .. $n);
}
print join ' ',factors(64);</lang>

=={{header|PicoLisp}}==
<lang PicoLisp>(de factors (N)
(filter
'((D) (=0 (% N D)))
(range 1 N) ) )</lang>
=={{header|Prolog}}==
=={{header|Prolog}}==
<lang Prolog>factor(N, L) :-
<lang Prolog>factor(N, L) :-