Count in factors: Difference between revisions

m (→‎{{header|Perl 6}}: fix illegalish syntax (though rakudo won't complain))
Line 3:
For examle, <math>2</math> is prime, so it would be shown as itself. <math>6</math> is not prime; it would be shown as <math>2\times3</math>. Likewise, 2144 is not prime; it would be shown as <math>2\times2\times2\times2\times2\times67</math>.
 
c.f. [[Prime decomposition]]
=={{header|D}}==
<lang d>import std.stdio, std.math, std.conv, std.algorithm, std.array, std.string ;
import xt.uiprimes ;
 
pragma(lib, "uiprimes.lib") ;
 
// function _factorize_ included in uiprimes.lib
ulong[] factorize(ulong n) {
if(n == 0) return [] ;
if(n == 1) return [1] ;
ulong[] res ;
uint limit = cast(uint) (1 + sqrt(n)) ;
foreach(p; Primes(limit)) {
if(n == 1) break ;
if(0UL == (n % p ))
while((n > 1) && (0UL == (n % p ))) {
res ~= p ;
n = n / p ;
}
}
if(n > 1)
res ~= [n] ;
return res ;
}
 
string productStr(T)(T[] nums) {
return array(map!q{to!string(a)}(nums)).join(" x ") ;
}
 
void main() {
foreach(i;1..21)
writefln("%2d = %s", i, productStr(factorize(i))) ;
}</lang>
{{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 ).
=={{header|J}}==
 
Anonymous user