Count in factors: Difference between revisions

Content added Content deleted
(→‎{{header|Scala}}: migrate to Scala 2.13)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 185: Line 185:
40=2*2*2*5
40=2*2*2*5
</pre>
</pre>



=={{header|Ada}}==
=={{header|Ada}}==
Line 300: Line 299:
21 = 3 × 7
21 = 3 × 7
22 = 2 × 11</pre>
22 = 2 × 11</pre>

=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{trans|D}}
{{trans|D}}
Line 393: Line 393:
6358=2*11*17*17
6358=2*11*17*17
</pre>
</pre>

=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> FOR i% = 1 TO 20
<lang bbcbasic> FOR i% = 1 TO 20
Line 546: Line 547:
.
.
.</pre>
.</pre>

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;

namespace prog
{
class MainClass
{
public static void Main (string[] args)
{
for( int i=1; i<=22; i++ )
{
List<int> f = Factorize(i);
Console.Write( i + ": " + f[0] );
for( int j=1; j<f.Count; j++ )
{
Console.Write( " * " + f[j] );
}
Console.WriteLine();
}
}
public static List<int> Factorize( int n )
{
List<int> l = new List<int>();
if ( n == 1 )
{
l.Add(1);
}
else
{
int k = 2;
while( n > 1 )
{
while( n % k == 0 )
{
l.Add( k );
n /= k;
}
k++;
}
}
return l;
}
}
}</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 617: Line 666:
.
.
</pre>
</pre>

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;

namespace prog
{
class MainClass
{
public static void Main (string[] args)
{
for( int i=1; i<=22; i++ )
{
List<int> f = Factorize(i);
Console.Write( i + ": " + f[0] );
for( int j=1; j<f.Count; j++ )
{
Console.Write( " * " + f[j] );
}
Console.WriteLine();
}
}
public static List<int> Factorize( int n )
{
List<int> l = new List<int>();
if ( n == 1 )
{
l.Add(1);
}
else
{
int k = 2;
while( n > 1 )
{
while( n % k == 0 )
{
l.Add( k );
n /= k;
}
k++;
}
}
return l;
}
}
}</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 894: Line 895:
writefln("%2d = %s", i, productStr(factorize(i)));
writefln("%2d = %s", i, productStr(factorize(i)));
}</lang>
}</lang>

=={{header|DCL}}==
=={{header|DCL}}==
Assumes file primes.txt is a list of prime numbers;
Assumes file primes.txt is a list of prime numbers;
Line 1,300: Line 1,302:


15 main bye</lang>
15 main bye</lang>



=={{header|Fortran}}==
=={{header|Fortran}}==
Line 2,734: Line 2,735:
die "Ran out of primes?!";
die "Ran out of primes?!";
}</lang>
}</lang>

=={{header|Perl 6}}==
{{works with|rakudo|2015-10-01}}
<lang perl6>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;

multi factors(1) { 1 }
multi factors(Int $remainder is copy) {
gather for @primes -> $factor {

# if remainder < factor², we're done
if $factor * $factor > $remainder {
take $remainder if $remainder > 1;
last;
}

# How many times can we divide by this prime?
while $remainder %% $factor {
take $factor;
last if ($remainder div= $factor) === 1;
}
}
}

say "$_: ", factors($_).join(" × ") for 1..*;</lang>
The first twenty numbers:
<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</pre>
Here we use a <tt>multi</tt> declaration with a constant parameter to match the degenerate case. We use <tt>copy</tt> parameters when we wish to reuse the formal parameter as a mutable variable within the function. (Parameters default to readonly in Perl&nbsp;6.) Note the use of <tt>gather</tt>/<tt>take</tt> as the final statement in the function, which is a common Perl&nbsp;6 idiom to set up a coroutine within a function to return a lazy list on demand.

Note also the '×' above is not ASCII 'x', but U+00D7 MULTIPLICATION SIGN. Perl&nbsp;6 does Unicode natively.

Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime.

<lang perl6>sub factor($n is copy) {
$n == 1 ?? 1 !!
gather {
$n /= take 2 while $n %% 2;
$n /= take 3 while $n %% 3;
loop (my $p = 5; $p*$p <= $n; $p+=2) {
$n /= take $p while $n %% $p;
}
take $n unless $n == 1;
}
}

say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</lang>

Same output as above.


Alternately, use a module:

<lang perl6>use Prime::Factor;

say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</lang>
{{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
100000000000000000000 = 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 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 x 5 x 5
100000000000000000001 = 73 x 137 x 1676321 x 5964848081
100000000000000000002 = 2 x 3 x 155977777 x 106852828571
100000000000000000003 = 373 x 155773 x 1721071782307
100000000000000000004 = 2 x 2 x 13 x 1597 x 240841 x 4999900001
100000000000000000005 = 3 x 5 x 7 x 7 x 83 x 1663 x 985694468327
100000000000000000006 = 2 x 31 x 6079 x 265323774602147
100000000000000000007 = 67 x 166909 x 8942221889969
100000000000000000008 = 2 x 2 x 2 x 3 x 3 x 3 x 233 x 1986965506278811
100000000000000000009 = 557 x 72937 x 2461483384901
100000000000000000010 = 2 x 5 x 11 x 909090909090909091</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 3,247: Line 3,153:
2149: 7 × 307
2149: 7 × 307
2150: 2 × 5 × 5 × 43</pre>
2150: 2 × 5 × 5 × 43</pre>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-10-01}}
<lang perl6>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime;

multi factors(1) { 1 }
multi factors(Int $remainder is copy) {
gather for @primes -> $factor {

# if remainder < factor², we're done
if $factor * $factor > $remainder {
take $remainder if $remainder > 1;
last;
}

# How many times can we divide by this prime?
while $remainder %% $factor {
take $factor;
last if ($remainder div= $factor) === 1;
}
}
}

say "$_: ", factors($_).join(" × ") for 1..*;</lang>
The first twenty numbers:
<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</pre>
Here we use a <tt>multi</tt> declaration with a constant parameter to match the degenerate case. We use <tt>copy</tt> parameters when we wish to reuse the formal parameter as a mutable variable within the function. (Parameters default to readonly in Perl&nbsp;6.) Note the use of <tt>gather</tt>/<tt>take</tt> as the final statement in the function, which is a common Perl&nbsp;6 idiom to set up a coroutine within a function to return a lazy list on demand.

Note also the '×' above is not ASCII 'x', but U+00D7 MULTIPLICATION SIGN. Perl&nbsp;6 does Unicode natively.

Here is a solution inspired from [[Almost_prime#C]]. It doesn't use &is-prime.

<lang perl6>sub factor($n is copy) {
$n == 1 ?? 1 !!
gather {
$n /= take 2 while $n %% 2;
$n /= take 3 while $n %% 3;
loop (my $p = 5; $p*$p <= $n; $p+=2) {
$n /= take $p while $n %% $p;
}
take $n unless $n == 1;
}
}

say "$_ == ", join " \x00d7 ", factor $_ for 1 .. 20;</lang>

Same output as above.


Alternately, use a module:

<lang perl6>use Prime::Factor;

say "$_ = {(.&prime-factors || 1).join: ' x ' }" for flat 1 .. 10, 10**20 .. 10**20 + 10;</lang>
{{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
100000000000000000000 = 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 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 x 5 x 5
100000000000000000001 = 73 x 137 x 1676321 x 5964848081
100000000000000000002 = 2 x 3 x 155977777 x 106852828571
100000000000000000003 = 373 x 155773 x 1721071782307
100000000000000000004 = 2 x 2 x 13 x 1597 x 240841 x 4999900001
100000000000000000005 = 3 x 5 x 7 x 7 x 83 x 1663 x 985694468327
100000000000000000006 = 2 x 31 x 6079 x 265323774602147
100000000000000000007 = 67 x 166909 x 8942221889969
100000000000000000008 = 2 x 2 x 2 x 3 x 3 x 3 x 233 x 1986965506278811
100000000000000000009 = 557 x 72937 x 2461483384901
100000000000000000010 = 2 x 5 x 11 x 909090909090909091</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 4,029: Line 4,031:
2144 = 2 * 2 * 2 * 2 * 2 * 67
2144 = 2 * 2 * 2 * 2 * 2 * 67
</pre>
</pre>



=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==