Count in factors: Difference between revisions

Add Refal
(→‎{{header|Phix}}: use new builtins)
(Add Refal)
 
(35 intermediate revisions by 23 users not shown)
Line 24:
 
=={{header|11l}}==
{{trans|C++}}<langsyntaxhighlight lang="11l">F get_prime_factors(=li)
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))</langsyntaxhighlight>
 
{{out}}
Line 68:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Count in factors 24/03/2017
COUNTFAC CSECT assist plig\COUNTFAC
USING COUNTFAC,R13 base register
Line 141:
PG DS CL80 buffer
YREGS
END COUNTFAC</langsyntaxhighlight>
{{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:
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line, Ada.Text_IO, Prime_Numbers;
procedure Count is
Line 220 ⟶ 272:
exit when N > Max_N;
end loop;
end Count;</langsyntaxhighlight>
 
{{out}}
Line 240 ⟶ 292:
 
=={{header|ALGOL 68}}==
{{trans|Euphoria}}<syntaxhighlight lang ALGOL68="algol68">OP +:= = (REF FLEX []INT a, INT b) VOID:
OP +:= = (REF FLEX []INT a, INT b) VOID:
BEGIN
[⌈aUPB a + 1] INT c;
c[:⌈aUPB a] := a;
c[⌈aUPB a+1:] := b;
a := c
END;
Line 275 ⟶ 328:
OD;
print ((new line))
OD</lang>
</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 */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ NBFACT, 33
.equ MAXI, 1<<31
 
//.equ NOMBRE, 65537
//.equ NOMBRE, 99999999
.equ NOMBRE, 2144
//.equ NOMBRE, 529
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessNumber: .asciz "Number @ : "
szMessResultFact: .asciz "@ "
szCarriageReturn: .asciz "\n"
szErrorGen: .asciz "Program error !!!\n"
szMessPrime: .asciz "This number is prime.\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
tbZoneDecom: .skip 8 * NBFACT // factor 4 bytes, number of each factor 4 bytes
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r7,iNombre @ number
mov r0,r7
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
ldr r0,iAdrszMessNumber
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
bl affichageMess @ display message
mov r0,r7
ldr r1,iAdrtbZoneDecom
bl decompFact
cmp r0,#-1
beq 98f @ error ?
mov r1,r0
ldr r0,iAdrtbZoneDecom
bl displayDivisors
 
b 100f
98:
ldr r0,iAdrszErrorGen
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessResultFact: .int szMessResultFact
iAdrszErrorGen: .int szErrorGen
iAdrsZoneConv: .int sZoneConv
iAdrtbZoneDecom: .int tbZoneDecom
iAdrszMessNumber: .int szMessNumber
iNombre: .int NOMBRE
/******************************************************************/
/* display divisors function */
/******************************************************************/
/* r0 contains address of divisors area */
/* r1 contains the number of area items */
displayDivisors:
push {r2-r8,lr} @ save registers
cmp r1,#0
beq 100f
mov r2,r1
mov r3,#0 @ indice
mov r4,r0
1:
add r5,r4,r3,lsl #3
ldr r7,[r5] @ load factor
ldr r6,[r5,#4] @ load number of factor
mov r8,#0 @ display factor counter
2:
mov r0,r7
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
ldr r0,iAdrszMessResultFact
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
bl affichageMess @ display message
add r8,#1 @ increment counter
cmp r8,r6 @ same factors number ?
blt 2b
add r3,#1 @ other ithem
cmp r3,r2 @ items maxi ?
blt 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
b 100f
 
100:
pop {r2-r8,lr} @ restaur registers
bx lr @ return
/******************************************************************/
/* factor decomposition */
/******************************************************************/
/* r0 contains number */
/* r1 contains address of divisors area */
/* r0 return divisors items in table */
decompFact:
push {r1-r8,lr} @ save registers
mov r5,r1
mov r8,r0 @ save number
bl isPrime @ prime ?
cmp r0,#1
beq 98f @ yes is prime
mov r4,#0 @ raz indice
mov r1,#2 @ first divisor
mov r6,#0 @ previous divisor
mov r7,#0 @ number of same divisors
2:
mov r0,r8 @ dividende
bl division @ r1 divisor r2 quotient r3 remainder
cmp r3,#0
bne 5f @ if remainder <> zero -> no divisor
mov r8,r2 @ else quotient -> new dividende
cmp r1,r6 @ same divisor ?
beq 4f @ yes
cmp r6,#0 @ no but is the first divisor ?
beq 3f @ yes
str r6,[r5,r4,lsl #2] @ else store in the table
add r4,r4,#1 @ and increment counter
str r7,[r5,r4,lsl #2] @ store counter
add r4,r4,#1 @ next item
mov r7,#0 @ and raz counter
3:
mov r6,r1 @ new divisor
4:
add r7,r7,#1 @ increment counter
b 7f @ and loop
/* not divisor -> increment next divisor */
5:
cmp r1,#2 @ if divisor = 2 -> add 1
addeq r1,#1
addne r1,#2 @ else add 2
b 2b
/* divisor -> test if new dividende is prime */
7:
mov r3,r1 @ save divisor
cmp r8,#1 @ dividende = 1 ? -> end
beq 10f
mov r0,r8 @ new dividende is prime ?
mov r1,#0
bl isPrime @ the new dividende is prime ?
cmp r0,#1
bne 10f @ the new dividende is not prime
 
cmp r8,r6 @ else dividende is same divisor ?
beq 9f @ yes
cmp r6,#0 @ no but is the first divisor ?
beq 8f @ yes it is a first
str r6,[r5,r4,lsl #2] @ else store in table
add r4,r4,#1 @ and increment counter
str r7,[r5,r4,lsl #2] @ and store counter
add r4,r4,#1 @ next item
8:
mov r6,r8 @ new dividende -> divisor prec
mov r7,#0 @ and raz counter
9:
add r7,r7,#1 @ increment counter
b 11f
10:
mov r1,r3 @ current divisor = new divisor
cmp r1,r8 @ current divisor > new dividende ?
ble 2b @ no -> loop
/* end decomposition */
11:
str r6,[r5,r4,lsl #2] @ store last divisor
add r4,r4,#1
str r7,[r5,r4,lsl #2] @ and store last number of same divisors
add r4,r4,#1
lsr r0,r4,#1 @ return number of table items
mov r3,#0
str r3,[r5,r4,lsl #2] @ store zéro in last table item
add r4,r4,#1
str r3,[r5,r4,lsl #2] @ and zero in counter same divisor
b 100f
 
98:
ldr r0,iAdrszMessPrime
bl affichageMess
mov r0,#1 @ return code
b 100f
99:
ldr r0,iAdrszErrorGen
bl affichageMess
mov r0,#-1 @ error code
b 100f
100:
pop {r1-r8,lr} @ restaur registers
bx lr
iAdrszMessPrime: .int szMessPrime
 
/***************************************************/
/* check if a number is prime */
/***************************************************/
/* r0 contains the number */
/* r0 return 1 if prime 0 else */
@2147483647
@4294967297
@131071
isPrime:
push {r1-r6,lr} @ save registers
cmp r0,#0
beq 90f
cmp r0,#17
bhi 1f
cmp r0,#3
bls 80f @ for 1,2,3 return prime
cmp r0,#5
beq 80f @ for 5 return prime
cmp r0,#7
beq 80f @ for 7 return prime
cmp r0,#11
beq 80f @ for 11 return prime
cmp r0,#13
beq 80f @ for 13 return prime
cmp r0,#17
beq 80f @ for 17 return prime
1:
tst r0,#1 @ even ?
beq 90f @ yes -> not prime
mov r2,r0 @ save number
sub r1,r0,#1 @ exposant n - 1
mov r0,#3 @ base
bl moduloPuR32 @ compute base power n - 1 modulo n
cmp r0,#1
bne 90f @ if <> 1 -> not prime
mov r0,#5
bl moduloPuR32
cmp r0,#1
bne 90f
mov r0,#7
bl moduloPuR32
cmp r0,#1
bne 90f
mov r0,#11
bl moduloPuR32
cmp r0,#1
bne 90f
mov r0,#13
bl moduloPuR32
cmp r0,#1
bne 90f
mov r0,#17
bl moduloPuR32
cmp r0,#1
bne 90f
80:
mov r0,#1 @ is prime
b 100f
90:
mov r0,#0 @ no prime
100: @ fin standard de la fonction
pop {r1-r6,lr} @ restaur des registres
bx lr @ retour de la fonction en utilisant lr
/********************************************************/
/* Calcul modulo de b puissance e modulo m */
/* Exemple 4 puissance 13 modulo 497 = 445 */
/* */
/********************************************************/
/* r0 nombre */
/* r1 exposant */
/* r2 modulo */
/* r0 return result */
moduloPuR32:
push {r1-r7,lr} @ save registers
cmp r0,#0 @ verif <> zero
beq 100f
cmp r2,#0 @ verif <> zero
beq 100f @
1:
mov r4,r2 @ save modulo
mov r5,r1 @ save exposant
mov r6,r0 @ save base
mov r3,#1 @ start result
 
mov r1,#0 @ division de r0,r1 par r2
bl division32R
mov r6,r2 @ base <- remainder
2:
tst r5,#1 @ exposant even or odd
beq 3f
umull r0,r1,r6,r3
mov r2,r4
bl division32R
mov r3,r2 @ result <- remainder
3:
umull r0,r1,r6,r6
mov r2,r4
bl division32R
mov r6,r2 @ base <- remainder
 
lsr r5,#1 @ left shift 1 bit
cmp r5,#0 @ end ?
bne 2b
mov r0,r3
100: @ fin standard de la fonction
pop {r1-r7,lr} @ restaur des registres
bx lr @ retour de la fonction en utilisant lr
 
/***************************************************/
/* division number 64 bits in 2 registers by number 32 bits */
/***************************************************/
/* r0 contains lower part dividende */
/* r1 contains upper part dividende */
/* r2 contains divisor */
/* r0 return lower part quotient */
/* r1 return upper part quotient */
/* r2 return remainder */
division32R:
push {r3-r9,lr} @ save registers
mov r6,#0 @ init upper upper part remainder !!
mov r7,r1 @ init upper part remainder with upper part dividende
mov r8,r0 @ init lower part remainder with lower part dividende
mov r9,#0 @ upper part quotient
mov r4,#0 @ lower part quotient
mov r5,#32 @ bits number
1: @ begin loop
lsl r6,#1 @ shift upper upper part remainder
lsls r7,#1 @ shift upper part remainder
orrcs r6,#1
lsls r8,#1 @ shift lower part remainder
orrcs r7,#1
lsls r4,#1 @ shift lower part quotient
lsl r9,#1 @ shift upper part quotient
orrcs r9,#1
@ divisor sustract upper part remainder
subs r7,r2
sbcs r6,#0 @ and substract carry
bmi 2f @ négative ?
@ positive or equal
orr r4,#1 @ 1 -> right bit quotient
b 3f
2: @ negative
orr r4,#0 @ 0 -> right bit quotient
adds r7,r2 @ and restaur remainder
adc r6,#0
3:
subs r5,#1 @ decrement bit size
bgt 1b @ end ?
mov r0,r4 @ lower part quotient
mov r1,r9 @ upper part quotient
mov r2,r7 @ remainder
100: @ function end
pop {r3-r9,lr} @ restaur registers
bx lr
 
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Number 2144 : 2 2 2 2 2 67
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">loop 1..30 'x [
fs: [1]
if x<>1 -> fs: factors.prime x
print [pad to :string x 3 "=" join.with:" x " to [:string] fs]
]</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|AutoHotkey}}==
{{trans|D}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">factorize(n){
if n = 1
return 1
Line 318 ⟶ 877:
Loop 22
out .= A_Index ": " factorize(A_index) "`n"
MsgBox % out</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 344 ⟶ 903:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f COUNT_IN_FACTORS.AWK
BEGIN {
Line 371 ⟶ 930:
return(substr(f,1,length(f)-1))
}
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 393 ⟶ 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}}==
<langsyntaxhighlight lang="bbcbasic"> FOR i% = 1 TO 20
PRINT i% " = " FNfactors(i%)
NEXT
Line 413 ⟶ 1,091:
ENDWHILE
= LEFT$(f$, LEN(f$) - 3)
</syntaxhighlight>
</lang>
Output:
<pre> 1 = 1
Line 439 ⟶ 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).
 
<langsyntaxhighlight lang="befunge">1>>>>:.48*"=",,::1-#v_.v
$<<<^_@#-"e":+1,+55$2<<<
v4_^#-1:/.:g00_00g1+>>0v
>8*"x",,:00g%!^!%g00:p0<</langsyntaxhighlight>
 
{{out}}
Line 465 ⟶ 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.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 528 ⟶ 1,206:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 549 ⟶ 1,227:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 594 ⟶ 1,272:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
<lang Cpp>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
Line 606 ⟶ 1,282:
{
int f = 2; string res;
if ( li == 1 ) res = "1";
else
{
while ( true )
{
if( !( li % f ) )
{
res += to_string(f);
stringstream ss; ss << f;
res += ss.str();
li /= f; if( li == 1 ) break;
res += " x ";
Line 626 ⟶ 1,301:
int main( int argc, char* argv[] )
{
for ( int x = 1; x < 101; x++ )
{
cout << right << setw( 4 ) << x << ": ";
Line 634 ⟶ 1,309:
cout << "\n\n";
return system( "pause" );
}</syntaxhighlight>
}
</lang>
{{out}}
<pre>
Line 668 ⟶ 1,342:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns listfactors
(:gen-class))
 
Line 688 ⟶ 1,362:
(doseq [q (range 1 26)]
(println q " = " (clojure.string/join " x "(factors q))))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 719 ⟶ 1,393:
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">count_primes = (max) ->
# Count through the natural numbers and give their prime
# factorization. This algorithm uses no division.
Line 758 ⟶ 1,432:
 
num_primes = count_primes 10000
console.log num_primes</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Auto extending prime list:
<langsyntaxhighlight lang="lisp">(defparameter *primes*
(make-array 10 :adjustable t :fill-pointer 0 :element-type 'integer))
 
Line 788 ⟶ 1,462:
 
(loop for n from 1 do
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</langsyntaxhighlight>
{{out}}
<pre>1:
Line 806 ⟶ 1,480:
...</pre>
Without saving the primes, and not all that much slower (probably because above code was not well-written):
<langsyntaxhighlight lang="lisp">(defun factors (n)
(loop with res for x from 2 to (isqrt n) do
(loop while (zerop (rem n x)) do
Line 814 ⟶ 1,488:
 
(loop for n from 1 do
(format t "~a: ~{~a~^ × ~}~%" n (reverse (factors n))))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">int[] factorize(in int n) pure nothrow
in {
assert(n > 0);
Line 838 ⟶ 1,512:
foreach (i; 1 .. 22)
writefln("%d: %(%d × %)", i, i.factorize());
}</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 863 ⟶ 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 ).
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.conv, std.algorithm,
std.array, std.string, import xt.uiprimes;
 
Line 894 ⟶ 1,568:
foreach (i; 1 .. 21)
writefln("%2d = %s", i, productStr(factorize(i)));
}</langsyntaxhighlight>
 
=={{header|DCL}}==
Assumes file primes.txt is a list of prime numbers;
<langsyntaxhighlight DCLlang="dcl">$ close /nolog primes
$ on control_y then $ goto clean
$
Line 941 ⟶ 1,615:
$
$ clean:
$ close /nolog primes</langsyntaxhighlight>
{{out}}
<pre>$ @count_in_factors
Line 952 ⟶ 1,626:
...
2144 = 2*2*2*2*2*67</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Count_in_factors#Pascal Pascal].
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">function Factorize(n : Integer) : String;
begin
if n <= 1 then
Line 971 ⟶ 1,647:
var i : Integer;
for i := 1 to 22 do
PrintLn(IntToStr(i) + ': ' + Factorize(i));</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 995 ⟶ 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}}==
<langsyntaxhighlight lang="scheme">
(define (task (nfrom 2) (range 20))
(for ((i (in-range nfrom (+ nfrom range))))
(writeln i "=" (string-join (prime-factors i) " x "))))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,030 ⟶ 1,768:
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
 
class
Line 1,097 ⟶ 1,835:
end
 
</syntaxhighlight>
</lang>
Test Output:
 
Line 1,127 ⟶ 1,865:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RC do
def factor(n), do: factor(n, 2, [])
Line 1,138 ⟶ 1,876:
 
Enum.each(1..20, fn n ->
IO.puts "#{n}: #{Enum.join(RC.factor(n)," x ")}" end)</langsyntaxhighlight>
 
{{out}}
Line 1,165 ⟶ 1,903:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function factorize(integer n)
sequence result
integer k
Line 1,192 ⟶ 1,930:
end for
printf(1, "%d\n", factors[$])
end for</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 1,219 ⟶ 1,957:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let factorsOf (num) =
Seq.unfold (fun (f, n) ->
let rec genFactor (f, n) =
Line 1,229 ⟶ 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)))</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 1,254 ⟶ 1,992:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel math.primes.factors math.ranges prettyprint
sequences ;
 
Line 1,261 ⟶ 1,999:
[ " × " write ] [ pprint ] interleave nl ;
 
"1: 1" print 2 20 [a,b] [ .factors ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,287 ⟶ 2,025:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: .factors ( n -- )
2
begin 2dup dup * >=
Line 1,301 ⟶ 2,039:
1+ 2 ?do i . ." : " i .factors cr loop ;
 
15 main bye</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 1,308 ⟶ 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">
<lang FORTRAN>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Thu Jun 6 23:29:06
Line 1,435 ⟶ 2,173:
call sieve(0) ! release memory
end program count_in_factors
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
Line 1,475 ⟶ 2,213:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,503 ⟶ 2,241:
=={{header|Frink}}==
Frink's factoring routines work on arbitrarily-large integers.
<langsyntaxhighlight lang="frink">i = 1
while true
{
println[join[" x ", factorFlat[i]]]
i = i + 1
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,529 ⟶ 2,340:
fmt.Println()
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,546 ⟶ 2,357:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def factors(number) {
if (number == 1) {
return [1]
Line 1,567 ⟶ 2,378:
((1..10) + (6351..6359)).each { number ->
println "$number = ${number.factors().join(' x ')}"
}</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 1,591 ⟶ 2,402:
=={{header|Haskell}}==
Using <code>factorize</code> function from the [[Prime_decomposition#Haskell|prime decomposition]] task,
<langsyntaxhighlight lang="haskell">import Data.List (intercalate)
 
showFactors n = show n ++ " = " ++ (intercalate " * " . map show . factorize) n
-- Pointfree form
showFactors = ((++) . show) <*> ((" = " ++) . intercalate " * " . map show . factorize)</langsyntaxhighlight>
isPrime n = n > 1 && noDivsBy primeNums n
{{out}}
<small><langsyntaxhighlight lang="haskell">Main> print 1 >> mapM_ (putStrLn . showFactors) [2..]
1
2 = 2
Line 1,639 ⟶ 2,450:
121231231232164 = 2 * 2 * 253811 * 119410931
121231231232165 = 5 * 137 * 176979899609
. . .</langsyntaxhighlight></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.
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
write("Press ^C to terminate")
every f := [i:= 1] | factors(i := seq(2)) do {
Line 1,651 ⟶ 2,462:
end
 
link factors</langsyntaxhighlight>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/factors.icn factors.icn provides factors]
Line 1,674 ⟶ 2,485:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Factors.bas"
110 FOR I=1 TO 30
120 PRINT I;"= ";FACTORS$(I)
Line 1,694 ⟶ 2,505:
280 LET FACTORS$=F$(1:LEN(F$)-1)
290 END IF
300 END DEF</langsyntaxhighlight>
{{out}}
<pre> 1 = 1
Line 1,728 ⟶ 2,539:
 
=={{header|J}}==
'''Solution''':Use J's factoring primitive, <syntaxhighlight lang ="j">q:</langsyntaxhighlight>
'''Example''' (including formatting):<langsyntaxhighlight lang="j"> ('1 : 1',":&> ,"1 ': ',"1 ":@q:) 2+i.10
1 : 1
2 : 2
Line 1,740 ⟶ 2,551:
9 : 3 3
10: 2 5
11: 11</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="java">public class CountingInFactors{
public static void main(String[] args){
for(int i = 1; i<= 10; i++){
Line 1,784 ⟶ 2,595:
return n;
}
}</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 1,808 ⟶ 2,619:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">for(i = 1; i <= 10; i++)
console.log(i + " : " + factor(i).join(" x "));
 
Line 1,822 ⟶ 2,633:
}
return factors;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,836 ⟶ 2,647:
10 : 2 x 5
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The following uses `factors/0`, a suitable implementation of which may be found at
[[Prime_decomposition#jq]].
 
gojq supports unlimited-precision integer arithmetic, but the C implementation of jq
currently uses IEEE 754 64-bit numbers, so using the latter, the following program will only be
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].
<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);
 
# Input: a non-negative integer determining when to stop
def count_in_factors:
"1: 1",
(range(2;.) | "\(.): \([factors] | join("x"))");
 
def count_in_factors($m;$n):
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))</syntaxhighlight>
{{out}}
The output shown here is based on a run of gojq.
<pre>
1: 1
2: 2
3: 3
4: 2x2
5: 5
6: 2x3
7: 7
8: 2x2x2
9: 3x3
 
2144: 2x2x2x2x2x67
 
1267650600228229401496703205376: 2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2
1267650600228229401496703205377: 17x401x61681x340801x2787601x3173389601
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes, Printf
function strfactor(n::Integer)
n > -2 || return "-1 × " * strfactor(-n)
Line 1,850 ⟶ 2,711:
for n in lo:hi
@printf("%5d = %s\n", n, strfactor(n))
end</langsyntaxhighlight>
 
{{out}}
Line 1,901 ⟶ 2,762:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun isPrime(n: Int) : Boolean {
Line 1,942 ⟶ 2,803:
for (i in list)
println("${"%4d".format(i)} = ${getPrimeFactors(i).joinToString(" * ")}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,973 ⟶ 2,834:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'see Run BASIC solution
for i = 1000 to 1016
Line 1,991 ⟶ 2,852:
end if
wend
end function </langsyntaxhighlight>
{{out}}
<pre>
Line 2,014 ⟶ 2,875:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function factorize( n )
if n == 1 then return {1} end
 
Line 2,037 ⟶ 2,898:
end
print ""
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Decompose function now return array (in number decomposition task return an inventory list).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Count_in_factors {
Inventory Known1=2@, 3@
Line 2,098 ⟶ 2,959:
}
Count_in_factors
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 2,117 ⟶ 2,978:
 
for(`y',1,25,1, `wby(y)
')</langsyntaxhighlight>
 
{{out}}
Line 2,149 ⟶ 3,010:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">factorNum := proc(n)
local i, j, firstNum;
if n = 1 then
Line 2,172 ⟶ 3,033:
printf("%2a: ", i);
factorNum(i);
end do;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,188 ⟶ 3,049:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">n = 2;
While[n < 100,
Print[Row[Riffle[Flatten[Map[Apply[ConstantArray, #] &, FactorInteger[n]]],"*"]]];
n++]</langsyntaxhighlight>
 
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,247 ⟶ 3,108:
end
return
</syntaxhighlight>
</lang>
{{out}}
<pre style="height: 30em;overflow: scroll; font-size: smaller;">
Line 2,325 ⟶ 3,186:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">var primes = newSeq[int]()
 
proc getPrime(idx: int): int =
Line 2,348 ⟶ 3,209:
stdout.write x, " = "
var n = x
var first = 1true
 
for i in 0 ..< int32.high:
Line 2,354 ⟶ 3,215:
while n mod p == 0:
n = n div p
if not first == 0: stdout.write " x "
first = 0false
stdout.write p
 
Line 2,363 ⟶ 3,224:
if first > 0: echo n
elif n > 1: echo " x ", n
else: echo ""</langsyntaxhighlight>
<pre>1 = 1
2 = 2
Line 2,381 ⟶ 3,242:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class CountingInFactors {
function : Main(args : String[]) ~ Nil {
Line 2,435 ⟶ 3,296:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,461 ⟶ 3,322:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">open Big_int
 
let prime_decomposition x =
Line 2,482 ⟶ 3,343:
aux (succ_big_int v)
in
aux unit_big_int</langsyntaxhighlight>
{{out|Execution}}
<pre>$ ocamlopt -o count.opt nums.cmxa count.ml
Line 2,508 ⟶ 3,369:
=={{header|Octave}}==
Octave's factor function returns an array:
<langsyntaxhighlight lang="octave">for (n = 1:20)
printf ("%i: ", n)
printf ("%i ", factor (n))
printf ("\n")
endfor</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 2,536 ⟶ 3,397:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">fnice(n)={
fnice(n)={
my(f,s="",s1);
if (n < 2, return(n));
Line 2,542 ⟶ 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));
for(i=2,#f[,1],
s
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++, print(fnice(n)))</lang>
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}}
<langsyntaxhighlight lang="pascal">program CountInFactors(output);
 
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
 
type
Line 2,559 ⟶ 3,447:
 
function factorize(number: integer): TdynArray;
var
k: integer;
begin
if number = 1 then
begin
setlength(Result, 1);
Result[0] := 1
end
else
begin
if numberk := 1 then2;
while number > 1 do
begin
while number mod k = 0 do
setlength(factorize, 1);
factorize[0] := 1
end
else
begin
k := 2;
while number > 1 do
begin
setlength(Result, length(Result) + 1);
while number mod k = 0 do
Result[high(Result)] := k;
begin
number := number div k;
setlength(factorize, length(factorize) + 1);
factorize[high(factorize)] := k;
number := number div k;
end;
inc(k);
end;
end inc(k);
end;
end
end;
 
var
Line 2,597 ⟶ 3,485:
writeln;
end;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,626 ⟶ 3,514:
=={{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}}
<langsyntaxhighlight lang="perl">use ntheory qw/factor/;
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</langsyntaxhighlight>
{{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 2,642 ⟶ 3,530:
 
Giving similar output and also good for large inputs:
<langsyntaxhighlight lang="perl">use Math::Pari qw/factorint/;
sub factor {
my ($pn,$pc) = @{Math::Pari::factorint(shift)};
return map { ($pn->[$_]) x $pc->[$_] } 0 .. $#$pn;
}
print "$_ = ", join(" x ", factor($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</langsyntaxhighlight>
 
or, somewhat slower and limited to native 32-bit or 64-bit integers only:
<langsyntaxhighlight lang="perl">use Math::Factor::XS qw/prime_factors/;
print "$_ = ", join(" x ", prime_factors($_)), "\n" for 1000000000000000000 .. 1000000000000000010;</langsyntaxhighlight>
 
 
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.
<langsyntaxhighlight lang="perl">sub factors {
my($n, $p, @out) = (shift, 3);
return if $n < 1;
Line 2,670 ⟶ 3,558:
}
 
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000100;</langsyntaxhighlight>
 
We could use the second extensible sieve from [[Sieve_of_Eratosthenes#Extensible_sieves]] to only divide by primes.
<langsyntaxhighlight lang="perl">tie my @primes, 'Tie::SieveOfEratosthenes';
 
sub factors {
Line 2,688 ⟶ 3,576:
}
 
print "$_ = ", join(" x ", factors($_)), "\n" for 100000000000 .. 100000000010;</langsyntaxhighlight>
{{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 2,703 ⟶ 3,591:
 
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.
<langsyntaxhighlight lang="perl">#!perl -C
use utf8;
use strict;
Line 2,734 ⟶ 3,622:
}
die "Ran out of primes?!";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
Requires 0.8.2+
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Phix>procedure factorise(integer n)
<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>
sequence res = prime_factors(n,true)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
res = join(apply(res,sprint)," x ")
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #008000;">" x "</span><span style="color: #0000FF;">)</span>
printf(1,"%2d: %s\n",{n,res})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
for i=1 to 10 do
<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>
factorise(i)
<!--</syntaxhighlight>-->
end for
factorise(2144)
factorise(1000000000)</lang>
{{out}}
<pre>
Line 2,767 ⟶ 3,653:
=={{header|PicoLisp}}==
This is the 'factor' function from [[Prime decomposition#PicoLisp]].
<langsyntaxhighlight PicoLisplang="picolisp">(de factor (N)
(make
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N))
Line 2,777 ⟶ 3,663:
 
(for N 20
(prinl N ": " (glue " * " (factor N))) )</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 2,801 ⟶ 3,687:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
cnt: procedure options (main);
declare (i, k, n) fixed binary;
Line 2,826 ⟶ 3,712:
end;
end cnt;
</syntaxhighlight>
</lang>
Results:
<pre> 1 = 1
Line 2,871 ⟶ 3,757:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function eratosthenes ($n) {
if($n -ge 1){
Line 2,904 ⟶ 3,790:
"$(prime-decomposition 100)"
"$(prime-decomposition 12)"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,913 ⟶ 3,799:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure Factorize(Number, List Factors())
Protected I = 3, Max
ClearList(Factors())
Line 2,950 ⟶ 3,836:
PrintN(text$)
Next a
EndIf</langsyntaxhighlight>
{{out}}
<pre> 1= 1
Line 2,975 ⟶ 3,861:
=={{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.
<langsyntaxhighlight lang="python">from functools import lru_cache
 
primes = [2, 3, 5, 7, 11, 13, 17] # Will be extended
Line 3,009 ⟶ 3,895:
print('\nNumber of primes gathered up to', n, 'is', len(primes))
print(pfactor.cache_info())</langsyntaxhighlight>
{{out}}
<pre> 1 1
Line 3,046 ⟶ 3,932:
Number of primes gathered up to 5000 is 669
CacheInfo(hits=3935, misses=7930, maxsize=2000, currsize=2000)</pre>
 
 
=={{header|Quackery}}==
 
Reusing the code from [http://rosettacode.org/wiki/Prime_decomposition#Quackery Prime Decomposition].
 
<syntaxhighlight lang="quackery"> [ [] swap
dup times
[ [ dup i^ 2 + /mod
0 = while
nip dip
[ i^ 2 + join ]
again ]
drop
dup 1 = if conclude ]
drop ] is primefactors ( n --> [ )
[ 1 dup echo cr
[ 1+ dup primefactors
witheach
[ echo
i if [ say " x " ] ]
cr again ] ] is countinfactors ( --> )
 
countinfactors</syntaxhighlight>
 
{{out}}
 
<pre>1
2
3
2 x 2
5
2 x 3
7
2 x 2 x 2
3 x 3
2 x 5
11
2 x 2 x 3
13
2 x 7
3 x 5
2 x 2 x 2 x 2
17
2 x 3 x 3
19
2 x 2 x 5
3 x 7
2 x 11
23</pre>
… and so on. Quackery uses bignums, so "… until boredom ensues."
 
=={{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.
 
Line 3,075 ⟶ 4,013:
}
count_in_factors(72)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,085 ⟶ 4,023:
See also [[#Scheme]]. This uses Racket&rsquo;s <code>math/number-theory</code> package
 
<langsyntaxhighlight lang="racket">#lang typed/racket
 
(require math/number-theory)
Line 3,106 ⟶ 4,044:
(factor-count 1 22)
(factor-count 2140 2150)
; tb</langsyntaxhighlight>
 
{{out}}
Line 3,147 ⟶ 4,085:
(formerly Perl 6)
{{works with|rakudo|2015-10-01}}
<syntaxhighlight lang="raku" perl6line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;
 
multi factors(1) { 1 }
Line 3,167 ⟶ 4,105:
}
 
say "$_: ", factors($_).join(" × ") for 1..*;</langsyntaxhighlight>
The first twenty numbers:
<pre>1: 1
Line 3,195 ⟶ 4,133:
Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime.
 
<syntaxhighlight lang="raku" perl6line>sub factor($n is copy) {
$n == 1 ?? 1 !!
gather {
Line 3,207 ⟶ 4,145:
}
 
say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</langsyntaxhighlight>
 
Same output as above.
Line 3,214 ⟶ 4,152:
Alternately, use a module:
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 3,239 ⟶ 4,177:
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,248 ⟶ 4,238:
<br>prime factors are listed, but the number of primes found is always shown. &nbsp; The showing of the count of
<br>primes was included to help verify the factoring (of composites).
<langsyntaxhighlight 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.*/
parse arg LO HI @ . /*get optional arguments from the C.L. */
Line 3,284 ⟶ 4,274:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,363 ⟶ 4,353:
 
Note that the &nbsp; '''integer square root''' &nbsp; section of code doesn't use any floating point numbers, just integers.
<langsyntaxhighlight 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.*/
parse arg LO HI @ . /*get optional arguments from the C.L. */
Line 3,413 ⟶ 4,403:
 
if z==1 then return substr($, 1+length(@) ) /*Is residual=1? Don't add 1*/
return substr($||@||z, 1+length(@) ) /*elide superfluous header. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,461 ⟶ 4,451:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
for i = 1 to 20
see "" + i + " = " + factors(i) + nl
Line 3,477 ⟶ 4,467:
end
return left(f, len(f) - 3)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,500 ⟶ 4,490:
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.
<langsyntaxhighlight lang="ruby">require 'optparse'
require 'prime'
 
Line 3,526 ⟶ 4,535:
end.join " x "
puts "#{i} is #{f}"
end</langsyntaxhighlight>
{{out|Example}}
<pre>$ ruby prime-count.rb -h
Line 3,554 ⟶ 4,563:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">for i = 1000 to 1016
print i;" = "; factorial$(i)
next
Line 3,570 ⟶ 4,579:
end if
wend
end function</langsyntaxhighlight>
{{out}}
<pre>1000 = 2 x 2 x 2 x 5 x 5 x 5
Line 3,592 ⟶ 4,601:
=={{header|Rust}}==
You can run and experiment with this code at https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b66c14d944ff0472d2460796513929e2
<langsyntaxhighlight lang="rust">use std::env;
 
fn main() {
Line 3,632 ⟶ 4,641:
}
vec![n]
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,655 ⟶ 4,664:
20 = 2 x 2 x 5
</pre>
 
=={{header|Sage}}==
<syntaxhighlight lang="python">def count_in_factors(n):
if is_prime(n) or n == 1:
print(n,end="")
return
while n != 1:
p = next_prime(1)
while n % p != 0:
p = next_prime(p)
print(p,end="")
n = n / p
if n != 1: print(" x",end=" ")
 
for i in range(1, 101):
print(i,"=",end=" ")
count_in_factors(i)
print("")</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
31 = 31
32 = 2 x 2 x 2 x 2 x 2
33 = 3 x 11
34 = 2 x 17
35 = 5 x 7
36 = 2 x 2 x 3 x 3
37 = 37
38 = 2 x 19
39 = 3 x 13
40 = 2 x 2 x 2 x 5
41 = 41
...
85 = 5 x 17
86 = 2 x 43
87 = 3 x 29
88 = 2 x 2 x 2 x 11
89 = 89
90 = 2 x 3 x 3 x 5
91 = 7 x 13
92 = 2 x 2 x 23
93 = 3 x 31
94 = 2 x 47
95 = 5 x 19
96 = 2 x 2 x 2 x 2 x 2 x 3
97 = 97
98 = 2 x 7 x 7
99 = 3 x 3 x 11
100 = 2 x 2 x 5 x 5</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
object CountInFactors extends App {
 
Line 3,685 ⟶ 4,773:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 : 1
Line 3,703 ⟶ 4,791:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="lisp">(define (factors n)
(let facs ((l '()) (d 2) (x n))
(cond ((= x 1) (if (null? l) '(1) l))
Line 3,722 ⟶ 4,810:
(display i)
(display " = ")
(show (reverse (factors i))))</langsyntaxhighlight>
{{out}}
<pre>1 = 1
Line 3,739 ⟶ 4,827:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: writePrimeFactors (in var integer: number) is func
Line 3,777 ⟶ 4,865:
writeln;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 3,799 ⟶ 4,887:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">class Counter {
method factors(n, p=2) {
var a = gather {
Line 3,827 ⟶ 4,915:
for i in (1..100) {
say "#{i} = #{Counter().factors(i).join(' × ')}"
}</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
public func primeDecomposition() -> [Self] {
Line 3,859 ⟶ 4,947:
print("\(i) = \(i.primeDecomposition().map(String.init).joined(separator: " x "))")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,886 ⟶ 4,974:
=={{header|Tcl}}==
This factorization code is based on the same engine that is used in the [[Parallel calculations#Tcl|parallel computation task]].
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
namespace eval prime {
Line 3,947 ⟶ 5,035:
return [join $v "*"]
}
}</langsyntaxhighlight>
Demonstration code:
<langsyntaxhighlight lang="tcl">set max 20
for {set i 1} {$i <= $max} {incr i} {
puts [format "%*d = %s" [string length $max] $i [prime::factors.rendered $i]]
}</langsyntaxhighlight>
 
=={{header|VBScript}}==
Made minor modifications on the code I posted under Prime Decomposition.
<langsyntaxhighlight lang="vb">Function CountFactors(n)
If n = 1 Then
CountFactors = 1
Line 4,014 ⟶ 5,102:
WScript.StdOut.WriteLine
WScript.StdOut.Write "2144 = " & CountFactors(2144)
WScript.StdOut.WriteLine</langsyntaxhighlight>
 
{{Out}}
Line 4,023 ⟶ 5,111:
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet">Module CountingInFactors
 
Sub Main()
Line 4,064 ⟶ 5,152:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 4,089 ⟶ 5,177:
</pre>
 
=={{header|XPL0V (Vlang)}}==
{{trans|go}}
<lang XPL0>include c:\cxpl\codes;
<syntaxhighlight lang="v (vlang)">fn main() {
int N0, N, F;
println("1: 1")
[N0:= 1;
for i := 2; ; i++ {
repeat IntOut(0, N0); Text(0, " = ");
Fprint("$i:= 2; N:= N0;")
repeatmut x if rem(N/F) := 0 then''
for n, f := i, 2; n != 1; f++ [if N # N0 then Text(0, " * ");{
for m := n % f; m == 0; m = n IntOut(0,% F);f {
N:= N/F;print('$x$f')
x = ]"×"
elsen F:/= F+1;f
until F>N; }
}
if N0=1 then IntOut(0, 1); \1 = 1
CrLfprintln(0'');
N0:= N0+1;}
}</syntaxhighlight>
until KeyHit;
{{out}}
]</lang>
 
Example output:
<pre>
1 =: 1
2 =: 2
3 =: 3
4: 2×2
4 = 2 * 2
5 =: 5
6: 2×3
6 = 2 * 3
7 =: 7
8: 2×2×2
8 = 2 * 2 * 2
9: 3×3
9 = 3 * 3
10: = 2 * 52×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|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
for (r in [1..9, 2144..2154, 9987..9999]) {
Line 4,153 ⟶ 5,219:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 4,192 ⟶ 5,258:
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}}==
<langsyntaxhighlight lang="zkl">foreach n in ([1..*]){ println(n,": ",primeFactors(n).concat("\U2715;")) }</langsyntaxhighlight>
Using the fixed size integer (64 bit) solution from [[Prime decomposition#zkl]]
<langsyntaxhighlight 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
if(n==1 or k>maxD) acc.close();
Line 4,209 ⟶ 5,329:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,228 ⟶ 5,348:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<langsyntaxhighlight lang="zxbasic">10 FOR i=1 TO 20
20 PRINT i;" = ";
30 IF i=1 THEN PRINT 1: GO TO 90
Line 4,238 ⟶ 5,358:
90 NEXT i
100 STOP
110 DEF FN m(a,b)=a-INT (a/b)*b</langsyntaxhighlight>
2,093

edits