Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -1,72 +1,76 @@
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Text_IO; with Ada.Integer_Text_IO;
with Ada.Strings.Fixed;
procedure Totient is
function Totient (N : in Integer) return Integer is
Tot : Integer := N;
I : Integer;
N2 : Integer := N;
begin
I := 2;
while I * I <= N2 loop
if N2 mod I = 0 then
while N2 mod I = 0 loop
N2 := N2 / I;
end loop;
Tot := Tot - Tot / I;
end if;
function Totient (N : in Integer) return Integer is
Tot : Integer := N;
I : Integer;
N2 : Integer := N;
begin
I := 2;
while I * I <= N2 loop
if N2 mod I = 0 then
while N2 mod I = 0 loop
N2 := N2 / I;
end loop;
Tot := Tot - Tot / I;
end if;
if I = 2 then
I := 1;
end if;
I := I + 2;
end loop;
if I = 2 then
I := 1;
end if;
I := I + 2;
end loop;
if N2 > 1 then
Tot := Tot - Tot / N2;
end if;
if N2 > 1 then
Tot := Tot - Tot / N2;
end if;
return Tot;
end Totient;
return Tot;
end Totient;
Count : Integer := 0;
Tot : Integer;
Placeholder : String := " n Phi Is_Prime";
Image_N : String renames Placeholder ( 1 .. 3);
Image_Phi : String renames Placeholder ( 6 .. 8);
Image_Prime : String renames Placeholder (11 .. 17);
use Ada.Text_IO;
use Ada.Integer_Text_IO;
Count : Integer := 0;
Tot : Integer;
Placeholder : String := " n Phi Is_Prime";
Image_N : String renames Placeholder ( 1 .. 3);
Image_Phi : String renames Placeholder ( 6 .. 8);
Image_Prime : String renames Placeholder (11 .. Placeholder'Last);
use Ada.Text_IO;
use Ada.Integer_Text_IO;
Last : constant := 25;
begin
Put_Line (Placeholder);
Put_Line (Placeholder);
for N in 1 .. 25 loop
Tot := Totient (N);
for N in 1 .. Last loop
Tot := Totient (N);
if N - 1 = Tot then
Count := Count + 1;
end if;
Put (Image_N, N);
Put (Image_Phi, Tot);
Image_Prime := (if N - 1 = Tot then " True" else " False");
Put_Line (Placeholder);
end loop;
New_Line;
if N - 1 = Tot then
Count := Count + 1;
end if;
Put (Image_N, N);
Put (Image_Phi, Tot);
Image_Prime := Ada.Strings.Fixed.Tail
(Source => (if N - 1 = Tot then True'Image else False'Image),
Count => Image_Prime'Length);
Put_Line (Placeholder);
end loop;
New_Line;
Put_Line ("Number of primes up to " & Integer'(25)'Image &" =" & Count'Image);
Put_Line ("Number of primes up to " & Last'Image &" =" & Count'Image);
for N in 26 .. 100_000 loop
Tot := Totient (N);
for N in Last + 1 .. 100_000 loop
Tot := Totient (N);
if Tot = N - 1 then
Count := Count + 1;
end if;
if Tot = N - 1 then
Count := Count + 1;
end if;
if N = 100 or N = 1_000 or N mod 10_000 = 0 then
Put_Line ("Number of primes up to " & N'Image & " =" & Count'Image);
end if;
end loop;
if N = 100 or N = 1_000 or N mod 10_000 = 0 then
Put_Line ("Number of primes up to " & N'Image & " =" & Count'Image);
end if;
end loop;
end Totient;

View file

@ -0,0 +1,37 @@
with javascript_semantics
atom t0 = time()
function totient(integer n)
integer tot = n, i = 2
while i*i<=n do
if mod(n,i)=0 then
while true do
n /= i
if mod(n,i)!=0 then exit end if
end while
tot -= tot/i
end if
i += iff(i=2?1:2)
end while
if n>1 then
tot -= tot/n
end if
return tot
end function
printf(1," n phi prime\n")
printf(1," --------------\n")
for n=1 to 25 do
integer tot = totient(n)
bool prime = (n-1=tot)
printf(1,"%2d %2d %t\n",{n,tot,prime})
end for
printf(1,"\n")
integer count = 0
for n=1 to 1e6 do
-- count += (totient(n)=n-1)
count += (phi(n)=n-1)
if find(n,{25,1e2,1e3,1e4,1e5,1e6,1e7}) then
printf(1,"Number of primes up to %d = %d\n",{n,count})
end if
end for
?elapsed(time()-t0)

View file

@ -0,0 +1,38 @@
with javascript_semantics
atom t0 = time()
constant maxphi = 1e7
sequence s_phi
procedure computePhi()
s_phi = tagset(maxphi)
for i=2 to maxphi do
if s_phi[i]=i then
for j=i to maxphi by i do
s_phi[j] -= s_phi[j] / i;
end for
end if
end for
end procedure
function countPrimes(int lo, hi)
int count = 0;
for i=lo to hi do
if s_phi[i] == i-1 then
count += 1
end if
end for
return count;
end function
computePhi()
printf(1," n phi prime\n")
printf(1," --------------\n")
for n=1 to 25 do
integer tot = s_phi[n]
bool prime = (n-1=tot)
printf(1,"%2d %2d %t\n",{n,tot,prime})
end for
printf(1,"\n")
for n in {25,1e2,1e3,1e4,1e5,1e6,1e7} do
printf(1,"Number of primes up to %d = %d\n",{n,countPrimes(1,n)})
end for
?elapsed(time()-t0)

View file

@ -1,37 +0,0 @@
(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">totient</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">tot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">tot</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">/</span><span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">tot</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">tot</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" n phi prime\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" --------------\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">tot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">totient</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">prime</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">tot</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">prime</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">isp</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prime</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"true"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"false"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d %2d %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tot</span><span style="color: #0000FF;">,</span><span style="color: #000000;">isp</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nNumber of primes up to 25 = %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">26</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100000</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">totient</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Number of primes up to %-6d = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--

View file

@ -0,0 +1,136 @@
include Settings
say version; say 'Totient function (Phi)'; say
call Time('r')
numeric digits 10; cycl. = 0; m = 7
call First25A
call PrimeCountA m
say Format(Time('e'),,3) 'seconds'; say
call Time('r')
call Totients 10**m
call First25B
call PrimeCountB m
say Format(Time('e'),,3) 'seconds'
exit
First25A:
procedure
say 'A: using calls to an optimized Totient()'; say
say ' N Phi(N) Prime?'
say Copies('-',16)
n = 0
do i = 1 to 25
p = Totient(i)
if p = i-1 then do
n = n+1; pr = 'Yes'
end
else
pr = ''
say Right(i,2) Right(p,6) pr
end
say Copies('-',16); say
say 'Found' Right(n,6) 'primes <' Right(25,8)
return
PrimeCountA:
procedure
arg x
n = 0; d = 1
do i = 1
p = Totient(i)
if p = i-1 then
n = n+1
e = Xpon(i)
if e > d then do
say 'Found' Right(n,6) 'primes <' Right(10**e,8)
if e > x-1 then
leave i
d = e
end
end
say
return
First25B:
procedure expose toti.
say 'B: generate and save all Totients, and use the stored values'; say
say ' N Phi(N) Prime?'
say Copies('-',16)
n = 0
do i = 1 to 25
p = toti.totient.i
if p = i-1 then do
n = n+1; pr = 'Yes'
end
else
pr = ''
say Right(i,2) Right(p,6) pr
end
say Copies('-',16)
say
say 'Found' Right(n,6) 'primes <' Right(25,8)
return
PrimeCountB:
procedure expose toti.
arg x
n = 0; d = 1
do i = 1
p = toti.totient.i
if p = i-1 then
n = n+1
e = Xpon(i)
if e > d then do
say 'Found' Right(n,6) 'primes <' Right(10**e,8)
if e > x-1 then
leave i
d = e
end
end
say
return
Totient:
/* Euler's totient function */
procedure expose toti.
arg x
/* Fast values */
if x < 3 then
return 1
if x < 5 then
return 2
/* Multiplicative property using Factors */
f = Factors(x)+1; fact.factor.f = 0
y = 1; v = fact.factor.1; n = 1
do i = 2 to f
a = fact.factor.i
if a = v then
n = n+1
else do
y = y*v**(n-1)*(v-1)
v = a; n = 1
end
end
return y
Totients:
/* Euler's totient numbers */
procedure expose toti.
arg x
/* Recurring sequence */
do i = 1 to x
toti.totient.i = i
end
do i = 2 to x
if toti.totient.i < i then
iterate i
do j = i by i to x
toti.totient.j = toti.totient.j-toti.totient.j/i
end
end
toti.0 = x
return x
include Functions
include Numbers
include Abend