Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,6 +1,13 @@
|
|||
The prime decomposition of a number is defined as a list of prime numbers which when all multiplied together, are equal to that number. Example: 12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
|
||||
{{omit from|GUISS}}
|
||||
The prime decomposition of a number is defined as a list of prime numbers
|
||||
which when all multiplied together, are equal to that number.
|
||||
|
||||
Write a function which returns an [[array]] or [[Collections|collection]] which contains the prime decomposition of a given number, n, greater than 1. If your language does not have an isPrime-like function available, you may assume that you have a function which determines whether a number is prime (note its name before your code).
|
||||
Example: 12 = 2 × 2 × 3, so its prime decomposition is {2, 2, 3}
|
||||
|
||||
Write a function which returns an [[array]] or [[Collections|collection]] which contains the prime decomposition of a given number, n, greater than 1.
|
||||
If your language does not have an isPrime-like function available,
|
||||
you may assume that you have a function which determines
|
||||
whether a number is prime (note its name before your code).
|
||||
|
||||
If you would like to test code from this task, you may use code from [[Primality by Trial Division|trial division]] or the [[Sieve of Eratosthenes]].
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
# Usage: awk -f primefac.awk
|
||||
function pfac(n, r, f){
|
||||
r = ""; f = 2
|
||||
while (f <= n) {
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ generic
|
|||
One : Number;
|
||||
Two : Number;
|
||||
with function "+" (X, Y : Number) return Number is <>;
|
||||
with function "*" (X, Y : Number) return Number is <>;
|
||||
with function "/" (X, Y : Number) return Number is <>;
|
||||
with function "mod" (X, Y : Number) return Number is <>;
|
||||
with function ">=" (X, Y : Number) return Boolean is <>;
|
||||
with function ">" (X, Y : Number) return Boolean is <>;
|
||||
package Prime_Numbers is
|
||||
type Number_List is array (Positive range <>) of Number;
|
||||
function Decompose (N : Number) return Number_List;
|
||||
function Is_Prime (N : Number) return Boolean;
|
||||
end Prime_Numbers;
|
||||
|
|
|
|||
|
|
@ -1,30 +1,31 @@
|
|||
package body Prime_Numbers is
|
||||
function Decompose (N : Number) return Number_List is
|
||||
Size : Natural := 0;
|
||||
M : Number := N;
|
||||
K : Number := Two;
|
||||
-- auxiliary (internal) functions
|
||||
function First_Factor (N : Number; Start : Number) return Number is
|
||||
K : Number := Start;
|
||||
begin
|
||||
-- Estimation of the result length from above
|
||||
while M >= Two loop
|
||||
M := (M + One) / Two;
|
||||
Size := Size + 1;
|
||||
while ((N mod K) /= Zero) and then (N > (K*K)) loop
|
||||
K := K + One;
|
||||
end loop;
|
||||
M := N;
|
||||
-- Filling the result with prime numbers
|
||||
declare
|
||||
Result : Number_List (1..Size);
|
||||
Index : Positive := 1;
|
||||
begin
|
||||
while N >= K loop -- Divisors loop
|
||||
while Zero = (M mod K) loop -- While divides
|
||||
Result (Index) := K;
|
||||
Index := Index + 1;
|
||||
M := M / K;
|
||||
end loop;
|
||||
K := K + One;
|
||||
end loop;
|
||||
return Result (1..Index - 1);
|
||||
end;
|
||||
if (N mod K) = Zero then
|
||||
return K;
|
||||
else
|
||||
return N;
|
||||
end if;
|
||||
end First_Factor;
|
||||
|
||||
function Decompose (N : Number; Start : Number) return Number_List is
|
||||
F: Number := First_Factor(N, Start);
|
||||
M: Number := N / F;
|
||||
begin
|
||||
if M = One then -- F is the last factor
|
||||
return (1 => F);
|
||||
else
|
||||
return F & Decompose(M, Start);
|
||||
end if;
|
||||
end Decompose;
|
||||
|
||||
-- functions visible from the outside
|
||||
function Decompose (N : Number) return Number_List is (Decompose(N, Two));
|
||||
function Is_Prime (N : Number) return Boolean is
|
||||
(N > One and then First_Factor(N, Two)=N);
|
||||
end Prime_Numbers;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.bigint, std.algorithm, std.traits, std.range;
|
||||
|
||||
Unqual!T[] decompose(T)(in T number) pure /*nothrow*/
|
||||
Unqual!T[] decompose(T)(in T number) pure nothrow
|
||||
in {
|
||||
assert(number > 1);
|
||||
} body {
|
||||
|
|
|
|||
37
Task/Prime-decomposition/Eiffel/prime-decomposition.e
Normal file
37
Task/Prime-decomposition/Eiffel/prime-decomposition.e
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
class
|
||||
PRIME_DECOMPOSITION
|
||||
feature
|
||||
factor(p: INTEGER): ARRAY[INTEGER]
|
||||
require
|
||||
p_positive: p>0
|
||||
local
|
||||
div,i: INTEGER
|
||||
next:INTEGER
|
||||
rest: INTEGER
|
||||
d: ARRAY[INTEGER]
|
||||
do
|
||||
create d.make_empty
|
||||
if p= 1 then
|
||||
d.force (1, 1)
|
||||
Result:= d
|
||||
end
|
||||
div:= 2
|
||||
next:= 3
|
||||
rest:=p
|
||||
from
|
||||
i:=1
|
||||
until rest=1
|
||||
loop
|
||||
from
|
||||
until rest\\div/=0
|
||||
loop
|
||||
d.force( div, i)
|
||||
rest := (rest/div).floor
|
||||
i:= i+1
|
||||
end
|
||||
div := next
|
||||
next:= next+2
|
||||
end
|
||||
Result:= d
|
||||
end
|
||||
end
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
factorize_ n | n > 1 = concat [divs n p | p <- [2..n], isPrime p]
|
||||
factorize n = concat [divs n p | p <- [2..n], isPrime p]
|
||||
where
|
||||
divs n p = if rem n p==0 then p:divs (quot n p) p else []
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
factorize n | n > 1 = go n primesList
|
||||
factorize n = divs n primesList
|
||||
where
|
||||
go n ds@(d:t)
|
||||
| d*d > n = [n]
|
||||
| r == 0 = d : go q ds
|
||||
| otherwise = go n t
|
||||
where
|
||||
(q,r) = quotRem n d
|
||||
divs n ds@(d:t) | d*d > n = [n | n > 1]
|
||||
| r == 0 = d : divs q ds
|
||||
| otherwise = divs n t
|
||||
where (q,r) = quotRem n d
|
||||
|
|
|
|||
|
|
@ -1,9 +1,2 @@
|
|||
q: 3684
|
||||
q: 3684
|
||||
2 2 3 307
|
||||
|
||||
_1+2^128x
|
||||
340282366920938463463374607431768211455
|
||||
q: _1+2^128x
|
||||
3 5 17 257 641 65537 274177 6700417 67280421310721
|
||||
*/ q: _1+2^128x
|
||||
340282366920938463463374607431768211455
|
||||
|
|
|
|||
6
Task/Prime-decomposition/J/prime-decomposition-3.j
Normal file
6
Task/Prime-decomposition/J/prime-decomposition-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
_1+2^128x
|
||||
340282366920938463463374607431768211455
|
||||
q: _1+2^128x
|
||||
3 5 17 257 641 65537 274177 6700417 67280421310721
|
||||
*/ q: _1+2^128x
|
||||
340282366920938463463374607431768211455
|
||||
|
|
@ -2,16 +2,13 @@ test: procedure options (main, reorder);
|
|||
declare (n, i) fixed binary (31);
|
||||
|
||||
get list (n);
|
||||
|
||||
put edit ( n, '[' ) (x(1), a);
|
||||
|
||||
restart:
|
||||
if is_prime(n) then
|
||||
do;
|
||||
put edit (trim(n), ']' ) (x(1), a);
|
||||
stop;
|
||||
end;
|
||||
|
||||
do i = n/2 to 2 by -1;
|
||||
if is_prime(i) then
|
||||
if (mod(n, i) = 0) then
|
||||
|
|
@ -23,7 +20,6 @@ restart:
|
|||
end;
|
||||
put edit ( ' ]' ) (a);
|
||||
|
||||
|
||||
is_prime: procedure (n) options (reorder) returns (bit(1));
|
||||
declare n fixed binary (31);
|
||||
declare i fixed binary (31);
|
||||
|
|
|
|||
14
Task/Prime-decomposition/Perl/prime-decomposition-2.pl
Normal file
14
Task/Prime-decomposition/Perl/prime-decomposition-2.pl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
sub prime_factors {
|
||||
my($n, $p, @out) = (shift, 3);
|
||||
return if $n < 1;
|
||||
while (!($n&1)) { $n >>= 1; push @out, 2; }
|
||||
while ($n > 1 && $p*$p <= $n) {
|
||||
while ( ($n % $p) == 0) {
|
||||
$n /= $p;
|
||||
push @out, $p;
|
||||
}
|
||||
$p += 2;
|
||||
}
|
||||
push @out, $n if $n > 1;
|
||||
@out;
|
||||
}
|
||||
7
Task/Prime-decomposition/Perl/prime-decomposition-3.pl
Normal file
7
Task/Prime-decomposition/Perl/prime-decomposition-3.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use ntheory qw/factor forprimes/;
|
||||
use bigint;
|
||||
|
||||
forprimes {
|
||||
my $p = 2 ** $_ - 1;
|
||||
print "2**$_-1: ", join(" ", factor($p)), "\n";
|
||||
} 100, 150;
|
||||
13
Task/Prime-decomposition/Perl/prime-decomposition-4.pl
Normal file
13
Task/Prime-decomposition/Perl/prime-decomposition-4.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use Math::Pari qw/:int factorint isprime/;
|
||||
|
||||
# Convert Math::Pari's format into simple vector
|
||||
sub factor {
|
||||
my ($pn,$pc) = @{Math::Pari::factorint(shift)};
|
||||
map { ($pn->[$_]) x $pc->[$_] } 0 .. $#$pn;
|
||||
}
|
||||
|
||||
for (100 .. 150) {
|
||||
next unless isprime($_);
|
||||
my $p = 2 ** $_ - 1;
|
||||
print "2^$_-1: ", join(" ", factor($p)), "\n";
|
||||
}
|
||||
|
|
@ -5,19 +5,14 @@ except NameError:
|
|||
long = int
|
||||
|
||||
def fac(n):
|
||||
step = lambda x: 1 + x*4 - (x//2)*2
|
||||
step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
|
||||
maxq = long(floor(sqrt(n)))
|
||||
d = 1
|
||||
q = n % 2 == 0 and 2 or 3
|
||||
while q <= maxq and n % q != 0:
|
||||
q = step(d)
|
||||
d += 1
|
||||
res = []
|
||||
if q <= maxq:
|
||||
res.extend(fac(n//q))
|
||||
res.extend(fac(q))
|
||||
else: res=[n]
|
||||
return res
|
||||
return q <= maxq and [q] + fac(n//q) or [n]
|
||||
|
||||
if __name__ == '__main__':
|
||||
import time
|
||||
|
|
|
|||
|
|
@ -31,10 +31,10 @@ object PrimeFactors extends App {
|
|||
|
||||
val datum = System.nanoTime
|
||||
val result = factorize(nMersenne)
|
||||
val mSec = ((System.nanoTime - datum) / 1.e+6).round
|
||||
val mSec = ((System.nanoTime - datum) / 1.0e+6).round
|
||||
|
||||
def decStr = { if (lit.length > 30) f"(M has ${lit.length}%3d dec)" else "" }
|
||||
def sPrime = { if (result.isEmpty) " is a Mersenne prime number." else "" }
|
||||
def sPrime = { if (result.isEmpty) " is a prime number." else "" }
|
||||
|
||||
println(
|
||||
f"$numM%4s = 2^$p%03d - 1 = ${lit}%s${sPrime} ($mSec%,4d msec) composed of ${result.mkString(" × ")}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue