Count in factors: Difference between revisions

Add Refal
(Added Chipmunk Basic)
(Add Refal)
(9 intermediate revisions by 5 users not shown)
Line 292:
 
=={{header|ALGOL 68}}==
{{trans|Euphoria}}<syntaxhighlight lang="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 327 ⟶ 328:
OD;
print ((new line))
OD
OD</syntaxhighlight>
</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 1,595 ⟶ 1,674:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
funcproc isPrimedecompose num . result$primes[] .
ifprimes[] num= <[ 2]
result$t = "false"2
while t * breakt 1<= num
if num mod t = 0
.
if num mod 2 = 0 andprimes[] num >&= 2t
result$ num = "false"num / t
break 1
.
for i = 3 to sqrt num
if num mod i = 0
result$ = "false"
break 2
.
.
result$ = "true"
.
func decompose num . primes[] .
# If the number is prime, return the number itself
call isPrime num result$
if result$ = "true"
primes[] &= num
break 1
.
currentPrime = 2
currentNum = num
repeat
if currentNum mod currentPrime = 0
primes[] &= currentPrime
currentNum = currentNum / currentPrime
else
repeatt += 1
currentPrime += 1
call isPrime currentPrime result$
until result$ = "true"
.
.
call isPrime currentNum result$
until result$ = "true"
.
primes[] &= currentNumnum
.
# The number 30 can be changed if you want to
for i = 1 to 30
ifwrite i =& 1": "
decompose i primes[]
print "1: 1"
for j = 1 to len primes[]
else
writeif ij &> ": "1
call decompose i primes[]write " x "
for j = 1 to len primes[]
if j > 1
write " x "
.
write primes[j]
.
printwrite ""primes[j]
.
print ""
primes[] = [ ]
.
Line 2,258 ⟶ 2,303:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Count_in_factors}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
The '''Factor''' expression reduces to a list of the primer factors of a given number.
In '''[https://formulae.org/?example=Count_in_factors this]''' page you can see the program(s) related to this task and their results.
 
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 4,120 ⟶ 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 4,381 ⟶ 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>
 
Line 5,079 ⟶ 5,207:
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,133 ⟶ 5,312:
57096 = 2 * 2 * 2 * 3 * 3 * 13 * 61
57097 = 57097
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascript">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>
 
2,114

edits