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,68 @@
-- Magic constants
-- J. Carter 2024 May
with Ada.Text_IO;
with System;
procedure Magic_Constant is
type Big_U is mod System.Max_Binary_Modulus;
function Magic (Order : in Big_U) return Big_U with
Pre => Order > 2;
-- Returns the constant for the magic square of order Order
function Order (Guess : in Big_U) return Big_U;
-- Returns the order of the smallest magic square with constant > Guess
function Image (N : in Big_U; Width : in Positive) return String;
-- Returns a blank-filled image of N of at least width characters
function Magic (Order : in Big_U) return Big_U is
(Order * (Order ** 2 + 1) / 2);
function Order (Guess : in Big_U) return Big_U is
Min : Big_U := 3;
Max : Big_U := 5_850_000;
Mid : Big_U;
Prev : Big_U := 0;
begin -- Order
Search : loop
Mid := Min + (Max - Min) / 2;
exit Search when Mid = Prev;
Prev := Mid;
if Magic (Mid) > Guess and (Mid = 3 or else Magic (Mid - 1) <= Guess) then
return Mid;
end if;
if Magic (Mid) > Guess then
Max := Mid;
else
Min := Mid;
end if;
end loop Search;
raise Program_Error with "Order: no solution for" & Guess'Image;
end Order;
function Image (N : in Big_U; Width : in Positive) return String is
Raw : String renames N'Image;
Img : String renames Raw (2 .. Raw'Last);
begin -- Image
return (1 .. Width - Img'Length => ' ') & Img;
end Image;
begin -- Magic_Constant
First_20 : for N in Big_U range 3 .. 22 loop
Ada.Text_IO.Put_Line (Item => Image (N, 2) & Image (Magic (N), 5) );
end loop First_20;
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line (Item => "1000" & Magic (1000)'Image);
Ada.Text_IO.New_Line;
Ten_To : for P in 1 .. (if Big_U'Size < 64 then 9 elsif Big_U'Size < 128 then 18 else 20) loop
Ada.Text_IO.Put_Line (Item => "10 ** " & Image (Big_U (P), 2) & Image (Order (10 ** P), 8) );
end loop Ten_To;
end Magic_Constant;

View file

@ -0,0 +1,21 @@
on magic(n)
n * (n ^ 2 + 1) / 2
end magic
repeat with i from 3 to 22
log magic(i) as integer
end repeat
log magic(1003) as integer
on inv_magic(lower)
set n to 3
repeat until magic(n) > lower
set n to n + 1
end repeat
return n
end inv_magic
repeat with i from 1 to 20
log inv_magic(10 ^ i)
end repeat

View file

@ -0,0 +1,115 @@
! Magic constant
! tested with Intel ifx (IFX) 2025.2.1 20250806 on Kubuntu 25.10
! GNU gfortran (Ubuntu 15.2.0-4ubuntu4) 15.2.0 on Kubuntu 25.10
! VSI Fortran x86-64 V8.7-001 on OpenVMS V9.2-3
! U.B., March 2026
!
program magicConstant
implicit none
integer,parameter :: DP=8, k_int=4
real(kind=DP), parameter :: realten=10._DP
integer(kind=k_int) :: order, ii
! Starting at order 3, show the first 20 magic constants.
write (*, '("Count Order Magic")')
order = 3
do ii=1,20
write (*,'(i5, 2x, i5,2x,i0)') ii, order, magicNumber (order)
order = order + 1
end do
! Show the 1000th magic constant. (Order 1003)
! 1st magic constant is for order 3, i.e. order = count+2
write (*,*)
order = 1002
ii = 1000
write (*,'(i5, 2x, i5,2x,i0,/)') ii, order, magicNumber (order)
!
! Find and show the order of the smallest N x N magic square whose constant is greater than 10^1 through 10^10.
! Stretch
! Find and show the order of the smallest N x N magic square whose constant is greater than 10^11 through 10^20.
!
write (*, '(" Limit Order")')
do i=1,20
order = unmagic (realten**i) ! Using REAL for all 10^i to avoid 132 bit integers
write (*,'("10**", i2, 2x, i0)') i, order
end do
contains
! =============================================
! Return the magic number of a sqare of order n
! =============================================
pure function magicNumber (n) result (retval)
implicit none
integer(kind=k_int), intent(in) :: n
integer(kind=k_int) :: retval
integer (kind=k_int) :: nn
nn = n
retval = (nn**3+nn) / 2
end function magicNumber
! =====================================================================================
! return the order of the smallest magic square with a magic number >= a given "magic"
! =====================================================================================
function unMagic (magic) result (order)
implicit none
real (kind=DP), intent(in) :: magic
real (kind=DP) :: guess
integer(kind=k_int) :: order
integer(kind=k_int) :: lo,hi,mid, mnc, mnf
! Following formula calculates an approximate value of the order we're looking for.
! This is because the term n^3 in the formula for the Magic Number is dominant,
! so the correct (real) value of the magic square's order is most likely between
! Floor and Ceiling of this first guess, so the integer result is CEILING (guess)
!
! It turned out this approximation and using ceiling(this guess) as function
! value is correct for all test values up to 10^20.
guess = (2._DP*magic)**(1._8/3._8)
! This check is only here because above reasoning is (maybe) plausible, but it is
! not a conclusive proof. This function allows us to check whether any results are incorrect.
if (checkGuess (magic, guess)) then
order = ceiling (guess, k_int)
else
order=-1 ! signal fault
print *, 'Problem: Guess for inverse magic fails for ', magic
endif
end function unMagic
! ========================================================================================================
! Check if the value of "guess" fulfills the condition
! magicNumber (floor(guess) <= magic < magicNumber(ceiling(guess)
! Special care has be taken to avoid integer overflows. Note that argument 'magic' can be as large as 10^20,
! and 'guess' is (2*guess)^(1/3)), which is .lt. 10^7. So guess , floor(guess) and ceiling(guess)
! can be represented by a 32-bin integer word, but 'magic' would cause an integer overflow.
! So we replicate the calculation of the integer function magicNumber(n) here, but we
! use double precision results instead of integers.
! ========================================================================================================
!
function checkGuess (magic, guess) result (guessIsOK)
real (kind=DP), intent(in) :: magic, guess
logical :: guessIsOK
real (kind=DP) :: FloorGuess, CeilingGuess
real (kind=dp) :: CeilingMagic,FloorMagic
FloorGuess = real (floor (guess, k_int) , DP)
FloorMagic = (Floorguess**3 + Floorguess) /2.0_DP
CeilingGuess = real (ceiling (guess, k_int) , DP)
CeilingMagic = (CeilingGuess**3 + Ceilingguess) /2.0_DP
guessIsOK = FloorMagic .le. magic .and. magic .lt. CeilingMagic
end function checkGuess
end program magicConstant

View file

@ -0,0 +1,21 @@
function magic(n) {
return n * (n ** 2 + 1) / 2;
}
for (let i = 3; i < 23; i++) {
console.log(magic(i));
}
console.log(magic(1003));
function inv_magic(lower) {
let n = 3;
while (magic(n) <= lower) {
n++;
}
return n;
}
for (let i = 1; i < 21; i++) {
console.log(inv_magic(10 ** i));
}

View file

@ -0,0 +1,8 @@
magic(n) := sum(i, i, 1, n^2)/n$ /* Defining it this way makes computing the inverse too slow */
magic(n) := n*(n^2+1)/2$ /* Therefore, we have to do it this way */
map(magic, makelist(i, i, 3, 22));
magic(1003);
inv_magic(lower) := block(n: 3, while(magic(n)<=lower) do(n: n+1), n)$
map(inv_magic, makelist(10^i, i, 1, 10));

View file

@ -1,21 +1,19 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #008080;">function</span> <span style="color: #000000;">magic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">nth</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">order</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nth</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">*</span><span style="color: #000000;">order</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: #000000;">order</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;">"First 20 magic constants: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">),</span><span style="color: #000000;">magic</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;">"1000th magic constant: %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">magic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)})</span>
function magic(integer nth)
integer order = nth+2
return (order*order+1)/2 * order
end function
printf(1,"First 20 magic constants: %V\n",{apply(tagset(20),magic)})
printf(1,"1000th magic constant: %,d\n",{magic(1000)})
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
include mpfr.e
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">goal</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">20</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">goal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_nthroot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</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;">"1e%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--
mpz {goal, order} = mpz_inits(2)
for i=1 to 20 do
mpz_ui_pow_ui(goal,10,i)
mpz_mul_si(order,goal,2)
mpz_nthroot(order,order,3)
mpz_add_si(order,order,1)
printf(1,"1e%d: %s\n",{i,mpz_get_str(order,10,true)})
end for

View file

@ -0,0 +1,17 @@
local fmt = require "fmt"
local magic_constant = |n| -> (n * n + 1) * n / 2
print("First 20 magic constants:")
local mc20 = range(3, 22):map(|n| -> magic_constant(n))
for mc20:chunk(10) as chunk do fmt.tprint("%5d", chunk) end
fmt.print("\n1,000th magic constant: %,s", magic_constant(1002))
print("\nSmallest order magic square with a constant greater than:")
for i = 1, 20 do
local goal = 10 ^ i
local order = math.floor(math.cbrt(goal * 2)) + 1
local len = (i <= 3) ? -3 : (4 <= i <= 9) ? -4 : -2
fmt.print($"10%{len}s : %,9s", fmt.super(i), order)
end

View file

@ -0,0 +1,13 @@
#This is equivalent to n*(n^2+1)/2, but it's easier to see where this form comes from
magic <- function(n) sum(1:(n^2))/n
sapply(3:22, magic)
magic(1003)
inv_magic <- function(lower){
n <- 3
while(magic(n)<=lower) n <- n+1
return(n)
}
sapply(cumprod(rep(10, 20)), inv_magic)