Count in factors: Difference between revisions
PascalABC.NET
(→{{header|PARI/GP}}: Add Output) |
(PascalABC.NET) |
||
(12 intermediate revisions by 8 users not shown) | |||
Line 292:
=={{header|ALGOL 68}}==
{{trans|Euphoria}}<syntaxhighlight lang="algol68">
OP +:= = (REF FLEX []INT a, INT b) VOID:
BEGIN
[
c[:
c[
a := c
END;
Line 327 ⟶ 328:
OD;
print ((new line))
OD
</syntaxhighlight>
{{out}}
<pre>1 = 1
Line 351 ⟶ 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}}
Line 873 ⟶ 952:
6358=2*11*17*17
</pre>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic"> 100 FOR I = 1 TO 20
Line 921 ⟶ 998:
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}}===
Line 1,572 ⟶ 1,674:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
while t *
if num mod t = 0
else
.
.
primes[] &=
.
for i = 1 to 30
decompose i primes[]
for j = 1 to len primes[]
.
.
print ""
primes[] = [ ]
.
Line 2,235 ⟶ 2,303:
=={{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}}==
Line 3,324 ⟶ 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
};
Line 3,430 ⟶ 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}}==
Line 4,096 ⟶ 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 4,357 ⟶ 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>
Line 5,055 ⟶ 5,264:
10: 2×5
...
</pre>
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="wren">import "./math" for Int
for (r in [1..9, 2144..2154, 9987..9999]) {
for (i in r) {
var factors = (i > 1) ? Int.primeFactors(i) : [1]
System.print("%(i): %(factors.join(" x "))")
}
System.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
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>
Line 5,109 ⟶ 5,369:
57096 = 2 * 2 * 2 * 3 * 3 * 13 * 61
57097 = 57097
</pre>
|