Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1,46 @@
scope # Totient function
# returns the number of integers k where 1 <= k <= n that are mutually prime to n
local procedure totient( n :: number ) :: number
if n < 3 then result := 1
elif n = 3 then result := 2
else
result := n;
local v, i := n, 2;
while i * i <= v do
if v mod i = 0 then
while v mod i = 0 do v \:= i od;
result -:= result \ i
fi;
if i = 2 then
i := 1
fi;
i +:= 2
od;
if v > 1 then result -:= result \ v fi;
fi;
return result
end;
# show the totient function values for the first 25 integers
io.write( " n phi(n) remarks\n" );
for n to 25 do
local constant tn := totient( n );
printf( "%2d: %5d%s\n", n, tn, if tn = n - 1 and tn <> 0 then " n is prime" else "" fi )
od;
# use the totient function to count primes
local n100, n1_000, n10_000, n100_000 := 0, 0, 0, 0;
for n to 100_000 do
if totient( n ) = n - 1 then
if n <= 100 then n100 +:= 1 fi;
if n <= 1000 then n1_000 +:= 1 fi;
if n <= 10000 then n10_000 +:= 1 fi;
if n <= 100000 then n100_000 +:= 1 fi
fi
od;
printf( "There are %6d primes below 100\n", n100 );
printf( "There are %6d primes below 1 000\n", n1_000 );
printf( "There are %6d primes below 10 000\n", n10_000 );
printf( "There are %6d primes below 100 000\n", n100_000 )
end

View file

@ -0,0 +1,54 @@
program totient_function
implicit none
integer, parameter :: M = 100000
integer :: i, j
integer, dimension(1:M) :: phi
character(len=10) :: is_prime
integer :: count100, count1000, count10000, count100000
! Initialize phi array
do i = 1, M
phi(i) = i
end do
! Compute totient using sieve method
do i = 2, M
if (phi(i) == i) then ! i is prime
do j = i, M, i
phi(j) = (phi(j) / i) * (i - 1)
end do
end if
end do
! Display for the first 25 integers
print*,'n phi prime'
print*,repeat('-',30)
do i = 1, 25
if (phi(i) == i - 1) then
is_prime = 'prime'
else
is_prime = 'not prime'
end if
print "(i0,t10,i3,t20,a)", i, phi(i), trim(is_prime)
end do
! Count primes up to specified limits
count100 = 0
count1000 = 0
count10000 = 0
count100000 = 0
do i = 2, M
if (phi(i) == i - 1) then
if (i <= 100) count100 = count100 + 1
if (i <= 1000) count1000 = count1000 + 1
if (i <= 10000) count10000 = count10000 + 1
count100000 = count100000 + 1
end if
end do
print *, 'Count of primes up to 100:', count100
print *, 'Count of primes up to 1,000:', count1000
print *, 'Count of primes up to 10,000:', count10000
print *, 'Count of primes up to 100,000:', count100000
end program totient_function

View file

@ -22,23 +22,20 @@ do
io.write( " n phi(n) remarks\n" )
for n = 1,25 do
local tn = totient( n )
io.write( string.format( "%2d", n ), ": ", string.format( "%5d", tn )
, ( tn == n - 1 and tn ~= 0 and " n is prime" or "" )
, "\n"
)
io.write( string.format( "%2d: %5d%s\n", n, tn, ( tn == n - 1 and tn ~= 0 and " n is prime" or "" ) ) )
end
-- use the totient function to count primes
local n100, n1000, n10000, n100000 = 0, 0, 0, 0
local n100, n1_000, n10_000, n100_000 = 0, 0, 0, 0
for n = 1,100000 do
if totient( n ) == n - 1 then
if n <= 100 then n100 = n100 + 1 end
if n <= 1000 then n1000 = n1000 + 1 end
if n <= 10000 then n10000 = n10000 + 1 end
if n <= 100000 then n100000 = n100000 + 1 end
if n <= 100 then n100 = n100 + 1 end
if n <= 1000 then n1_000 = n1_000 + 1 end
if n <= 10000 then n10_000 = n10_000 + 1 end
if n <= 100000 then n100_000 = n100_000 + 1 end
end
end
io.write( "There are ", string.format( "%6d", n100 ), " primes below 100\n" )
io.write( "There are ", string.format( "%6d", n1000 ), " primes below 1 000\n" )
io.write( "There are ", string.format( "%6d", n10000 ), " primes below 10 000\n" )
io.write( "There are ", string.format( "%6d", n100000 ), " primes below 100 000\n" )
io.write( string.format( "There are %6d primes below 100\n", n100 ) )
io.write( string.format( "There are %6d primes below 1 000\n", n1_000 ) )
io.write( string.format( "There are %6d primes below 10 000\n", n10_000 ) )
io.write( string.format( "There are %6d primes below 100 000\n", n100_000 ) )
end

View file

@ -0,0 +1,33 @@
local function totient(n)
local tot = n
local i = 2
while i * i <= n do
if n % i == 0 then
while n % i == 0 do n //= i end
tot -= tot // i
end
if i == 2 then i = 1 end
i += 2
end
if n > 1 do tot -= tot // n end
return tot
end
print(" n phi prime")
print("---------------")
local count = 0
for n = 1, 25 do
local tot = totient(n)
local isPrime = (n - 1) == tot
if isPrime then ++count end
print(string.format("%2d %2d %s", n, tot, isPrime))
end
print("\nNumber of primes up to 25 = " .. count)
for n = 26, 100_000 do
local tot = totient(n)
if tot == n - 1 then ++count end
if n == 100 or n == 1_000 or n % 10_000 == 0 then
local ns = string.format("%-6d", n)
print($"\nNumber of primes up to {ns} = {count}")
end
end