Count in factors: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24: | Line 24: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|C++}}< |
{{trans|C++}}<syntaxhighlight lang="11l">F get_prime_factors(=li) |
||
I li == 1 |
I li == 1 |
||
R ‘1’ |
R ‘1’ |
||
Line 43: | Line 43: | ||
L(x) 1..17 |
L(x) 1..17 |
||
print(‘#4: #.’.format(x, get_prime_factors(x))) |
print(‘#4: #.’.format(x, get_prime_factors(x))) |
||
print(‘2144: ’get_prime_factors(2144))</ |
print(‘2144: ’get_prime_factors(2144))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 68: | Line 68: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Count in factors 24/03/2017 |
||
COUNTFAC CSECT assist plig\COUNTFAC |
COUNTFAC CSECT assist plig\COUNTFAC |
||
USING COUNTFAC,R13 base register |
USING COUNTFAC,R13 base register |
||
Line 141: | Line 141: | ||
PG DS CL80 buffer |
PG DS CL80 buffer |
||
YREGS |
YREGS |
||
END COUNTFAC</ |
END COUNTFAC</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:20ex"> |
<pre style="height:20ex"> |
||
Line 187: | Line 187: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC PrintFactors(CARD a) |
||
BYTE notFirst |
BYTE notFirst |
||
CARD p |
CARD p |
||
Line 220: | Line 220: | ||
PutE() |
PutE() |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_in_factors.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_in_factors.png Screenshot from Atari 8-bit computer] |
||
Line 243: | Line 243: | ||
;count.adb: |
;count.adb: |
||
< |
<syntaxhighlight lang="ada">with Ada.Command_Line, Ada.Text_IO, Prime_Numbers; |
||
procedure Count is |
procedure Count is |
||
Line 272: | Line 272: | ||
exit when N > Max_N; |
exit when N > Max_N; |
||
end loop; |
end loop; |
||
end Count;</ |
end Count;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 292: | Line 292: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|Euphoria}}< |
{{trans|Euphoria}}<syntaxhighlight lang="algol68">OP +:= = (REF FLEX []INT a, INT b) VOID: |
||
BEGIN |
BEGIN |
||
[⌈a + 1] INT c; |
[⌈a + 1] INT c; |
||
Line 327: | Line 327: | ||
OD; |
OD; |
||
print ((new line)) |
print ((new line)) |
||
OD</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 353: | Line 353: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program countFactors.s */ |
/* program countFactors.s */ |
||
Line 735: | Line 735: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Number 2144 : 2 2 2 2 2 67 |
Number 2144 : 2 2 2 2 2 67 |
||
Line 741: | Line 741: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">loop 1..30 'x [ |
||
fs: [1] |
fs: [1] |
||
if x<>1 -> fs: factors.prime x |
if x<>1 -> fs: factors.prime x |
||
print [pad to :string x 3 "=" join.with:" x " to [:string] fs] |
print [pad to :string x 3 "=" join.with:" x " to [:string] fs] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 782: | Line 782: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="autohotkey">factorize(n){ |
||
if n = 1 |
if n = 1 |
||
return 1 |
return 1 |
||
Line 798: | Line 798: | ||
Loop 22 |
Loop 22 |
||
out .= A_Index ": " factorize(A_index) "`n" |
out .= A_Index ": " factorize(A_index) "`n" |
||
MsgBox % out</ |
MsgBox % out</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 824: | Line 824: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f COUNT_IN_FACTORS.AWK |
# syntax: GAWK -f COUNT_IN_FACTORS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 851: | Line 851: | ||
return(substr(f,1,length(f)-1)) |
return(substr(f,1,length(f)-1)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<p>output:</p> |
<p>output:</p> |
||
<pre> |
<pre> |
||
Line 878: | Line 878: | ||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="applesoftbasic"> 100 FOR I = 1 TO 20 |
||
110 GOSUB 200"FACTORIAL |
110 GOSUB 200"FACTORIAL |
||
120 PRINT I" = "FA$ |
120 PRINT I" = "FA$ |
||
Line 897: | Line 897: | ||
300 NEXT F |
300 NEXT F |
||
310 FA$ = MID$ (FA$,O) |
310 FA$ = MID$ (FA$,O) |
||
320 RETURN </ |
320 RETURN </syntaxhighlight> |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
{{trans|Run BASIC}} |
{{trans|Run BASIC}} |
||
< |
<syntaxhighlight lang="freebasic">for i = 1 to 20 |
||
print i; " = "; factorial$(i) |
print i; " = "; factorial$(i) |
||
next i |
next i |
||
Line 920: | Line 920: | ||
end while |
end while |
||
return factor$ |
return factor$ |
||
end function</ |
end function</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
{{trans|Run BASIC}} |
{{trans|Run BASIC}} |
||
< |
<syntaxhighlight lang="qbasic">FUNCTION factorial$ (num) |
||
LET f$ = "" |
LET f$ = "" |
||
LET x$ = "" |
LET x$ = "" |
||
Line 944: | Line 944: | ||
PRINT i; "= "; factorial$(i) |
PRINT i; "= "; factorial$(i) |
||
NEXT i |
NEXT i |
||
END</ |
END</syntaxhighlight> |
||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
{{trans|Run BASIC}} |
{{trans|Run BASIC}} |
||
< |
<syntaxhighlight lang="freebasic">for i = 1 to 20 |
||
print i, " = ", factorial$(i) |
print i, " = ", factorial$(i) |
||
next i |
next i |
||
Line 968: | Line 968: | ||
wend |
wend |
||
return f$ |
return f$ |
||
end sub</ |
end sub</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> FOR i% = 1 TO 20 |
||
PRINT i% " = " FNfactors(i%) |
PRINT i% " = " FNfactors(i%) |
||
NEXT |
NEXT |
||
Line 989: | Line 989: | ||
ENDWHILE |
ENDWHILE |
||
= LEFT$(f$, LEN(f$) - 3) |
= LEFT$(f$, LEN(f$) - 3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> 1 = 1 |
<pre> 1 = 1 |
||
Line 1,015: | Line 1,015: | ||
Lists the first 100 entries in the sequence. If you wish to extend that, the upper limit is implementation dependent, but may be as low as 130 for an interpreter with signed 8 bit data cells (131 is the first prime outside that range). |
Lists the first 100 entries in the sequence. If you wish to extend that, the upper limit is implementation dependent, but may be as low as 130 for an interpreter with signed 8 bit data cells (131 is the first prime outside that range). |
||
< |
<syntaxhighlight lang="befunge">1>>>>:.48*"=",,::1-#v_.v |
||
$<<<^_@#-"e":+1,+55$2<<< |
$<<<^_@#-"e":+1,+55$2<<< |
||
v4_^#-1:/.:g00_00g1+>>0v |
v4_^#-1:/.:g00_00g1+>>0v |
||
>8*"x",,:00g%!^!%g00:p0<</ |
>8*"x",,:00g%!^!%g00:p0<</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,041: | Line 1,041: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Code includes a dynamically extending prime number list. The program doesn't stop until you kill it, or it runs out of memory, or it overflows. |
Code includes a dynamically extending prime number list. The program doesn't stop until you kill it, or it runs out of memory, or it overflows. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,104: | Line 1,104: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 1,125: | Line 1,125: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,170: | Line 1,170: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
using namespace std; |
using namespace std; |
||
Line 1,207: | Line 1,207: | ||
cout << "\n\n"; |
cout << "\n\n"; |
||
return system( "pause" ); |
return system( "pause" ); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,240: | Line 1,240: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(ns listfactors |
||
(:gen-class)) |
(:gen-class)) |
||
Line 1,260: | Line 1,260: | ||
(doseq [q (range 1 26)] |
(doseq [q (range 1 26)] |
||
(println q " = " (clojure.string/join " x "(factors q)))) |
(println q " = " (clojure.string/join " x "(factors q)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,291: | Line 1,291: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript">count_primes = (max) -> |
||
# Count through the natural numbers and give their prime |
# Count through the natural numbers and give their prime |
||
# factorization. This algorithm uses no division. |
# factorization. This algorithm uses no division. |
||
Line 1,330: | Line 1,330: | ||
num_primes = count_primes 10000 |
num_primes = count_primes 10000 |
||
console.log num_primes</ |
console.log num_primes</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Auto extending prime list: |
Auto extending prime list: |
||
< |
<syntaxhighlight lang="lisp">(defparameter *primes* |
||
(make-array 10 :adjustable t :fill-pointer 0 :element-type 'integer)) |
(make-array 10 :adjustable t :fill-pointer 0 :element-type 'integer)) |
||
Line 1,360: | Line 1,360: | ||
(loop for n from 1 do |
(loop for n from 1 do |
||
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</ |
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: |
<pre>1: |
||
Line 1,378: | Line 1,378: | ||
...</pre> |
...</pre> |
||
Without saving the primes, and not all that much slower (probably because above code was not well-written): |
Without saving the primes, and not all that much slower (probably because above code was not well-written): |
||
< |
<syntaxhighlight lang="lisp">(defun factors (n) |
||
(loop with res for x from 2 to (isqrt n) do |
(loop with res for x from 2 to (isqrt n) do |
||
(loop while (zerop (rem n x)) do |
(loop while (zerop (rem n x)) do |
||
Line 1,386: | Line 1,386: | ||
(loop for n from 1 do |
(loop for n from 1 do |
||
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</ |
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">int[] factorize(in int n) pure nothrow |
||
in { |
in { |
||
assert(n > 0); |
assert(n > 0); |
||
Line 1,410: | Line 1,410: | ||
foreach (i; 1 .. 22) |
foreach (i; 1 .. 22) |
||
writefln("%d: %(%d × %)", i, i.factorize()); |
writefln("%d: %(%d × %)", i, i.factorize()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 1,435: | Line 1,435: | ||
===Alternative Version=== |
===Alternative Version=== |
||
{{libheader|uiprimes}} Library ''uiprimes'' is a homebrew library to generate prime numbers upto the maximum 32bit unsigned integer range 2^32-1, by using a pre-generated bit array of [[Sieve of Eratosthenes]] (a dll in size of ~256M bytes :p ). |
{{libheader|uiprimes}} Library ''uiprimes'' is a homebrew library to generate prime numbers upto the maximum 32bit unsigned integer range 2^32-1, by using a pre-generated bit array of [[Sieve of Eratosthenes]] (a dll in size of ~256M bytes :p ). |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.math, std.conv, std.algorithm, |
||
std.array, std.string, import xt.uiprimes; |
std.array, std.string, import xt.uiprimes; |
||
Line 1,466: | Line 1,466: | ||
foreach (i; 1 .. 21) |
foreach (i; 1 .. 21) |
||
writefln("%2d = %s", i, productStr(factorize(i))); |
writefln("%2d = %s", i, productStr(factorize(i))); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|DCL}}== |
=={{header|DCL}}== |
||
Assumes file primes.txt is a list of prime numbers; |
Assumes file primes.txt is a list of prime numbers; |
||
< |
<syntaxhighlight lang="dcl">$ close /nolog primes |
||
$ on control_y then $ goto clean |
$ on control_y then $ goto clean |
||
$ |
$ |
||
Line 1,513: | Line 1,513: | ||
$ |
$ |
||
$ clean: |
$ clean: |
||
$ close /nolog primes</ |
$ close /nolog primes</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ @count_in_factors |
<pre>$ @count_in_factors |
||
Line 1,528: | Line 1,528: | ||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">function Factorize(n : Integer) : String; |
||
begin |
begin |
||
if n <= 1 then |
if n <= 1 then |
||
Line 1,545: | Line 1,545: | ||
var i : Integer; |
var i : Integer; |
||
for i := 1 to 22 do |
for i := 1 to 22 do |
||
PrintLn(IntToStr(i) + ': ' + Factorize(i));</ |
PrintLn(IntToStr(i) + ': ' + Factorize(i));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 1,571: | Line 1,571: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (task (nfrom 2) (range 20)) |
(define (task (nfrom 2) (range 20)) |
||
(for ((i (in-range nfrom (+ nfrom range)))) |
(for ((i (in-range nfrom (+ nfrom range)))) |
||
(writeln i "=" (string-join (prime-factors i) " x ")))) |
(writeln i "=" (string-join (prime-factors i) " x ")))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,604: | Line 1,604: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
Line 1,671: | Line 1,671: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test Output: |
Test Output: |
||
Line 1,701: | Line 1,701: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def factor(n), do: factor(n, 2, []) |
def factor(n), do: factor(n, 2, []) |
||
Line 1,712: | Line 1,712: | ||
Enum.each(1..20, fn n -> |
Enum.each(1..20, fn n -> |
||
IO.puts "#{n}: #{Enum.join(RC.factor(n)," x ")}" end)</ |
IO.puts "#{n}: #{Enum.join(RC.factor(n)," x ")}" end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,739: | Line 1,739: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function factorize(integer n) |
||
sequence result |
sequence result |
||
integer k |
integer k |
||
Line 1,766: | Line 1,766: | ||
end for |
end for |
||
printf(1, "%d\n", factors[$]) |
printf(1, "%d\n", factors[$]) |
||
end for</ |
end for</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 1,793: | Line 1,793: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let factorsOf (num) = |
||
Seq.unfold (fun (f, n) -> |
Seq.unfold (fun (f, n) -> |
||
let rec genFactor (f, n) = |
let rec genFactor (f, n) = |
||
Line 1,803: | Line 1,803: | ||
let showLines = Seq.concat (seq { yield seq{ yield(Seq.singleton 1)}; yield (Seq.skip 2 (Seq.initInfinite factorsOf))}) |
let showLines = Seq.concat (seq { yield seq{ yield(Seq.singleton 1)}; yield (Seq.skip 2 (Seq.initInfinite factorsOf))}) |
||
showLines |> Seq.iteri (fun i f -> printfn "%d = %s" (i+1) (String.Join(" * ", Seq.toArray f)))</ |
showLines |> Seq.iteri (fun i f -> printfn "%d = %s" (i+1) (String.Join(" * ", Seq.toArray f)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 1,828: | Line 1,828: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math.primes.factors math.ranges prettyprint |
||
sequences ; |
sequences ; |
||
Line 1,835: | Line 1,835: | ||
[ " × " write ] [ pprint ] interleave nl ; |
[ " × " write ] [ pprint ] interleave nl ; |
||
"1: 1" print 2 20 [a,b] [ .factors ] each</ |
"1: 1" print 2 20 [a,b] [ .factors ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,861: | Line 1,861: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: .factors ( n -- ) |
||
2 |
2 |
||
begin 2dup dup * >= |
begin 2dup dup * >= |
||
Line 1,875: | Line 1,875: | ||
1+ 2 ?do i . ." : " i .factors cr loop ; |
1+ 2 ?do i . ." : " i .factors cr loop ; |
||
15 main bye</ |
15 main bye</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 1,882: | Line 1,882: | ||
This algorithm creates a sieve of Eratosthenes, storing the largest prime factor to mark composites. It then finds prime factors by repeatedly looking up the value in the sieve, then dividing by the factor found until the value is itself prime. Using the sieve table to store factors rather than as a plain bitmap was to me a novel idea. |
This algorithm creates a sieve of Eratosthenes, storing the largest prime factor to mark composites. It then finds prime factors by repeatedly looking up the value in the sieve, then dividing by the factor found until the value is itself prime. Using the sieve table to store factors rather than as a plain bitmap was to me a novel idea. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
!-*- mode: compilation; default-directory: "/tmp/" -*- |
!-*- mode: compilation; default-directory: "/tmp/" -*- |
||
!Compilation started at Thu Jun 6 23:29:06 |
!Compilation started at Thu Jun 6 23:29:06 |
||
Line 2,009: | Line 2,009: | ||
call sieve(0) ! release memory |
call sieve(0) ! release memory |
||
end program count_in_factors |
end program count_in_factors |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Sub getPrimeFactors(factors() As UInteger, n As UInteger) |
Sub getPrimeFactors(factors() As UInteger, n As UInteger) |
||
Line 2,049: | Line 2,049: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,077: | Line 2,077: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Frink's factoring routines work on arbitrarily-large integers. |
Frink's factoring routines work on arbitrarily-large integers. |
||
< |
<syntaxhighlight lang="frink">i = 1 |
||
while true |
while true |
||
{ |
{ |
||
println[join[" x ", factorFlat[i]]] |
println[join[" x ", factorFlat[i]]] |
||
i = i + 1 |
i = i + 1 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
Line 2,093: | Line 2,093: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,111: | Line 2,111: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,128: | Line 2,128: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def factors(number) { |
||
if (number == 1) { |
if (number == 1) { |
||
return [1] |
return [1] |
||
Line 2,149: | Line 2,149: | ||
((1..10) + (6351..6359)).each { number -> |
((1..10) + (6351..6359)).each { number -> |
||
println "$number = ${number.factors().join(' x ')}" |
println "$number = ${number.factors().join(' x ')}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 2,173: | Line 2,173: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Using <code>factorize</code> function from the [[Prime_decomposition#Haskell|prime decomposition]] task, |
Using <code>factorize</code> function from the [[Prime_decomposition#Haskell|prime decomposition]] task, |
||
< |
<syntaxhighlight lang="haskell">import Data.List (intercalate) |
||
showFactors n = show n ++ " = " ++ (intercalate " * " . map show . factorize) n |
showFactors n = show n ++ " = " ++ (intercalate " * " . map show . factorize) n |
||
-- Pointfree form |
-- Pointfree form |
||
showFactors = ((++) . show) <*> ((" = " ++) . intercalate " * " . map show . factorize)</ |
showFactors = ((++) . show) <*> ((" = " ++) . intercalate " * " . map show . factorize)</syntaxhighlight> |
||
isPrime n = n > 1 && noDivsBy primeNums n |
isPrime n = n > 1 && noDivsBy primeNums n |
||
{{out}} |
{{out}} |
||
<small>< |
<small><syntaxhighlight lang="haskell">Main> print 1 >> mapM_ (putStrLn . showFactors) [2..] |
||
1 |
1 |
||
2 = 2 |
2 = 2 |
||
Line 2,221: | Line 2,221: | ||
121231231232164 = 2 * 2 * 253811 * 119410931 |
121231231232164 = 2 * 2 * 253811 * 119410931 |
||
121231231232165 = 5 * 137 * 176979899609 |
121231231232165 = 5 * 137 * 176979899609 |
||
. . .</ |
. . .</syntaxhighlight></small> |
||
The real solution seems to have to be some sort of a segmented offset sieve of Eratosthenes, storing factors in array's cells instead of just marks. That way the speed of production might not be diminishing as much. |
The real solution seems to have to be some sort of a segmented offset sieve of Eratosthenes, storing factors in array's cells instead of just marks. That way the speed of production might not be diminishing as much. |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
write("Press ^C to terminate") |
write("Press ^C to terminate") |
||
every f := [i:= 1] | factors(i := seq(2)) do { |
every f := [i:= 1] | factors(i := seq(2)) do { |
||
Line 2,233: | Line 2,233: | ||
end |
end |
||
link factors</ |
link factors</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
[http://www.cs.arizona.edu/icon/library/src/procs/factors.icn factors.icn provides factors] |
[http://www.cs.arizona.edu/icon/library/src/procs/factors.icn factors.icn provides factors] |
||
Line 2,256: | Line 2,256: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Factors.bas" |
||
110 FOR I=1 TO 30 |
110 FOR I=1 TO 30 |
||
120 PRINT I;"= ";FACTORS$(I) |
120 PRINT I;"= ";FACTORS$(I) |
||
Line 2,276: | Line 2,276: | ||
280 LET FACTORS$=F$(1:LEN(F$)-1) |
280 LET FACTORS$=F$(1:LEN(F$)-1) |
||
290 END IF |
290 END IF |
||
300 END DEF</ |
300 END DEF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 = 1 |
<pre> 1 = 1 |
||
Line 2,310: | Line 2,310: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''':Use J's factoring primitive, <lang |
'''Solution''':Use J's factoring primitive, <syntaxhighlight lang="j">q:</syntaxhighlight> |
||
'''Example''' (including formatting):< |
'''Example''' (including formatting):<syntaxhighlight lang="j"> ('1 : 1',":&> ,"1 ': ',"1 ":@q:) 2+i.10 |
||
1 : 1 |
1 : 1 |
||
2 : 2 |
2 : 2 |
||
Line 2,322: | Line 2,322: | ||
9 : 3 3 |
9 : 3 3 |
||
10: 2 5 |
10: 2 5 |
||
11: 11</ |
11: 11</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Visual Basic .NET}} |
{{trans|Visual Basic .NET}} |
||
< |
<syntaxhighlight lang="java">public class CountingInFactors{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
for(int i = 1; i<= 10; i++){ |
for(int i = 1; i<= 10; i++){ |
||
Line 2,366: | Line 2,366: | ||
return n; |
return n; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 2,390: | Line 2,390: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">for(i = 1; i <= 10; i++) |
||
console.log(i + " : " + factor(i).join(" x ")); |
console.log(i + " : " + factor(i).join(" x ")); |
||
Line 2,404: | Line 2,404: | ||
} |
} |
||
return factors; |
return factors; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,430: | Line 2,430: | ||
reliable for integers up to and including 9,007,199,254,740,992 (2^53). However, "factors" |
reliable for integers up to and including 9,007,199,254,740,992 (2^53). However, "factors" |
||
could be easily modified to work with a "BigInt" library for jq, such as [https://gist.github.com/pkoppstein/d06a123f30c033195841 BigInt.jq]. |
could be easily modified to work with a "BigInt" library for jq, such as [https://gist.github.com/pkoppstein/d06a123f30c033195841 BigInt.jq]. |
||
< |
<syntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic: |
||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
||
Line 2,441: | Line 2,441: | ||
if . == 1 then "1: 1" else empty end, |
if . == 1 then "1: 1" else empty end, |
||
(range($m;$n) | "\(.): \([factors] | join("x"))"); |
(range($m;$n) | "\(.): \([factors] | join("x"))"); |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Examples''' |
'''Examples''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
10 | count_in_factors, |
10 | count_in_factors, |
||
"", |
"", |
||
count_in_factors(2144; 2145), |
count_in_factors(2144; 2145), |
||
"", |
"", |
||
(2|power(100) | count_in_factors(.; .+ 2))</ |
(2|power(100) | count_in_factors(.; .+ 2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
The output shown here is based on a run of gojq. |
The output shown here is based on a run of gojq. |
||
Line 2,470: | Line 2,470: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes, Printf |
||
function strfactor(n::Integer) |
function strfactor(n::Integer) |
||
n > -2 || return "-1 × " * strfactor(-n) |
n > -2 || return "-1 × " * strfactor(-n) |
||
Line 2,482: | Line 2,482: | ||
for n in lo:hi |
for n in lo:hi |
||
@printf("%5d = %s\n", n, strfactor(n)) |
@printf("%5d = %s\n", n, strfactor(n)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,533: | Line 2,533: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun isPrime(n: Int) : Boolean { |
fun isPrime(n: Int) : Boolean { |
||
Line 2,574: | Line 2,574: | ||
for (i in list) |
for (i in list) |
||
println("${"%4d".format(i)} = ${getPrimeFactors(i).joinToString(" * ")}") |
println("${"%4d".format(i)} = ${getPrimeFactors(i).joinToString(" * ")}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,605: | Line 2,605: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
'see Run BASIC solution |
'see Run BASIC solution |
||
for i = 1000 to 1016 |
for i = 1000 to 1016 |
||
Line 2,623: | Line 2,623: | ||
end if |
end if |
||
wend |
wend |
||
end function </ |
end function </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,646: | Line 2,646: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function factorize( n ) |
||
if n == 1 then return {1} end |
if n == 1 then return {1} end |
||
Line 2,669: | Line 2,669: | ||
end |
end |
||
print "" |
print "" |
||
end</ |
end</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Decompose function now return array (in number decomposition task return an inventory list). |
Decompose function now return array (in number decomposition task return an inventory list). |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Count_in_factors { |
Module Count_in_factors { |
||
Inventory Known1=2@, 3@ |
Inventory Known1=2@, 3@ |
||
Line 2,730: | Line 2,730: | ||
} |
} |
||
Count_in_factors |
Count_in_factors |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">define(`for', |
||
`ifelse($#,0,``$0'', |
`ifelse($#,0,``$0'', |
||
`ifelse(eval($2<=$3),1, |
`ifelse(eval($2<=$3),1, |
||
Line 2,749: | Line 2,749: | ||
for(`y',1,25,1, `wby(y) |
for(`y',1,25,1, `wby(y) |
||
')</ |
')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,781: | Line 2,781: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">factorNum := proc(n) |
||
local i, j, firstNum; |
local i, j, firstNum; |
||
if n = 1 then |
if n = 1 then |
||
Line 2,804: | Line 2,804: | ||
printf("%2a: ", i); |
printf("%2a: ", i); |
||
factorNum(i); |
factorNum(i); |
||
end do;</ |
end do;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,820: | Line 2,820: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">n = 2; |
||
While[n < 100, |
While[n < 100, |
||
Print[Row[Riffle[Flatten[Map[Apply[ConstantArray, #] &, FactorInteger[n]]],"*"]]]; |
Print[Row[Riffle[Flatten[Map[Apply[ConstantArray, #] &, FactorInteger[n]]],"*"]]]; |
||
n++]</ |
n++]</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 2,879: | Line 2,879: | ||
end |
end |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height: 30em;overflow: scroll; font-size: smaller;"> |
<pre style="height: 30em;overflow: scroll; font-size: smaller;"> |
||
Line 2,957: | Line 2,957: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="nim">var primes = newSeq[int]() |
||
proc getPrime(idx: int): int = |
proc getPrime(idx: int): int = |
||
Line 2,995: | Line 2,995: | ||
if first > 0: echo n |
if first > 0: echo n |
||
elif n > 1: echo " x ", n |
elif n > 1: echo " x ", n |
||
else: echo ""</ |
else: echo ""</syntaxhighlight> |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
2 = 2 |
2 = 2 |
||
Line 3,013: | Line 3,013: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
class CountingInFactors { |
class CountingInFactors { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 3,067: | Line 3,067: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,093: | Line 3,093: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">open Big_int |
||
let prime_decomposition x = |
let prime_decomposition x = |
||
Line 3,114: | Line 3,114: | ||
aux (succ_big_int v) |
aux (succ_big_int v) |
||
in |
in |
||
aux unit_big_int</ |
aux unit_big_int</syntaxhighlight> |
||
{{out|Execution}} |
{{out|Execution}} |
||
<pre>$ ocamlopt -o count.opt nums.cmxa count.ml |
<pre>$ ocamlopt -o count.opt nums.cmxa count.ml |
||
Line 3,140: | Line 3,140: | ||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
Octave's factor function returns an array: |
Octave's factor function returns an array: |
||
< |
<syntaxhighlight lang="octave">for (n = 1:20) |
||
printf ("%i: ", n) |
printf ("%i: ", n) |
||
printf ("%i ", factor (n)) |
printf ("%i ", factor (n)) |
||
printf ("\n") |
printf ("\n") |
||
endfor</ |
endfor</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 3,168: | Line 3,168: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">fnice(n)={ |
||
my(f,s="",s1); |
my(f,s="",s1); |
||
if (n < 2, return(n)); |
if (n < 2, return(n)); |
||
Line 3,181: | Line 3,181: | ||
s |
s |
||
}; |
}; |
||
n=0;while(n++, print(fnice(n)))</ |
n=0;while(n++, print(fnice(n)))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
< |
<syntaxhighlight lang="pascal">program CountInFactors(output); |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
Line 3,233: | Line 3,233: | ||
writeln; |
writeln; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,262: | Line 3,262: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Typically one would use a module for this. Note that these modules all return an empty list for '1'. This should be efficient to 50+ digits:{{libheader|ntheory}} |
Typically one would use a module for this. Note that these modules all return an empty list for '1'. This should be efficient to 50+ digits:{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/factor/; |
||
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</ |
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1000000000000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 |
<pre>1000000000000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 |
||
Line 3,278: | Line 3,278: | ||
Giving similar output and also good for large inputs: |
Giving similar output and also good for large inputs: |
||
< |
<syntaxhighlight lang="perl">use Math::Pari qw/factorint/; |
||
sub factor { |
sub factor { |
||
my ($pn,$pc) = @{Math::Pari::factorint(shift)}; |
my ($pn,$pc) = @{Math::Pari::factorint(shift)}; |
||
return map { ($pn->[$_]) x $pc->[$_] } 0 .. $#$pn; |
return map { ($pn->[$_]) x $pc->[$_] } 0 .. $#$pn; |
||
} |
} |
||
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</ |
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</syntaxhighlight> |
||
or, somewhat slower and limited to native 32-bit or 64-bit integers only: |
or, somewhat slower and limited to native 32-bit or 64-bit integers only: |
||
< |
<syntaxhighlight lang="perl">use Math::Factor::XS qw/prime_factors/; |
||
print "$_ = ", join(" x ", prime_factors($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</ |
print "$_ = ", join(" x ", prime_factors($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</syntaxhighlight> |
||
If we want to implement it self-contained, we could use the prime decomposition routine from the [[Prime_decomposition]] task. This is reasonably fast and small, though much slower than the modules and certainly could have more optimization. |
If we want to implement it self-contained, we could use the prime decomposition routine from the [[Prime_decomposition]] task. This is reasonably fast and small, though much slower than the modules and certainly could have more optimization. |
||
< |
<syntaxhighlight lang="perl">sub factors { |
||
my($n, $p, @out) = (shift, 3); |
my($n, $p, @out) = (shift, 3); |
||
return if $n < 1; |
return if $n < 1; |
||
Line 3,306: | Line 3,306: | ||
} |
} |
||
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000100;</ |
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000100;</syntaxhighlight> |
||
We could use the second extensible sieve from [[Sieve_of_Eratosthenes#Extensible_sieves]] to only divide by primes. |
We could use the second extensible sieve from [[Sieve_of_Eratosthenes#Extensible_sieves]] to only divide by primes. |
||
< |
<syntaxhighlight lang="perl">tie my @primes, 'Tie::SieveOfEratosthenes'; |
||
sub factors { |
sub factors { |
||
Line 3,324: | Line 3,324: | ||
} |
} |
||
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000010;</ |
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000010;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>100000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 |
<pre>100000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 |
||
Line 3,339: | Line 3,339: | ||
This next example isn't quite as fast and uses much more memory, but it is self-contained and shows a different approach. As written it must start at 1, but a range can be handled by using a <code>map</code> to prefill the <tt>p_and_sq</tt> array. |
This next example isn't quite as fast and uses much more memory, but it is self-contained and shows a different approach. As written it must start at 1, but a range can be handled by using a <code>map</code> to prefill the <tt>p_and_sq</tt> array. |
||
< |
<syntaxhighlight lang="perl">#!perl -C |
||
use utf8; |
use utf8; |
||
use strict; |
use strict; |
||
Line 3,370: | Line 3,370: | ||
} |
} |
||
die "Ran out of primes?!"; |
die "Ran out of primes?!"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">factorise</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">factorise</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
||
Line 3,382: | Line 3,382: | ||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)&{</span><span style="color: #000000;">2144</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000000</span><span style="color: #0000FF;">},</span><span style="color: #000000;">factorise</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)&{</span><span style="color: #000000;">2144</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000000</span><span style="color: #0000FF;">},</span><span style="color: #000000;">factorise</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,401: | Line 3,401: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
This is the 'factor' function from [[Prime decomposition#PicoLisp]]. |
This is the 'factor' function from [[Prime decomposition#PicoLisp]]. |
||
< |
<syntaxhighlight lang="picolisp">(de factor (N) |
||
(make |
(make |
||
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N)) |
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N)) |
||
Line 3,411: | Line 3,411: | ||
(for N 20 |
(for N 20 |
||
(prinl N ": " (glue " * " (factor N))) )</ |
(prinl N ": " (glue " * " (factor N))) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 3,435: | Line 3,435: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
cnt: procedure options (main); |
cnt: procedure options (main); |
||
declare (i, k, n) fixed binary; |
declare (i, k, n) fixed binary; |
||
Line 3,460: | Line 3,460: | ||
end; |
end; |
||
end cnt; |
end cnt; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
<pre> 1 = 1 |
<pre> 1 = 1 |
||
Line 3,505: | Line 3,505: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function eratosthenes ($n) { |
function eratosthenes ($n) { |
||
if($n -ge 1){ |
if($n -ge 1){ |
||
Line 3,538: | Line 3,538: | ||
"$(prime-decomposition 100)" |
"$(prime-decomposition 100)" |
||
"$(prime-decomposition 12)" |
"$(prime-decomposition 12)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 3,547: | Line 3,547: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure Factorize(Number, List Factors()) |
||
Protected I = 3, Max |
Protected I = 3, Max |
||
ClearList(Factors()) |
ClearList(Factors()) |
||
Line 3,584: | Line 3,584: | ||
PrintN(text$) |
PrintN(text$) |
||
Next a |
Next a |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1= 1 |
<pre> 1= 1 |
||
Line 3,609: | Line 3,609: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
This uses the [http://docs.python.org/dev/library/functools.html#functools.lru_cache functools.lru_cache] standard library module to cache intermediate results. |
This uses the [http://docs.python.org/dev/library/functools.html#functools.lru_cache functools.lru_cache] standard library module to cache intermediate results. |
||
< |
<syntaxhighlight lang="python">from functools import lru_cache |
||
primes = [2, 3, 5, 7, 11, 13, 17] # Will be extended |
primes = [2, 3, 5, 7, 11, 13, 17] # Will be extended |
||
Line 3,643: | Line 3,643: | ||
print('\nNumber of primes gathered up to', n, 'is', len(primes)) |
print('\nNumber of primes gathered up to', n, 'is', len(primes)) |
||
print(pfactor.cache_info())</ |
print(pfactor.cache_info())</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 1 |
<pre> 1 1 |
||
Line 3,686: | Line 3,686: | ||
Reusing the code from [http://rosettacode.org/wiki/Prime_decomposition#Quackery Prime Decomposition]. |
Reusing the code from [http://rosettacode.org/wiki/Prime_decomposition#Quackery Prime Decomposition]. |
||
< |
<syntaxhighlight lang="quackery"> [ [] swap |
||
dup times |
dup times |
||
[ [ dup i^ 2 + /mod |
[ [ dup i^ 2 + /mod |
||
Line 3,704: | Line 3,704: | ||
cr again ] ] is countinfactors ( --> ) |
cr again ] ] is countinfactors ( --> ) |
||
countinfactors</ |
countinfactors</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,734: | Line 3,734: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
#initially I created a function which returns prime factors then I have created another function counts in the factors and #prints the values. |
#initially I created a function which returns prime factors then I have created another function counts in the factors and #prints the values. |
||
Line 3,761: | Line 3,761: | ||
} |
} |
||
count_in_factors(72) |
count_in_factors(72) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,771: | Line 3,771: | ||
See also [[#Scheme]]. This uses Racket’s <code>math/number-theory</code> package |
See also [[#Scheme]]. This uses Racket’s <code>math/number-theory</code> package |
||
< |
<syntaxhighlight lang="racket">#lang typed/racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
Line 3,792: | Line 3,792: | ||
(factor-count 1 22) |
(factor-count 1 22) |
||
(factor-count 2140 2150) |
(factor-count 2140 2150) |
||
; tb</ |
; tb</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,833: | Line 3,833: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|rakudo|2015-10-01}} |
{{works with|rakudo|2015-10-01}} |
||
<lang |
<syntaxhighlight lang="raku" line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime; |
||
multi factors(1) { 1 } |
multi factors(1) { 1 } |
||
Line 3,853: | Line 3,853: | ||
} |
} |
||
say "$_: ", factors($_).join(" × ") for 1..*;</ |
say "$_: ", factors($_).join(" × ") for 1..*;</syntaxhighlight> |
||
The first twenty numbers: |
The first twenty numbers: |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 3,881: | Line 3,881: | ||
Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime. |
Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime. |
||
<lang |
<syntaxhighlight lang="raku" line>sub factor($n is copy) { |
||
$n == 1 ?? 1 !! |
$n == 1 ?? 1 !! |
||
gather { |
gather { |
||
Line 3,893: | Line 3,893: | ||
} |
} |
||
say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</ |
say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</syntaxhighlight> |
||
Same output as above. |
Same output as above. |
||
Line 3,900: | Line 3,900: | ||
Alternately, use a module: |
Alternately, use a module: |
||
<lang |
<syntaxhighlight lang="raku" line>use Prime::Factor; |
||
say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</ |
say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 3,934: | Line 3,934: | ||
<br>prime factors are listed, but the number of primes found is always shown. The showing of the count of |
<br>prime factors are listed, but the number of primes found is always shown. The showing of the count of |
||
<br>primes was included to help verify the factoring (of composites). |
<br>primes was included to help verify the factoring (of composites). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
||
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
||
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
||
Line 3,970: | Line 3,970: | ||
end /*j*/ |
end /*j*/ |
||
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
||
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</ |
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 4,049: | Line 4,049: | ||
Note that the '''integer square root''' section of code doesn't use any floating point numbers, just integers. |
Note that the '''integer square root''' section of code doesn't use any floating point numbers, just integers. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program lists the prime factors of a specified integer (or a range of integers).*/ |
||
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/ |
||
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
parse arg LO HI @ . /*get optional arguments from the C.L. */ |
||
Line 4,099: | Line 4,099: | ||
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/ |
||
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</ |
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 4,147: | Line 4,147: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
for i = 1 to 20 |
for i = 1 to 20 |
||
see "" + i + " = " + factors(i) + nl |
see "" + i + " = " + factors(i) + nl |
||
Line 4,163: | Line 4,163: | ||
end |
end |
||
return left(f, len(f) - 3) |
return left(f, len(f) - 3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,190: | Line 4,190: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Starting with Ruby 1.9, 'prime' is part of the standard library and provides Integer#prime_division. |
Starting with Ruby 1.9, 'prime' is part of the standard library and provides Integer#prime_division. |
||
< |
<syntaxhighlight lang="ruby">require 'optparse' |
||
require 'prime' |
require 'prime' |
||
Line 4,212: | Line 4,212: | ||
end.join " x " |
end.join " x " |
||
puts "#{i} is #{f}" |
puts "#{i} is #{f}" |
||
end</ |
end</syntaxhighlight> |
||
{{out|Example}} |
{{out|Example}} |
||
<pre>$ ruby prime-count.rb -h |
<pre>$ ruby prime-count.rb -h |
||
Line 4,240: | Line 4,240: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">for i = 1000 to 1016 |
||
print i;" = "; factorial$(i) |
print i;" = "; factorial$(i) |
||
next |
next |
||
Line 4,256: | Line 4,256: | ||
end if |
end if |
||
wend |
wend |
||
end function</ |
end function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1000 = 2 x 2 x 2 x 5 x 5 x 5 |
<pre>1000 = 2 x 2 x 2 x 5 x 5 x 5 |
||
Line 4,278: | Line 4,278: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
You can run and experiment with this code at https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b66c14d944ff0472d2460796513929e2 |
You can run and experiment with this code at https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b66c14d944ff0472d2460796513929e2 |
||
< |
<syntaxhighlight lang="rust">use std::env; |
||
fn main() { |
fn main() { |
||
Line 4,318: | Line 4,318: | ||
} |
} |
||
vec![n] |
vec![n] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 4,343: | Line 4,343: | ||
=={{header|Sage}}== |
=={{header|Sage}}== |
||
< |
<syntaxhighlight lang="python">def count_in_factors(n): |
||
if is_prime(n) or n == 1: |
if is_prime(n) or n == 1: |
||
print(n,end="") |
print(n,end="") |
||
Line 4,358: | Line 4,358: | ||
print(i,"=",end=" ") |
print(i,"=",end=" ") |
||
count_in_factors(i) |
count_in_factors(i) |
||
print("")</ |
print("")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,422: | Line 4,422: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
object CountInFactors extends App { |
object CountInFactors extends App { |
||
Line 4,450: | Line 4,450: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 1 : 1 |
<pre> 1 : 1 |
||
Line 4,468: | Line 4,468: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="lisp">(define (factors n) |
||
(let facs ((l '()) (d 2) (x n)) |
(let facs ((l '()) (d 2) (x n)) |
||
(cond ((= x 1) (if (null? l) '(1) l)) |
(cond ((= x 1) (if (null? l) '(1) l)) |
||
Line 4,487: | Line 4,487: | ||
(display i) |
(display i) |
||
(display " = ") |
(display " = ") |
||
(show (reverse (factors i))))</ |
(show (reverse (factors i))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 = 1 |
<pre>1 = 1 |
||
Line 4,504: | Line 4,504: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: writePrimeFactors (in var integer: number) is func |
const proc: writePrimeFactors (in var integer: number) is func |
||
Line 4,542: | Line 4,542: | ||
writeln; |
writeln; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,564: | Line 4,564: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">class Counter { |
||
method factors(n, p=2) { |
method factors(n, p=2) { |
||
var a = gather { |
var a = gather { |
||
Line 4,592: | Line 4,592: | ||
for i in (1..100) { |
for i in (1..100) { |
||
say "#{i} = #{Counter().factors(i).join(' × ')}" |
say "#{i} = #{Counter().factors(i).join(' × ')}" |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">extension BinaryInteger { |
||
@inlinable |
@inlinable |
||
public func primeDecomposition() -> [Self] { |
public func primeDecomposition() -> [Self] { |
||
Line 4,624: | Line 4,624: | ||
print("\(i) = \(i.primeDecomposition().map(String.init).joined(separator: " x "))") |
print("\(i) = \(i.primeDecomposition().map(String.init).joined(separator: " x "))") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,651: | Line 4,651: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
This factorization code is based on the same engine that is used in the [[Parallel calculations#Tcl|parallel computation task]]. |
This factorization code is based on the same engine that is used in the [[Parallel calculations#Tcl|parallel computation task]]. |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
namespace eval prime { |
namespace eval prime { |
||
Line 4,712: | Line 4,712: | ||
return [join $v "*"] |
return [join $v "*"] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Demonstration code: |
Demonstration code: |
||
< |
<syntaxhighlight lang="tcl">set max 20 |
||
for {set i 1} {$i <= $max} {incr i} { |
for {set i 1} {$i <= $max} {incr i} { |
||
puts [format "%*d = %s" [string length $max] $i [prime::factors.rendered $i]] |
puts [format "%*d = %s" [string length $max] $i [prime::factors.rendered $i]] |
||
}</ |
}</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Made minor modifications on the code I posted under Prime Decomposition. |
Made minor modifications on the code I posted under Prime Decomposition. |
||
< |
<syntaxhighlight lang="vb">Function CountFactors(n) |
||
If n = 1 Then |
If n = 1 Then |
||
CountFactors = 1 |
CountFactors = 1 |
||
Line 4,779: | Line 4,779: | ||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
WScript.StdOut.Write "2144 = " & CountFactors(2144) |
WScript.StdOut.Write "2144 = " & CountFactors(2144) |
||
WScript.StdOut.WriteLine</ |
WScript.StdOut.WriteLine</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,788: | Line 4,788: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vbnet">Module CountingInFactors |
||
Sub Main() |
Sub Main() |
||
Line 4,829: | Line 4,829: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,856: | Line 4,856: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
println("1: 1") |
println("1: 1") |
||
for i := 2; ; i++ { |
for i := 2; ; i++ { |
||
Line 4,870: | Line 4,870: | ||
println('') |
println('') |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,887: | Line 4,887: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
int N0, N, F; |
int N0, N, F; |
||
[N0:= 1; |
[N0:= 1; |
||
Line 4,903: | Line 4,903: | ||
N0:= N0+1; |
N0:= N0+1; |
||
until KeyHit; |
until KeyHit; |
||
]</ |
]</syntaxhighlight> |
||
Example output: |
Example output: |
||
Line 4,942: | Line 4,942: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
for (r in [1..9, 2144..2154, 9987..9999]) { |
for (r in [1..9, 2144..2154, 9987..9999]) { |
||
Line 4,950: | Line 4,950: | ||
} |
} |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,992: | Line 4,992: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">foreach n in ([1..*]){ println(n,": ",primeFactors(n).concat("\U2715;")) }</syntaxhighlight> |
||
Using the fixed size integer (64 bit) solution from [[Prime decomposition#zkl]] |
Using the fixed size integer (64 bit) solution from [[Prime decomposition#zkl]] |
||
< |
<syntaxhighlight lang="zkl">fcn primeFactors(n){ // Return a list of factors of n |
||
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum |
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum |
||
if(n==1 or k>maxD) acc.close(); |
if(n==1 or k>maxD) acc.close(); |
||
Line 5,006: | Line 5,006: | ||
if(n!=m) acc.append(n/m); // opps, missed last factor |
if(n!=m) acc.append(n/m); // opps, missed last factor |
||
else acc; |
else acc; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,025: | Line 5,025: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
{{trans|BBC_BASIC}} |
{{trans|BBC_BASIC}} |
||
< |
<syntaxhighlight lang="zxbasic">10 FOR i=1 TO 20 |
||
20 PRINT i;" = "; |
20 PRINT i;" = "; |
||
30 IF i=1 THEN PRINT 1: GO TO 90 |
30 IF i=1 THEN PRINT 1: GO TO 90 |
||
Line 5,035: | Line 5,035: | ||
90 NEXT i |
90 NEXT i |
||
100 STOP |
100 STOP |
||
110 DEF FN m(a,b)=a-INT (a/b)*b</ |
110 DEF FN m(a,b)=a-INT (a/b)*b</syntaxhighlight> |