Count in factors: Difference between revisions
PascalABC.NET
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
(PascalABC.NET) |
||
(24 intermediate revisions by 15 users not shown) | |||
Line 24:
=={{header|11l}}==
{{trans|C++}}<
I li == 1
R ‘1’
Line 43:
L(x) 1..17
print(‘#4: #.’.format(x, get_prime_factors(x)))
print(‘2144: ’get_prime_factors(2144))</
{{out}}
Line 68:
=={{header|360 Assembly}}==
<
COUNTFAC CSECT assist plig\COUNTFAC
USING COUNTFAC,R13 base register
Line 141:
PG DS CL80 buffer
YREGS
END COUNTFAC</
{{out}}
<pre style="height:20ex">
Line 184:
39=3*13
40=2*2*2*5
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintFactors(CARD a)
BYTE notFirst
CARD p
IF a=1 THEN
PrintC(a) RETURN
FI
p=2 notFirst=0
WHILE p<=a
DO
IF a MOD p=0 THEN
IF notFirst THEN
Put('x)
FI
notFirst=1
PrintC(p)
a==/p
ELSE
p==+1
FI
OD
RETURN
PROC Main()
CARD i
FOR i=1 TO 1000
DO
PrintC(i) Put('=)
PrintFactors(i)
PutE()
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_in_factors.png Screenshot from Atari 8-bit computer]
<pre>
1=1
2=2
3=3
4=2x2
5=5
...
995=5x199
996=2x2x3x83
997=997
998=2x499
999=3x3x3x37
1000=2x2x2x5x5x5
</pre>
Line 191 ⟶ 243:
;count.adb:
<
procedure Count is
Line 220 ⟶ 272:
exit when N > Max_N;
end loop;
end Count;</
{{out}}
Line 240 ⟶ 292:
=={{header|ALGOL 68}}==
{{trans|Euphoria}}<syntaxhighlight lang
OP +:= = (REF FLEX []INT a, INT b) VOID:
BEGIN
[
c[:
c[
a := c
END;
Line 275 ⟶ 328:
OD;
print ((new line))
OD
</syntaxhighlight>
{{out}}
<pre>1 = 1
Line 299 ⟶ 353:
21 = 3 × 7
22 = 2 × 11</pre>
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % show numbers and their prime factors %
% shows nand its prime factors %
procedure showFactors ( integer value n ) ;
if n <= 3 then write( i_w := 1, s_w := 0, n, ": ", n )
else begin
integer v, f; logical first;
first := true;
v := n;
write( i_w := 1, s_w := 0, n, ": " );
while not odd( v ) and v > 1 do begin
if not first then writeon( s_w := 0, " x " );
writeon( i_w := 1, s_w := 0, 2 );
v := v div 2;
first := false
end while_not_odd_v ;
f := 1;
while v > 1 do begin
f := f + 2;
while v rem f = 0 do begin
if not first then writeon( s_w := 0, " x " );
writeon( i_w := 1, s_w := 0, f );
v := v div f;
first := false
end while_v_rem_f_eq_0
end while_v_gt_0_and_f_le_v
end showFactors ;
% show the factors of various ranges - same as Wren %
for i := 1 until 9 do showFactors( i );
write( "... " );
for i := 2144 until 2154 do showFactors( i );
write( "... " );
for i := 9987 until 9999 do showFactors( i )
end.
</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 2
3: 3
4: 2 x 2
5: 5
6: 2 x 3
7: 7
8: 2 x 2 x 2
9: 3 x 3
...
2144: 2 x 2 x 2 x 2 x 2 x 67
2145: 3 x 5 x 11 x 13
2146: 2 x 29 x 37
2147: 19 x 113
2148: 2 x 2 x 3 x 179
2149: 7 x 307
2150: 2 x 5 x 5 x 43
2151: 3 x 3 x 239
2152: 2 x 2 x 2 x 269
2153: 2153
2154: 2 x 3 x 359
...
9987: 3 x 3329
9988: 2 x 2 x 11 x 227
9989: 7 x 1427
9990: 2 x 3 x 3 x 3 x 5 x 37
9991: 97 x 103
9992: 2 x 2 x 2 x 1249
9993: 3 x 3331
9994: 2 x 19 x 263
9995: 5 x 1999
9996: 2 x 2 x 3 x 7 x 7 x 17
9997: 13 x 769
9998: 2 x 4999
9999: 3 x 3 x 11 x 101
</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program countFactors.s */
Line 683 ⟶ 814:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Number 2144 : 2 2 2 2 2 67
Line 689 ⟶ 820:
=={{header|Arturo}}==
<
fs: [1]
if x<>1 -> fs: factors.prime x
print [pad to :string x 3 "=" join.with:" x " to [:string] fs]
]</
{{out}}
Line 730 ⟶ 861:
=={{header|AutoHotkey}}==
{{trans|D}}
<
if n = 1
return 1
Line 746 ⟶ 877:
Loop 22
out .= A_Index ": " factorize(A_index) "`n"
MsgBox % out</
{{out}}
<pre>1: 1
Line 772 ⟶ 903:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COUNT_IN_FACTORS.AWK
BEGIN {
Line 799 ⟶ 930:
return(substr(f,1,length(f)-1))
}
</syntaxhighlight>
<p>output:</p>
<pre>
Line 821 ⟶ 952:
6358=2*11*17*17
</pre>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic"> 100 FOR I = 1 TO 20
110 GOSUB 200"FACTORIAL
120 PRINT I" = "FA$
130 NEXT I
140 END
200 FA$ = "1"
210 LET NUM = I
220 LET O = 5 - (I = 1) * 4
230 FOR F = 2 TO I
240 LET M = INT (NUM / F) * F
250 IF NUM - M GOTO 300
260 LET NUM = NUM / F
270 LET F$ = STR $(F)
280 FA$ = FA$ + " X " + F$
290 LET F = F - 1
300 NEXT F
310 FA$ = MID$ (FA$,O)
320 RETURN </syntaxhighlight>
==={{header|BASIC256}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="freebasic">for i = 1 to 20
print i; " = "; factorial$(i)
next i
end
function factorial$ (num)
factor$ = "" : x$ = ""
if num = 1 then return "1"
fct = 2
while fct <= num
if (num mod fct) = 0 then
factor$ += x$ + string(fct)
x$ = " x "
num /= fct
else
fct += 1
end if
end while
return factor$
end function</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Run BASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 for i = 1 to 20
120 rem for i = 1000 to 1016
130 print i;"= ";factorial$(i)
140 next i
150 end
160 function factorial$(num)
170 factor$ = "" : x$ = ""
180 if num = 1 then print "1"
190 fct = 2
200 while fct <= num
210 if (num mod fct) = 0 then
220 factor$ = factor$+x$+str$(fct)
230 x$ = " x "
240 num = num/fct
250 else
260 fct = fct+1
270 endif
280 wend
290 print factor$
300 end function</syntaxhighlight>
==={{header|True BASIC}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="qbasic">FUNCTION factorial$ (num)
LET f$ = ""
LET x$ = ""
IF num = 1 THEN LET f$ = "1"
LET fct = 2
DO WHILE fct <= num
IF MOD(num, fct) = 0 THEN
LET f$ = f$ & x$ & STR$(fct)
LET x$ = " x "
LET num = num / fct
ELSE
LET fct = fct + 1
END IF
LOOP
LET factorial$ = f$
END FUNCTION
FOR i = 1 TO 20
PRINT i; "= "; factorial$(i)
NEXT i
END</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="freebasic">for i = 1 to 20
print i, " = ", factorial$(i)
next i
end
sub factorial$ (num)
local f$, x$
f$ = "" : x$ = ""
if num = 1 return "1"
fct = 2
while fct <= num
if mod(num, fct) = 0 then
f$ = f$ + x$ + str$(fct)
x$ = " x "
num = num / fct
else
fct = fct + 1
end if
wend
return f$
end sub</syntaxhighlight>
=={{header|BBC BASIC}}==
<
PRINT i% " = " FNfactors(i%)
NEXT
Line 841 ⟶ 1,091:
ENDWHILE
= LEFT$(f$, LEN(f$) - 3)
</syntaxhighlight>
Output:
<pre> 1 = 1
Line 867 ⟶ 1,117:
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).
<
$<<<^_@#-"e":+1,+55$2<<<
v4_^#-1:/.:g00_00g1+>>0v
>8*"x",,:00g%!^!%g00:p0<</
{{out}}
Line 893 ⟶ 1,143:
=={{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.
<
#include <stdlib.h>
Line 956 ⟶ 1,206:
}
return 0;
}</
{{out}}
<pre>1 = 1
Line 977 ⟶ 1,227:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
Line 1,022 ⟶ 1,272:
}
}
}</
=={{header|C++}}==
<
#include <iomanip>
using namespace std;
Line 1,059 ⟶ 1,309:
cout << "\n\n";
return system( "pause" );
}</
{{out}}
<pre>
Line 1,092 ⟶ 1,342:
=={{header|Clojure}}==
<
(:gen-class))
Line 1,112 ⟶ 1,362:
(doseq [q (range 1 26)]
(println q " = " (clojure.string/join " x "(factors q))))
</syntaxhighlight>
{{Output}}
<pre>
Line 1,143 ⟶ 1,393:
=={{header|CoffeeScript}}==
<
# Count through the natural numbers and give their prime
# factorization. This algorithm uses no division.
Line 1,182 ⟶ 1,432:
num_primes = count_primes 10000
console.log num_primes</
=={{header|Common Lisp}}==
Auto extending prime list:
<
(make-array 10 :adjustable t :fill-pointer 0 :element-type 'integer))
Line 1,212 ⟶ 1,462:
(loop for n from 1 do
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</
{{out}}
<pre>1:
Line 1,230 ⟶ 1,480:
...</pre>
Without saving the primes, and not all that much slower (probably because above code was not well-written):
<
(loop with res for x from 2 to (isqrt n) do
(loop while (zerop (rem n x)) do
Line 1,238 ⟶ 1,488:
(loop for n from 1 do
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</
=={{header|D}}==
<
in {
assert(n > 0);
Line 1,262 ⟶ 1,512:
foreach (i; 1 .. 22)
writefln("%d: %(%d × %)", i, i.factorize());
}</
{{out}}
<pre>1: 1
Line 1,287 ⟶ 1,537:
===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 ).
<
std.array, std.string, import xt.uiprimes;
Line 1,318 ⟶ 1,568:
foreach (i; 1 .. 21)
writefln("%2d = %s", i, productStr(factorize(i)));
}</
=={{header|DCL}}==
Assumes file primes.txt is a list of prime numbers;
<
$ on control_y then $ goto clean
$
Line 1,365 ⟶ 1,615:
$
$ clean:
$ close /nolog primes</
{{out}}
<pre>$ @count_in_factors
Line 1,380 ⟶ 1,630:
=={{header|DWScript}}==
<
begin
if n <= 1 then
Line 1,397 ⟶ 1,647:
var i : Integer;
for i := 1 to 22 do
PrintLn(IntToStr(i) + ': ' + Factorize(i));</
{{out}}
<pre>1: 1
Line 1,421 ⟶ 1,671:
21: 3 * 7
22: 2 * 11</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc decompose num . primes[] .
primes[] = [ ]
t = 2
while t * t <= num
if num mod t = 0
primes[] &= t
num = num / t
else
t += 1
.
.
primes[] &= num
.
for i = 1 to 30
write i & ": "
decompose i primes[]
for j = 1 to len primes[]
if j > 1
write " x "
.
write primes[j]
.
print ""
primes[] = [ ]
.
</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 2
3: 3
4: 2 x 2
5: 5
6: 2 x 3
7: 7
8: 2 x 2 x 2
9: 3 x 3
10: 2 x 5
11: 11
12: 2 x 2 x 3
13: 13
14: 2 x 7
15: 3 x 5
16: 2 x 2 x 2 x 2
17: 17
18: 2 x 3 x 3
19: 19
20: 2 x 2 x 5
21: 3 x 7
22: 2 x 11
23: 23
24: 2 x 2 x 2 x 3
25: 5 x 5
26: 2 x 13
27: 3 x 3 x 3
28: 2 x 2 x 7
29: 29
30: 2 x 3 x 5
</pre>
=={{header|EchoLisp}}==
<
(define (task (nfrom 2) (range 20))
(for ((i (in-range nfrom (+ nfrom range))))
(writeln i "=" (string-join (prime-factors i) " x "))))
</syntaxhighlight>
{{out}}
<pre>
Line 1,456 ⟶ 1,768:
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
Line 1,523 ⟶ 1,835:
end
</syntaxhighlight>
Test Output:
Line 1,553 ⟶ 1,865:
=={{header|Elixir}}==
<
def factor(n), do: factor(n, 2, [])
Line 1,564 ⟶ 1,876:
Enum.each(1..20, fn n ->
IO.puts "#{n}: #{Enum.join(RC.factor(n)," x ")}" end)</
{{out}}
Line 1,591 ⟶ 1,903:
=={{header|Euphoria}}==
<
sequence result
integer k
Line 1,618 ⟶ 1,930:
end for
printf(1, "%d\n", factors[$])
end for</
{{out}}
<pre>1: 1
Line 1,645 ⟶ 1,957:
=={{header|F_Sharp|F#}}==
<
Seq.unfold (fun (f, n) ->
let rec genFactor (f, n) =
Line 1,655 ⟶ 1,967:
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)))</
{{out}}
<pre>1 = 1
Line 1,680 ⟶ 1,992:
=={{header|Factor}}==
<
sequences ;
Line 1,687 ⟶ 1,999:
[ " × " write ] [ pprint ] interleave nl ;
"1: 1" print 2 20 [a,b] [ .factors ] each</
{{out}}
<pre>
Line 1,713 ⟶ 2,025:
=={{header|Forth}}==
<
2
begin 2dup dup * >=
Line 1,727 ⟶ 2,039:
1+ 2 ?do i . ." : " i .factors cr loop ;
15 main bye</
=={{header|Fortran}}==
Line 1,734 ⟶ 2,046:
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">
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Thu Jun 6 23:29:06
Line 1,861 ⟶ 2,173:
call sieve(0) ! release memory
end program count_in_factors
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
Line 1,901 ⟶ 2,213:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,929 ⟶ 2,241:
=={{header|Frink}}==
Frink's factoring routines work on arbitrarily-large integers.
<
while true
{
println[join[" x ", factorFlat[i]]]
i = i + 1
}</
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn Factorial( num as long ) as CFStringRef
CFStringRef x, f, result
long fct
f = @"" : x = @""
if num = 1 then result = @" 1" : exit fn
fct = 2
while ( fct <= num )
if ( num mod fct == 0 )
f = fn StringWithFormat( @"%@%@%@", f, x, str( fct ) )
x = @" x"
num = num / fct
else
fct++
end if
wend
result = f
end fn = result
long i
for i = 1 to 20
printf @"%2ld =%@", i, fn Factorial(i)
next
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1 = 1
2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3
7 = 7
8 = 2 x 2 x 2
9 = 3 x 3
10 = 2 x 5
11 = 11
12 = 2 x 2 x 3
13 = 13
14 = 2 x 7
15 = 3 x 5
16 = 2 x 2 x 2 x 2
17 = 17
18 = 2 x 3 x 3
19 = 19
20 = 2 x 2 x 5
</pre>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Count_in_factors}}
'''Solution'''
The '''Factor''' expression reduces to a list of the primer factors of a given number.
We cannot create the multiplication directly, because it would be reduced immediately to its value. We can make use of the reflection capabilities:
[[File:Fōrmulæ - Count in factors 01.png]]
[[File:Fōrmulæ - Count in factors 02.png]]
[[File:Fōrmulæ - Count in factors 03.png]]
[[File:Fōrmulæ - Count in factors 04.png]]
[[File:Fōrmulæ - Count in factors 05.png]]
=={{header|Go}}==
<
import "fmt"
Line 1,963 ⟶ 2,340:
fmt.Println()
}
}</
{{out}}
<pre>
Line 1,980 ⟶ 2,357:
=={{header|Groovy}}==
<
if (number == 1) {
return [1]
Line 2,001 ⟶ 2,378:
((1..10) + (6351..6359)).each { number ->
println "$number = ${number.factors().join(' x ')}"
}</
{{out}}
<pre>1 = 1
Line 2,025 ⟶ 2,402:
=={{header|Haskell}}==
Using <code>factorize</code> function from the [[Prime_decomposition#Haskell|prime decomposition]] task,
<
showFactors n = show n ++ " = " ++ (intercalate " * " . map show . factorize) n
-- Pointfree form
showFactors = ((++) . show) <*> ((" = " ++) . intercalate " * " . map show . factorize)</
isPrime n = n > 1 && noDivsBy primeNums n
{{out}}
<small><
1
2 = 2
Line 2,073 ⟶ 2,450:
121231231232164 = 2 * 2 * 253811 * 119410931
121231231232165 = 5 * 137 * 176979899609
. . .</
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}}==
<
write("Press ^C to terminate")
every f := [i:= 1] | factors(i := seq(2)) do {
Line 2,085 ⟶ 2,462:
end
link factors</
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/factors.icn factors.icn provides factors]
Line 2,108 ⟶ 2,485:
=={{header|IS-BASIC}}==
<
110 FOR I=1 TO 30
120 PRINT I;"= ";FACTORS$(I)
Line 2,128 ⟶ 2,505:
280 LET FACTORS$=F$(1:LEN(F$)-1)
290 END IF
300 END DEF</
{{out}}
<pre> 1 = 1
Line 2,162 ⟶ 2,539:
=={{header|J}}==
'''Solution''':Use J's factoring primitive, <syntaxhighlight lang
'''Example''' (including formatting):<
1 : 1
2 : 2
Line 2,174 ⟶ 2,551:
9 : 3 3
10: 2 5
11: 11</
=={{header|Java}}==
{{trans|Visual Basic .NET}}
<
public static void main(String[] args){
for(int i = 1; i<= 10; i++){
Line 2,218 ⟶ 2,595:
return n;
}
}</
{{out}}
<pre>1 = 1
Line 2,242 ⟶ 2,619:
=={{header|JavaScript}}==
<
console.log(i + " : " + factor(i).join(" x "));
Line 2,256 ⟶ 2,633:
}
return factors;
}</
{{out}}
<pre>
Line 2,282 ⟶ 2,659:
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].
<
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
Line 2,293 ⟶ 2,670:
if . == 1 then "1: 1" else empty end,
(range($m;$n) | "\(.): \([factors] | join("x"))");
</syntaxhighlight>
'''Examples'''
<syntaxhighlight lang="jq">
10 | count_in_factors,
"",
count_in_factors(2144; 2145),
"",
(2|power(100) | count_in_factors(.; .+ 2))</
{{out}}
The output shown here is based on a run of gojq.
Line 2,322 ⟶ 2,699:
=={{header|Julia}}==
<
function strfactor(n::Integer)
n > -2 || return "-1 × " * strfactor(-n)
Line 2,334 ⟶ 2,711:
for n in lo:hi
@printf("%5d = %s\n", n, strfactor(n))
end</
{{out}}
Line 2,385 ⟶ 2,762:
=={{header|Kotlin}}==
<
fun isPrime(n: Int) : Boolean {
Line 2,426 ⟶ 2,803:
for (i in list)
println("${"%4d".format(i)} = ${getPrimeFactors(i).joinToString(" * ")}")
}</
{{out}}
Line 2,457 ⟶ 2,834:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
'see Run BASIC solution
for i = 1000 to 1016
Line 2,475 ⟶ 2,852:
end if
wend
end function </
{{out}}
<pre>
Line 2,498 ⟶ 2,875:
=={{header|Lua}}==
<
if n == 1 then return {1} end
Line 2,521 ⟶ 2,898:
end
print ""
end</
=={{header|M2000 Interpreter}}==
Decompose function now return array (in number decomposition task return an inventory list).
<syntaxhighlight lang="m2000 interpreter">
Module Count_in_factors {
Inventory Known1=2@, 3@
Line 2,582 ⟶ 2,959:
}
Count_in_factors
</syntaxhighlight>
=={{header|M4}}==
<
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 2,601 ⟶ 2,978:
for(`y',1,25,1, `wby(y)
')</
{{out}}
Line 2,633 ⟶ 3,010:
=={{header|Maple}}==
<
local i, j, firstNum;
if n = 1 then
Line 2,656 ⟶ 3,033:
printf("%2a: ", i);
factorNum(i);
end do;</
{{out}}
<pre>
Line 2,672 ⟶ 3,049:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
While[n < 100,
Print[Row[Riffle[Flatten[Map[Apply[ConstantArray, #] &, FactorInteger[n]]],"*"]]];
n++]</
=={{header|NetRexx}}==
{{trans|Java}}
<
options replace format comments java crossref symbols nobinary
Line 2,731 ⟶ 3,108:
end
return
</syntaxhighlight>
{{out}}
<pre style="height: 30em;overflow: scroll; font-size: smaller;">
Line 2,809 ⟶ 3,186:
=={{header|Nim}}==
{{trans|C}}
<
proc getPrime(idx: int): int =
Line 2,847 ⟶ 3,224:
if first > 0: echo n
elif n > 1: echo " x ", n
else: echo ""</
<pre>1 = 1
2 = 2
Line 2,865 ⟶ 3,242:
=={{header|Objeck}}==
<
class CountingInFactors {
function : Main(args : String[]) ~ Nil {
Line 2,919 ⟶ 3,296:
}
}
</syntaxhighlight>
Output:
<pre>
Line 2,945 ⟶ 3,322:
=={{header|OCaml}}==
<
let prime_decomposition x =
Line 2,966 ⟶ 3,343:
aux (succ_big_int v)
in
aux unit_big_int</
{{out|Execution}}
<pre>$ ocamlopt -o count.opt nums.cmxa count.ml
Line 2,992 ⟶ 3,369:
=={{header|Octave}}==
Octave's factor function returns an array:
<
printf ("%i: ", n)
printf ("%i ", factor (n))
printf ("\n")
endfor</
{{out}}
<pre>1: 1
Line 3,020 ⟶ 3,397:
=={{header|PARI/GP}}==
<
fnice(n)={
my(f,s="",s1);
if (n < 2, return(n));
Line 3,026 ⟶ 3,404:
s = Str(s, f[1,1]);
if (f[1, 2] != 1, s=Str(s, "^", f[1,2]));
for(i=2,#f[,1], s1 = Str(" * ", f[i, 1]); if (f[i, 2] != 1, s1 = Str(s1, "^", f[i, 2])); s = Str(s, s1));
s
};
n=0;while(n++<21, printf("%2s: %s\n",n,fnice(n)))
</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 2
3: 3
4: 2^2
5: 5
6: 2 * 3
7: 7
8: 2^3
9: 3^2
10: 2 * 5
11: 11
12: 2^2 * 3
13: 13
14: 2 * 7
15: 3 * 5
16: 2^4
17: 17
18: 2 * 3^2
19: 19
20: 2^2 * 5
</pre>
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<
{$IFDEF FPC}
Line 3,085 ⟶ 3,485:
writeln;
end;
end.</
{{out}}
<pre>
Line 3,111 ⟶ 3,511:
22: 2 * 11
</pre>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
// https://rosettacode.org/wiki/Count_in_factors#PascalABC.NET
function Factorize(x: integer): List<integer>;
begin
Result := new List<integer>;
if x = 1 then
begin
Result.Add(1);
exit
end;
var i := 2;
repeat
if x.Divs(i) then
begin
Result.Add(i);
x := x div i;
end
else i += 1;
until x = 1;
end;
begin
var n := 22;
(1..n).PrintLines(x -> $'{x,3}: {Factorize(x).JoinToString('' * '')}')
end.
</syntaxhighlight>
{{out}}
<pre>
1: 1
2: 2
3: 3
4: 2 * 2
5: 5
6: 2 * 3
7: 7
8: 2 * 2 * 2
9: 3 * 3
10: 2 * 5
11: 11
12: 2 * 2 * 3
13: 13
14: 2 * 7
15: 3 * 5
16: 2 * 2 * 2 * 2
17: 17
18: 2 * 3 * 3
19: 19
20: 2 * 2 * 5
21: 3 * 7
22: 2 * 11
</pre>
=={{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}}
<
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</
{{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
Line 3,130 ⟶ 3,587:
Giving similar output and also good for large inputs:
<
sub factor {
my ($pn,$pc) = @{Math::Pari::factorint(shift)};
return map { ($pn->[$_]) x $pc->[$_] } 0 .. $#$pn;
}
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</
or, somewhat slower and limited to native 32-bit or 64-bit integers only:
<
print "$_ = ", join(" x ", prime_factors($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</
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.
<
my($n, $p, @out) = (shift, 3);
return if $n < 1;
Line 3,158 ⟶ 3,615:
}
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000100;</
We could use the second extensible sieve from [[Sieve_of_Eratosthenes#Extensible_sieves]] to only divide by primes.
<
sub factors {
Line 3,176 ⟶ 3,633:
}
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000010;</
{{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
Line 3,191 ⟶ 3,648:
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.
<
use utf8;
use strict;
Line 3,222 ⟶ 3,679:
}
die "Ran out of primes?!";
}</
=={{header|Phix}}==
<!--<
<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>
Line 3,234 ⟶ 3,691:
<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>
<!--</
{{out}}
<pre>
Line 3,253 ⟶ 3,710:
=={{header|PicoLisp}}==
This is the 'factor' function from [[Prime decomposition#PicoLisp]].
<
(make
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N))
Line 3,263 ⟶ 3,720:
(for N 20
(prinl N ": " (glue " * " (factor N))) )</
{{out}}
<pre>1: 1
Line 3,287 ⟶ 3,744:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
cnt: procedure options (main);
declare (i, k, n) fixed binary;
Line 3,312 ⟶ 3,769:
end;
end cnt;
</syntaxhighlight>
Results:
<pre> 1 = 1
Line 3,357 ⟶ 3,814:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function eratosthenes ($n) {
if($n -ge 1){
Line 3,390 ⟶ 3,847:
"$(prime-decomposition 100)"
"$(prime-decomposition 12)"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 3,399 ⟶ 3,856:
=={{header|PureBasic}}==
<
Protected I = 3, Max
ClearList(Factors())
Line 3,436 ⟶ 3,893:
PrintN(text$)
Next a
EndIf</
{{out}}
<pre> 1= 1
Line 3,461 ⟶ 3,918:
=={{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.
<
primes = [2, 3, 5, 7, 11, 13, 17] # Will be extended
Line 3,495 ⟶ 3,952:
print('\nNumber of primes gathered up to', n, 'is', len(primes))
print(pfactor.cache_info())</
{{out}}
<pre> 1 1
Line 3,538 ⟶ 3,995:
Reusing the code from [http://rosettacode.org/wiki/Prime_decomposition#Quackery Prime Decomposition].
<
dup times
[ [ dup i^ 2 + /mod
Line 3,556 ⟶ 4,013:
cr again ] ] is countinfactors ( --> )
countinfactors</
{{out}}
Line 3,586 ⟶ 4,043:
=={{header|R}}==
<syntaxhighlight 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.
Line 3,613 ⟶ 4,070:
}
count_in_factors(72)
</syntaxhighlight>
{{out}}
Line 3,623 ⟶ 4,080:
See also [[#Scheme]]. This uses Racket’s <code>math/number-theory</code> package
<
(require math/number-theory)
Line 3,644 ⟶ 4,101:
(factor-count 1 22)
(factor-count 2140 2150)
; tb</
{{out}}
Line 3,685 ⟶ 4,142:
(formerly Perl 6)
{{works with|rakudo|2015-10-01}}
<syntaxhighlight lang="raku"
multi factors(1) { 1 }
Line 3,705 ⟶ 4,162:
}
say "$_: ", factors($_).join(" × ") for 1..*;</
The first twenty numbers:
<pre>1: 1
Line 3,733 ⟶ 4,190:
Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime.
<syntaxhighlight lang="raku"
$n == 1 ?? 1 !!
gather {
Line 3,745 ⟶ 4,202:
}
say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</
Same output as above.
Line 3,752 ⟶ 4,209:
Alternately, use a module:
<syntaxhighlight lang="raku"
say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</
{{out}}
<pre>1 = 1
Line 3,777 ⟶ 4,234:
100000000000000000009 = 557 x 72937 x 2461483384901
100000000000000000010 = 2 x 5 x 11 x 909090909090909091</pre>
=={{header|Refal}}==
<syntaxhighlight language="refal">$ENTRY Go {
= <Each Show <Iota 1 15> 2144>;
};
Factorize {
1 = 1;
s.N = <Factorize 2 s.N>;
s.D s.N, <Compare s.N s.D>: '-' = ;
s.D s.N, <Divmod s.N s.D>: {
(s.R) 0 = s.D <Factorize s.D s.R>;
e.X = <Factorize <+ 1 s.D> s.N>;
};
};
Join {
(e.J) = ;
(e.J) s.N = <Symb s.N>;
(e.J) s.N e.X = <Symb s.N> e.J <Join (e.J) e.X>;
};
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ s.Start 1> s.End>;
};
Each {
s.F = ;
s.F t.I e.X = <Mu s.F t.I> <Each s.F e.X>;
};
Show {
e.N = <Prout <Symb e.N> ' = ' <Join (' x ') <Factorize e.N>>>;
}; </syntaxhighlight>
{{out}}
<pre>1 = 1
2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3
7 = 7
8 = 2 x 2 x 2
9 = 3 x 3
10 = 2 x 5
11 = 11
12 = 2 x 2 x 3
13 = 13
14 = 2 x 7
15 = 3 x 5
2144 = 2 x 2 x 2 x 2 x 2 x 67</pre>
=={{header|REXX}}==
Line 3,786 ⟶ 4,295:
<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).
<
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/
parse arg LO HI @ . /*get optional arguments from the C.L. */
Line 3,822 ⟶ 4,331:
end /*j*/
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,901 ⟶ 4,410:
Note that the '''integer square root''' section of code doesn't use any floating point numbers, just integers.
<
@.=left('', 8); @.0="{unity} "; @.1='[prime] ' /*some tags and handy-dandy literals.*/
parse arg LO HI @ . /*get optional arguments from the C.L. */
Line 3,951 ⟶ 4,460:
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,999 ⟶ 4,508:
=={{header|Ring}}==
<
for i = 1 to 20
see "" + i + " = " + factors(i) + nl
Line 4,015 ⟶ 4,524:
end
return left(f, len(f) - 3)
</syntaxhighlight>
Output:
<pre>
Line 4,038 ⟶ 4,547:
19 = 19
20 = 2 x 2 x 5
</pre>
=={{header|RPL}}==
<code>PDIV</code> is defined at [[Prime decomposition#RPL|Prime decomposition]]
≪ { "1" } 2 ROT '''FOR''' j
"" j <span style="color:blue">PDIV</span> → factors
≪ '''IF''' factors SIZE 1 == '''THEN''' j →STR +
'''ELSE'''
1 factors SIZE '''FOR''' k
'''IF''' k 1 ≠ '''THEN''' 130 CHR + '''END'''
factors k GET →STR +
'''NEXT END'''
≫ + '''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
20 <span style="color:blue">TASK</span>
{{out}}
<pre>
1: { "1" "2" "3" "2×2" "5" "2×3" "7" "2×2×2" "3×3" "2×5" "11" "2×2×3" "13" "2×7" "3×5" "2×2×2×2" "17" "2×3×3" "19" "2×2×5" }
</pre>
=={{header|Ruby}}==
Starting with Ruby 1.9, 'prime' is part of the standard library and provides Integer#prime_division.
<
require 'prime'
Line 4,064 ⟶ 4,592:
end.join " x "
puts "#{i} is #{f}"
end</
{{out|Example}}
<pre>$ ruby prime-count.rb -h
Line 4,092 ⟶ 4,620:
=={{header|Run BASIC}}==
<
print i;" = "; factorial$(i)
next
Line 4,108 ⟶ 4,636:
end if
wend
end function</
{{out}}
<pre>1000 = 2 x 2 x 2 x 5 x 5 x 5
Line 4,130 ⟶ 4,658:
=={{header|Rust}}==
You can run and experiment with this code at https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b66c14d944ff0472d2460796513929e2
<
fn main() {
Line 4,170 ⟶ 4,698:
}
vec![n]
}</
{{out}}
<pre>1
Line 4,195 ⟶ 4,723:
=={{header|Sage}}==
<
if is_prime(n) or n == 1:
print(n,end="")
Line 4,210 ⟶ 4,738:
print(i,"=",end=" ")
count_in_factors(i)
print("")</
{{out}}
Line 4,274 ⟶ 4,802:
=={{header|Scala}}==
<
object CountInFactors extends App {
Line 4,302 ⟶ 4,830:
}
</syntaxhighlight>
{{out}}
<pre> 1 : 1
Line 4,320 ⟶ 4,848:
=={{header|Scheme}}==
<
(let facs ((l '()) (d 2) (x n))
(cond ((= x 1) (if (null? l) '(1) l))
Line 4,339 ⟶ 4,867:
(display i)
(display " = ")
(show (reverse (factors i))))</
{{out}}
<pre>1 = 1
Line 4,356 ⟶ 4,884:
=={{header|Seed7}}==
<
const proc: writePrimeFactors (in var integer: number) is func
Line 4,394 ⟶ 4,922:
writeln;
end for;
end func;</
{{out}}
<pre>
Line 4,416 ⟶ 4,944:
=={{header|Sidef}}==
<
method factors(n, p=2) {
var a = gather {
Line 4,444 ⟶ 4,972:
for i in (1..100) {
say "#{i} = #{Counter().factors(i).join(' × ')}"
}</
=={{header|Swift}}==
<
@inlinable
public func primeDecomposition() -> [Self] {
Line 4,476 ⟶ 5,004:
print("\(i) = \(i.primeDecomposition().map(String.init).joined(separator: " x "))")
}
}</
{{out}}
Line 4,503 ⟶ 5,031:
=={{header|Tcl}}==
This factorization code is based on the same engine that is used in the [[Parallel calculations#Tcl|parallel computation task]].
<
namespace eval prime {
Line 4,564 ⟶ 5,092:
return [join $v "*"]
}
}</
Demonstration code:
<
for {set i 1} {$i <= $max} {incr i} {
puts [format "%*d = %s" [string length $max] $i [prime::factors.rendered $i]]
}</
=={{header|VBScript}}==
Made minor modifications on the code I posted under Prime Decomposition.
<
If n = 1 Then
CountFactors = 1
Line 4,631 ⟶ 5,159:
WScript.StdOut.WriteLine
WScript.StdOut.Write "2144 = " & CountFactors(2144)
WScript.StdOut.WriteLine</
{{Out}}
Line 4,640 ⟶ 5,168:
=={{header|Visual Basic .NET}}==
<
Sub Main()
Line 4,681 ⟶ 5,209:
End Sub
End Module</
{{out}}
<pre>
Line 4,706 ⟶ 5,234:
</pre>
=={{header|
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
println("1: 1")
for i := 2; ; i++ {
for n, f := i, 2; n != 1; f++
for m := n % f; m == 0; m = n
x =
}
}</syntaxhighlight>
{{out}}
<pre>
1
2
3
4: 2×2
5
6: 2×3
7
8: 2×2×2
9: 3×3
10:
...
</pre>
=={{header|Wren}}==
{{libheader|Wren-math}}
<
for (r in [1..9, 2144..2154, 9987..9999]) {
Line 4,770 ⟶ 5,276:
}
System.print()
}</
{{out}}
Line 4,809 ⟶ 5,315:
9998: 2 x 4999
9999: 3 x 3 x 11 x 101
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
int N0, N, F;
[N0:= 1;
repeat IntOut(0, N0); Text(0, " = ");
F:= 2; N:= N0;
repeat if rem(N/F) = 0 then
[if N # N0 then Text(0, " * ");
IntOut(0, F);
N:= N/F;
]
else F:= F+1;
until F>N;
if N0=1 then IntOut(0, 1); \1 = 1
CrLf(0);
N0:= N0+1;
until KeyHit;
]</syntaxhighlight>
Example output:
<pre>
1 = 1
2 = 2
3 = 3
4 = 2 * 2
5 = 5
6 = 2 * 3
7 = 7
8 = 2 * 2 * 2
9 = 3 * 3
10 = 2 * 5
11 = 11
12 = 2 * 2 * 3
13 = 13
14 = 2 * 7
15 = 3 * 5
16 = 2 * 2 * 2 * 2
17 = 17
18 = 2 * 3 * 3
. . .
57086 = 2 * 17 * 23 * 73
57087 = 3 * 3 * 6343
57088 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 223
57089 = 57089
57090 = 2 * 3 * 5 * 11 * 173
57091 = 37 * 1543
57092 = 2 * 2 * 7 * 2039
57093 = 3 * 19031
57094 = 2 * 28547
57095 = 5 * 19 * 601
57096 = 2 * 2 * 2 * 3 * 3 * 13 * 61
57097 = 57097
</pre>
=={{header|zkl}}==
<
Using the fixed size integer (64 bit) solution from [[Prime decomposition#zkl]]
<
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum
if(n==1 or k>maxD) acc.close();
Line 4,826 ⟶ 5,386:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</
{{out}}
<pre>
Line 4,845 ⟶ 5,405:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<
20 PRINT i;" = ";
30 IF i=1 THEN PRINT 1: GO TO 90
Line 4,855 ⟶ 5,415:
90 NEXT i
100 STOP
110 DEF FN m(a,b)=a-INT (a/b)*b</
|