Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,53 @@
begin
integer procedure mod(a, b);
value a, b; integer a, b;
begin
mod := a - entier(a/b) * b;
end;
comment
Decompose n into its prime factors and store
in the array pf, returning the number found.
If n is prime, it will be stored as the first
and only factor;
integer procedure primefactors(n, pf);
value n; integer n; integer array pf;
begin
integer i, count;
count := 1;
i := 2;
for i := i while (i * i) <= n do
begin
if mod(n, i) = 0 then
begin
pf[count] := i;
count := count + 1;
n := n / i;
end
else
i := i + 1;
end;
pf[count] := n;
primefactors := count;
end;
comment
exercise the procedure by displaying the prime
factors of the odd numbers from 77 to 99;
integer i, k, nfound;
integer array factors[1:32];
for i := 77 step 2 until 99 do
begin
nfound := primefactors(i, factors);
outinteger(1,i);
outstring(1,": ");
for k := 1 step 1 until nfound do
outinteger(1,factors[k]);
outstring(1,"\n");
end;
end

View file

@ -0,0 +1,15 @@
generic
type Number is private;
Zero : Number;
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 <>;
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;

View file

@ -0,0 +1,31 @@
package body Prime_Numbers is
-- auxiliary (internal) functions
function First_Factor (N : Number; Start : Number) return Number is
K : Number := Start;
begin
while ((N mod K) /= Zero) and then (N > (K*K)) loop
K := K + One;
end loop;
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;

View file

@ -0,0 +1,18 @@
with Prime_Numbers, Ada.Text_IO;
procedure Test_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
procedure Put (List : Number_List) is
begin
for Index in List'Range loop
Ada.Text_IO.Put (Positive'Image (List (Index)));
end loop;
end Put;
begin
Put (Decompose (12));
end Test_Prime;

View file

@ -0,0 +1,68 @@
with Ada.Text_IO; use Ada.Text_IO;
with Unbounded_Unsigneds; use Unbounded_Unsigneds;
with Unbounded_Unsigneds.Primes; use Unbounded_Unsigneds.Primes;
with Strings_Edit.Unbounded_Unsigned_Edit;
use Strings_Edit.Unbounded_Unsigned_Edit;
with Generic_Unbounded_Array;
procedure Prime_Decomposition is
type Unbounded_Unsigned_Array is
array (Positive range <>) of Unbounded_Unsigned;
function Decompose (X : Unbounded_Unsigned)
return Unbounded_Unsigned_Array is
package Unbounded_Arrays is
new Generic_Unbounded_Array
( Positive,
Unbounded_Unsigned,
Unbounded_Unsigned_Array,
Zero
);
Result : Unbounded_Arrays.Unbounded_Array;
Count : Natural := 0;
Factor : Unbounded_Unsigned := Two;
Value : Unbounded_Unsigned := X;
Limit : Unbounded_Unsigned := Sqrt (X);
begin
loop
if Is_Zero (Value mod Factor) then
loop
Count := Count + 1;
Result.Put (Count, Factor);
Div (Value, Factor);
exit when not Is_Zero (Value mod Factor);
end loop;
if Is_Prime (Value, 10) = Prime then
Count := Count + 1;
Result.Put (Count, Value);
exit;
end if;
end if;
Next_Prime (Factor, 10);
exit when Factor > Limit;
end loop;
if Count = 0 then
return (1..0 => Zero);
else
return Result.Vector (1..Count);
end if;
end Decompose;
procedure Print (A : Unbounded_Unsigned_Array) is
begin
for I in A'Range loop
if I > A'First then
Put (", ");
end if;
Put (Image (A (I)));
end loop;
New_Line;
end Print;
begin
Print (Decompose (Two * 2 * 3));
Print (Decompose (Two * 3 * 5 * 7 * 11 * 11 * 13 * 17));
Print (Decompose (From_Half_Word (233) * 1103 * 2089));
Print (Decompose (From_Half_Word (431) * 9719 * 2099863));
Print (Decompose (From_Half_Word (179951) * 16860167264933));
end Prime_Decomposition;

View file

@ -0,0 +1,38 @@
Program PrimeDecomposition(output);
type
DynArray = array of integer;
procedure findFactors(n: Int64; var d: DynArray);
var
divisor, next, rest: Int64;
i: integer;
begin
i := 0;
divisor := 2;
next := 3;
rest := n;
while (rest <> 1) do
begin
while (rest mod divisor = 0) do
begin
setlength(d, i+1);
d[i] := divisor;
inc(i);
rest := rest div divisor;
end;
divisor := next;
next := next + 2;
end;
end;
var
factors: DynArray;
j: integer;
begin
setlength(factors, 1);
findFactors(1023*1024, factors);
for j := low(factors) to high(factors) do
writeln (factors[j]);
end.

View file

@ -0,0 +1,46 @@
Program PrimeDecomposition(output);
type
DynArray = array of integer;
procedure findFactors(n: Int64; var d: DynArray);
var
divisor, next, rest: Int64;
i: integer;
begin
i := 0;
divisor := 2;
next := 3;
rest := n;
while (rest <> 1) do
begin
while (rest mod divisor = 0) do
begin
setlength(d, i+1);
d[i] := divisor;
inc(i);
rest := rest div divisor;
end;
divisor := next;
next := next + 2; // try only odd numbers
// cut condition: avoid many useless iterations
if (rest < divisor * divisor) then
begin
setlength(d, i+1);
d[i] := rest;
rest := 1;
end;
end;
end;
var
factors: DynArray;
j: integer;
begin
setlength(factors, 1);
findFactors(1023*1024, factors);
for j := low(factors) to high(factors) do
writeln (factors[j]);
readln;
end.

View file

@ -0,0 +1,40 @@
include "NSlog.incl"
CFStringRef local fn PrimeFactors( num as UInt64 )
UInt64 i, count = 0, n = num
if ( n < 0 ) then n = -n
while ( n % 2 == 0 )
mda(count) = 2 : count++ : n = n/2
wend
for i = 3 to sqr(n) + 1 step 2
while ( n % i == 0 )
mda(count) = i : count++ : n = n/i
wend
next
if ( n > 2 ) then mda(count) = n
if ( num < 0 )then mda(0) = -mda_integer(0)
CFStringRef factorsStr = fn ArrayComponentsJoinedByString( fn MDAArray(0), @" * " ) : mda_kill
return fn StringWithFormat( @"%19llu factors: %@", num, factorsStr )
end fn = NULL
CFTimeInterval t : t = fn CACurrentMediaTime
NSLog( @"%@", fn PrimeFactors( 3 ) )
NSLog( @"%@", fn PrimeFactors( 7 ) )
NSLog( @"%@", fn PrimeFactors( 31 ) )
NSLog( @"%@", fn PrimeFactors( 127 ) )
NSLog( @"%@", fn PrimeFactors( 2047 ) )
NSLog( @"%@", fn PrimeFactors( 8191 ) )
NSLog( @"%@", fn PrimeFactors( 131071 ) )
NSLog( @"%@", fn PrimeFactors( 524287 ) )
NSLog( @"%@", fn PrimeFactors( 8388607 ) )
NSLog( @"%@", fn PrimeFactors( 536870911 ) )
NSLog( @"%@", fn PrimeFactors( 2147483647 ) )
NSLog( @"%@", fn PrimeFactors( 137438953471 ) )
NSLog( @"%@", fn PrimeFactors( 2199023255551 ) )
NSLog( @"%@", fn PrimeFactors( 8796093022207 ) )
NSLog( @"%@", fn PrimeFactors( 140737488355327 ) )
NSLog( @"%@", fn PrimeFactors( 9007199254740991 ) )
NSLog( @"%@", fn PrimeFactors( 576460752303423487 ) )
NSLog( @"\nTime to compute all these factors: %.3f ms",(fn CACurrentMediaTime-t)*1000 )
HandleEvents

View file

@ -0,0 +1,39 @@
<?php
// Prime decomposition
const MAX_FAC_INDEX = 30; // -(2^31) has most prime factors (31 twos) than other 32-bit signed integer.
$facs = array_fill(0, MAX_FAC_INDEX + 1, 0);
test(2);
test(2520);
test(13);
function test($n) {
echo $n.' => ';
$cnt = facs_cnt($n, $facs);
for ($i = 0; $i <= $cnt - 2; $i++)
echo $facs[$i].' ';
echo $facs[$cnt - 1].PHP_EOL;
}
function facs_cnt(int $n, &$facs): int {
$n = abs($n);
$cnt = 0;
if ($n >= 2) {
$i = 2;
while ($i * $i <= $n) {
if ($n % $i == 0) {
$n = intdiv($n, $i);
$facs[$cnt] = $i;
$cnt++;
$i = 2;
}
else {
$i++;
}
}
$facs[$cnt] = $n;
$cnt++;
}
return $cnt;
}
?>

View file

@ -0,0 +1,47 @@
program primdecomp(input, output);
(* Prime decomposition *)
const
maxfacindex = 30;
(* -(2^31) has most prime factors (31 twos) than other 32-bit signed integer.
*)
type
tfacs = array[0 .. maxfacindex] of integer;
var
i, n, cnt: integer;
facs: tfacs;
function facscnt(n: integer; var facs: tfacs): integer;
var
i, cnt: integer;
begin
n := abs(n);
cnt := 0;
if n >= 2 then
begin
i := 2;
while i * i <= n do
begin
if n mod i = 0 then
begin
n := n div i;
facs[cnt] := i;
cnt := cnt + 1;
i := 2;
end
else
i := i + 1;
end;
facs[cnt] := n;
cnt := cnt + 1;
end;
facscnt := cnt;
end;
begin
write('Enter a number: ');
read(n);
cnt := facscnt(n, facs);
for i := 0 to cnt - 2 do write(facs[i]:1, ' ');
writeln(facs[cnt - 1]:1);
(* readln; *)
end.

View file

@ -0,0 +1,5 @@
local int = require "int"
local fmt = require "fmt"
local vals = {1 << 31, 1234567, 333333, 987653, 2 * 3 * 5 * 7 * 11 * 13 * 17}
for vals as val do fmt.print("%10d -> %,s", val, int.factors(val)) end

View file

@ -0,0 +1,32 @@
function eratosthenes ($n) {
if($n -gt 1){
$prime = @(1..($n+1) | foreach{$true})
$prime[1] = $false
$m = [Math]::Floor([Math]::Sqrt($n))
function multiple($i) {
for($j = $i*$i; $j -le $n; $j += $i) {
$prime[$j] = $false
}
}
multiple 2
for($i = 3; $i -le $m; $i += 2) {
if($prime[$i]) {multiple $i}
}
1..$n | where{$prime[$_]}
} else {
Write-Error "$n is not greater than 1"
}
}
function prime-decomposition ($n) {
$array = eratosthenes $n
$prime = @()
foreach($p in $array) {
while($n%$p -eq 0) {
$n /= $p
$prime += @($p)
}
}
$prime
}
"$(prime-decomposition 12)"
"$(prime-decomposition 100)"

View file

@ -0,0 +1,17 @@
function prime-decomposition ($n) {
$values = [System.Collections.Generic.List[string]]::new()
while ((($n % 2) -eq 0) -and ($n -gt 2)) {
$values.Add(2)
$n /= 2
}
for ($i = 3; $n -ge ($i * $i); $i += 2) {
if (($n % $i) -eq 0){
$values.Add($i)
$n /= $i
$i -= 2
}
}
$values.Add($n)
return $values
}
"$(prime-decomposition 1000000)"

View file

@ -0,0 +1,41 @@
' Prime decomposition
DECLARE SUB Test (N AS INTEGER)
DECLARE FUNCTION FacsCnt% (BYVAL N AS INTEGER, Facs() AS INTEGER)
CONST MAXFACINDEX% = 30
Test 2
Test 2520
Test 13
END
FUNCTION FacsCnt% (BYVAL N AS INTEGER, Facs() AS INTEGER)
DIM I AS INTEGER, Cnt AS INTEGER
N = ABS(N)
Cnt = 0
IF N >= 2 THEN
I = 2
DO WHILE I * I <= N
IF N MOD I = 0 THEN
N = N \ I
Facs(Cnt) = I
Cnt = Cnt + 1
I = 2
ELSE
I = I + 1
END IF
LOOP
Facs(Cnt) = N
Cnt = Cnt + 1
END IF
FacsCnt% = Cnt
END FUNCTION
SUB Test (N AS INTEGER)
DIM Facs(0 TO MAXFACINDEX%) AS INTEGER
DIM Cnt AS INTEGER, I AS INTEGER
PRINT N; "=>";
Cnt = FacsCnt%(N, Facs())
FOR I = 0 TO Cnt - 2
PRINT Facs(I);
NEXT I
PRINT Facs(Cnt - 1)
END SUB

View file

@ -0,0 +1,73 @@
-- 25 Apr 2026
Main:
include Setting
Memo.cache=0
say 'PRIME DECOMPOSITION'
say version
say 'Task'
say
numeric digits 100
call ShowFactors 100,120
call Timer 'R'
call ShowFactors 720720
call Timer 'R'
call ShowFactors 9007199254740991
call Timer 'R'
call ShowFactors 2543821448263974486045199
call Timer 'R'
call ShowFactors 2942942095527502756823568345688
call Timer 'R'
call ShowFactors 340282366920938463463374607431768211455
call Timer 'R'
call ShowFactors Primorial(150)
call Timer 'R'
numeric digits 50
call ShowMersenne
call Timer 'R'
exit
ShowFactors:
-- Show factors for a range numbers
arg xx,yy
if yy='' then
yy=xx
do i=xx to yy
call Charout ,i '= '
f=FactorS(i)
if f=1 then
call Charout ,'Prime'
else do
do j=1 to f
if j<f then
call Charout ,Fact.j 'x '
else
call Charout ,Fact.j
end
end
say
end
return
ShowMersenne:
-- Show factors for Mersenne numbers
p=2
do until p>100
m=2**p-1; f=FactorS(m)
call Charout ,'M'p '=' m '= '
if f=1 then
call Charout ,'Prime'
else do
do j=1 to f-1
call Charout ,Fact.j 'x '
end
call Charout ,Fact.f
end
say
p=Nextprime(p)
end
return
-- Nextprime; FactorS; Timer
include Math

View file

@ -0,0 +1,39 @@
-- 25 Apr 2026
Main:
include Setting
Memo.cache=0
say 'PRIME DECOMPOSITION'
say version
arg digs
-- Above 30 digits factorizations may become slow
if digs='' then
digs=30
say 'Endless run max' digs 'digits'
say
numeric digits 2*digs+10
say 'Seqnum Elapsed' Left('Number',digs) 'Prime factors'
n=0
-- Run forever
do forever
-- Generate random number with 1 to given digits
arg1=''
do Random(1,digs)
arg1=arg1||Random(9)
end
if arg1=0 then
iterate
n+=1
-- Factorize
arg1/=1; f=FactorS(arg1)
-- Show prime factors
l=Right(n,6) Format(Elaps('r'),3,3) Left(arg1,digs)
if f=0 then
say l 'failed'
else
say l Stem2struct('Fact.')
end
exit
-- FactorS; Stem2struct; Elaps
include Math

View file

@ -0,0 +1 @@
≡(&p$"_: \t_"⟜°/×)[2147483648 1234567 333333 987653 510510]

View file

@ -0,0 +1,26 @@
fn is_prime(num int) bool {
if num <= 1 {return false }
if num % 2 == 0 && num != 2 { return false }
for i := 3; i <= (num / 2) - 1; i += 2 {
if num % i == 0 { return false }
}
return true
}
fn decomp(nr int) {
mut x := ""
for i := 1; i <= nr; i++ {
if is_prime(i) && nr % i == 0 { x += "${i} * " }
}
if x.len > 2 {
x2 := x[..x.len - 3] // Remove last " * "
println("${nr} = ${x2}")
} else {
println("${nr} has no prime factors")
}
}
fn main() {
prime := 18705
decomp(prime)
}

View file

@ -0,0 +1,43 @@
Function PrimeFactors(n)
arrP = Split(ListPrimes(n)," ")
divnum = n
Do Until divnum = 1
'The -1 is to account for the null element of arrP
For i = 0 To UBound(arrP)-1
If divnum = 1 Then
Exit For
ElseIf divnum Mod arrP(i) = 0 Then
divnum = divnum/arrP(i)
PrimeFactors = PrimeFactors & arrP(i) & " "
End If
Next
Loop
End Function
Function IsPrime(n)
If n = 2 Then
IsPrime = True
ElseIf n <= 1 Or n Mod 2 = 0 Then
IsPrime = False
Else
IsPrime = True
For i = 3 To Int(Sqr(n)) Step 2
If n Mod i = 0 Then
IsPrime = False
Exit For
End If
Next
End If
End Function
Function ListPrimes(n)
ListPrimes = ""
For i = 1 To n
If IsPrime(i) Then
ListPrimes = ListPrimes & i & " "
End If
Next
End Function
WScript.StdOut.Write PrimeFactors(CInt(WScript.Arguments(0)))
WScript.StdOut.WriteLine