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,11 @@
100 REM Babbage problem
110 REM We can start at the square root of 269696
120 LET I = 520
130 REM 269696 is a multiple of 4, 520 too
140 REM so we can increment I by 4
150 DO WHILE MOD((I * I), 1000000) <> 269696
160 LET I = I + 4
170 LOOP
180 PRINT "The smallest positive integer whose square ends in the digits 269696 is"; I
190 PRINT "Its square is"; I * I
200 END

View file

@ -0,0 +1,49 @@
#!/usr/bin/awk -f
# Quite efficient solution based on modulo arithmetic
# needs 270 square/mod calculations (variable tally)
# to calculate all solutions, and stop if none
BEGIN {
q = 269696
if (ARGC > 1) q = ARGV[1]
# print q ":"
set[1] = 0
m = 1
while (m < q && length(set) > 0) {
new_set(set, m, q)
m *= 10
}
if (length(set) > 1) {
asort(set)
for (i in set) {
v = set[i]
printf("%6d^2 -- %12d\n", v, v*v)
}
print tally " square/mod calculations"
} else {
print "NO SOLUTION"
}
}
function new_set (old, m, q, new, mm, qm, i, j, k, l) {
mm = 10 * m
qm = q % mm
for (i in old)
for (j = 0; j < 10*m; j += m) {
tally += 1
k = i + j
l = (k*k) % mm
if (l == qm)
new[k] = k
}
# new set in old
delete old
for (i in new)
old[i] = new[i]
# show(new)
}
function show(set) {
for (i in set)
printf("%d ", i)
print ""
}

View file

@ -0,0 +1,54 @@
-- The program is written in the programming language Ada. The name "Ada"
-- has been chosen in honour of your friend,
-- Augusta Ada King-Noel, Countess of Lovelace (née Byron).
--
-- This is an program to search for the smallest integer X, such that
-- (X*X) mod 1_000_000 = 269_696.
--
-- In the Ada language, "*" represents the multiplication symbol, "mod" the
-- modulo reduction, and the underscore "_" after every third digit in
-- literals is supposed to simplify reading numbers for humans.
-- Everything written after "--" in a line is a comment for the human,
-- and will be ignored by the computer.
with Ada.Text_IO;
-- We need this to tell the computer how it will later output its result.
procedure Babbage_Problem is
-- We know that 99_736*99_736 is 9_947_269_696. This implies:
-- 1. The smallest X with X*X mod 1_000_000 = 269_696 is at most 99_736.
-- 2. The largest square X*X, which the program may have to deal with,
-- will be at most 9_947_269_69.
type Number is range 1 .. 99_736*99_736;
X: Number := 1;
-- X can store numbers between 1 and 99_736*99_736. Computations
-- involving X can handle intermediate results in that range.
-- Initially the value stored at X is 1.
-- When running the program, the value will become 2, 3, 4, etc.
begin
-- The program starts running.
-- The computer first squares X, then it truncates the square, such
-- that the result is a six-digit number.
-- Finally, the computer checks if this number is 269_696.
while not (((X*X) mod 1_000_000) = 269_696) loop
-- When the computer goes here, the number was not 269_696.
X := X+1;
-- So we replace X by X+1, and then go back and try again.
end loop;
-- When the computer eventually goes here, the number is 269_696.
-- E.e., the value stored at X is the value we are searching for.
-- We still have to print out this value.
Ada.Text_IO.Put_Line(Number'Image(X));
-- Number'Image(X) converts the value stored at X into a string of
-- printable characters (more specifically, of digits).
-- Ada.Text_IO.Put_Line(...) prints this string, for humans to read.
-- I did already run the program, and it did print out 25264.
end Babbage_Problem;

View file

@ -0,0 +1,54 @@
;; Let's Create a tool [a function] that helps us check positive integers
;; to see which one's square is the first one that ends in ...269696
;; It should be noted that a function can be used many times repetitively
;; so let's create that function which will be used many times.
;; First, define the function, give it a name and parameters; it will look like:
;; (defun [function_name] ([input_parameter] / [local_variables])
(defun SquareEndsWith269696? (number / )
;; Let's have the computer Square the number (whichever number was passed to the function)
(setq number (* number number))
;; Now let's convert that number to a string
(setq numberString (itoa number))
;; Is the length of the numberString greater than or equal to 6 digits?
(if (>= (strlen numberString) 6)
;; If it was greater than or equal to 6, we will check the last 6 characters to see if they are equal to "269696"
;; If this equality check is successful, then we have found our number, and will return a successful function check!
(eq "269696" (substr numberString (- (strlen numberString) 5)))
;; Otherwise, an unsuccessful function check will be returned
)
)
;; Second, we will create a function that only needs to be used once, they do not always need to be used many times
;; Let's use your name as the initiator/name for this function
(defun c:BABBAGE ( / integer SquareFound)
;; Once we have initiated the function, let's create our first integer to check
(setq integer 1)
;; Now, let's perform a loop do perform an action many times within the function
;; We do not want our loop to run infinitely, so let's create a True/False variable to identify if our task is complete
(setq SquareFound nil)
;; ok, let's use the loop and True/False variable to continually check if we have found our correct integer
(while (not SquareFound)
;; Now within the loop, let's use our First function to check if we have the correct integer
;; We will save the new result of our check over our existing True/False variable
(setq SquareFound (SquareEndsWith269696? integer))
;; If the square was not found...
(if (not SquareFound)
;; ...increase our integer number by 1
(setq integer (1+ integer))
)
;; Once the integer is found, our loop will end and the code will continue forward
;; Otherwise, it will start back at "(while ..."
)
;; If we have made it this far, then our integer has been found!
;; let's show everyone what the integer is
(prompt
(strcat
"\nThe smallest integer has been found!"
"\nInteger: " (itoa integer) ;; <-- we are converting the integer to a string
"\nSquared: " (itoa (* integer integer)) ;; <-- let's show everyone the squared number to prove it
)
)
;; All done, this last line is not required, but makes the ending cleaner
(princ)
)

View file

@ -0,0 +1,9 @@
to babbage.problem
; We can start at the square root of 269696
make "I 520
; 269696 is a multiple of 4, 520 too
; so we can increment :i by 4
while [not (modulo (:i * :i) 1000000 = 269696)] [make "i :i + 4]
print sentence "|The smallest positive integer whose square ends in the digits 269696 is| :i
print sentence "|Its square is|" (:i * :i)
end

View file

@ -0,0 +1,32 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. BABBAGE-PROGRAM.
* A line beginning with an asterisk is an explanatory note.
* The machine will disregard any such line.
DATA DIVISION.
WORKING-STORAGE SECTION.
* In this part of the program we reserve the storage space we shall
* be using for our variables, using a 'PICTURE' clause to specify
* how many digits the machine is to keep free.
* The prefixed number 77 indicates that these variables do not form part
* of any larger 'record' that we might want to deal with as a whole.
77 N PICTURE 99999.
* We know that 99,736 is a valid answer.
77 N-SQUARED PICTURE 9999999999.
77 LAST-SIX PICTURE 999999.
PROCEDURE DIVISION.
* Here we specify the calculations that the machine is to carry out.
CONTROL-PARAGRAPH.
PERFORM COMPUTATION-PARAGRAPH VARYING N FROM 1 BY 1
UNTIL LAST-SIX IS EQUAL TO 269696.
STOP RUN.
COMPUTATION-PARAGRAPH.
MULTIPLY N BY N GIVING N-SQUARED.
MOVE N-SQUARED TO LAST-SIX.
* Since the variable LAST-SIX can hold a maximum of six digits,
* only the final six digits of N-SQUARED will be moved into it:
* the rest will not fit and will simply be discarded.
IF LAST-SIX IS EQUAL TO 269696 THEN DISPLAY N.
END PROGRAM BABBAGE-PROGRAM.

View file

@ -0,0 +1,7 @@
# brute force approach
i = Math.isqrt(269_696)
i -= 1 unless i.even?
while i*i % 1_000_000 != 269_696
i += 2
end
p i

View file

@ -0,0 +1,9 @@
EXPORT BabbageProblem()
BEGIN
PRINT();
PRINT("BABBAGE PROBLEM:");
I := 520;
WHILE I*I MOD 1000000 ≠ 269696 DO
I := I + 4;
END;
PRINT("Min X = " + I + " con X² = " + I*I);

View file

@ -0,0 +1,5 @@
procedure main(A)
tail := \A[1] | 269696
size := 10^*tail
if ((n := seq())^2 % size) = tail then write(n," -> ",n^2)
end

View file

@ -12,6 +12,7 @@ procedure solve_babbage_problem() -- (so that return quits 3 loops)
cc += 1
if remainder(square,r10) = remainder(269696,r10) then
if digits=6 then
-- if remainder(square,1e6)=269696 then
printf(1,"Solution: %d (%d calcs)\n",{cand,cc})
return -- leave solve_babbage_problem()
end if
@ -20,7 +21,8 @@ procedure solve_babbage_problem() -- (so that return quits 3 loops)
end for
end for
{cands,nextc} = {nextc,{}}
printf(1,"%d-digit candidates: %v\n",{digits,cands})
string cs = join(cands,",",fmt:=sprintf("%%0%dd",digits))
printf(1,"%d-digit candidates: %s\n",{digits,cs})
p10 *= 10
r10 *= 10
end for

View file

@ -0,0 +1,25 @@
--[[
The answer must be an even number and it can't be less than the square root of 269,696.
So, if we start from that, keep on adding 2 and squaring it we'll eventually find the answer.
However, we can skip numbers which don't end in 4 or 6 as their squares can't end in 6.
]]
local fmt = require "fmt" -- this enables us to format numbers with thousand separators
local start = math.sqrt(269696) -- get the square root of the starting value
start = math.ceil(start) -- get the next integer higher than (or equal to) the square root
start = math.ceil(start / 2) * 2 -- if it's odd, use the next even integer
local i = start -- assign it to a variable 'i' for use in the following loop
while true do -- loop indefinitely till we find the answer
local sq = i * i -- get the square of 'i'
local last6 = sq % 1000000 -- get its last 6 digits by taking the remainder after division by a million
if last6 == 269696 then -- if those digits are 269696, we're done and can print the result
fmt.print($"The lowest number whose square ends in 269,696 is %,s.", i)
fmt.print($"Its square is %,s.", sq)
break -- break from the loop and end the program
end
if i % 10 == 6 then -- get the last digit by taking the remainder after division by 10
i += 8 -- if the last digit is 6 add 8 (to end in 4)
else
i += 2 -- otherwise add 2
end
end

View file

@ -0,0 +1,45 @@
###########################################################################################
#
# Definitions:
#
# Lines that begin with the "#" symbol are comments: they will be ignored by the machine.
#
# -----------------------------------------------------------------------------------------
#
# While
#
# Run a command block based on the results of a conditional test.
#
# Syntax
# while (condition) {command_block}
#
# Key
#
# condition If this evaluates to TRUE the loop {command_block} runs.
# when the loop has run once the condition is evaluated again.
#
# command_block Commands to run each time the loop repeats.
#
# As long as the condition remains true, PowerShell reruns the {command_block} section.
#
# -----------------------------------------------------------------------------------------
#
# * means 'multiplied by'
# % means 'modulo', or remainder after division
# -ne means 'is not equal to'
# ++ means 'increment variable by one'
#
###########################################################################################
# Declare a variable, $integer, with a starting value of 0.
$integer = 0
while (($integer * $integer) % 1000000 -ne 269696)
{
$integer++
}
# Show the result.
$integer

View file

@ -0,0 +1,24 @@
# Start with the smallest potential square number
$TestSquare = 269696
# Test if our potential square is a square
# by testing if the square root of it is an integer
# Test if the square root is an integer by testing if the remainder
# of the square root divided by 1 is greater than zero
# % is the remainder operator
# -gt is the "greater than" operator
# While the remainder of the square root divided by one is greater than zero
While ( [Math]::Sqrt( $TestSquare ) % 1 -gt 0 )
{
# Add 100,000 to get the next potential square number
$TestSquare = $TestSquare + 1000000
}
# This will loop until we get a value for $TestSquare that is a square number
# Caclulate the root
$Root = [Math]::Sqrt( $TestSquare )
# Display the result and its square
$Root
$TestSquare

View file

@ -0,0 +1,10 @@
number: 510 ;; starting number
;; repeat, until the last condition in the block is true
until [
number: number + 2 ;; only even numbers can have even squares
;; The word modulo computes the non-negative remainder of the
;; first argument divided by the second argument.
;; ** => Returns a number raised to a given power (exponent)
269696 = modulo (number ** 2) 1000000
]
?? number

View file

@ -0,0 +1,6 @@
main (p):+
q = 269696 \ ending to be found
m = 1000000 \ corresponding modulus
for i = from 1 upto m^2
if i^2 %% m = q: break
print i, i^2, q

View file

@ -0,0 +1,39 @@
main (p):+
q =: string p.1 as integer else 269696
debug enable
debug print "Sets:"
\ central loop: calculate new set of endings for powers of 10
set =: new row size 1 init 0
m =: 1
while m < q && set.Count > 0
set =: new set set mod m find q
debug print join set
m =*10
rounds += 1
debug print ''
\ show result
print format "Result for %_;:" q
if set.Count < 1
print "NO SOLUTION"
return
row set sort ascending
for x =: row set give values
print format '%6;^2 = %16_;', x, x^2
print rounds, "rounds, ", $tally, "square/mod calculations"
$tally =: ()
\ calculate new set of endings mod m from m-1
new set (old) mod (m) find (q):
new =: new row
mm =: 10 * m
qm =: q %% mm
for i =: give values old
for j =: from 0 upto 9*m step m
k =: i + j
l =: k^2 %% mm
$tally += 1 \ global to count calculations
if l = qm
new[] =: k
return new

View file

@ -0,0 +1 @@
⍢(+1|≠269696◿₁₀₀₀₀₀₀˙×) 1

View file

@ -0,0 +1,25 @@
'Sir, this is a script that could solve your problem.
'Lines that begin with the apostrophe are comments. The machine ignores them.
'The next line declares a variable n and sets it to 0. Note that the
'equals sign "assigns", not just "relates". So in here, this is more
'of a command, rather than just a mere proposition.
n = 0
'Starting from the initial value, which is 0, n is being incremented
'by 1 while its square, n * n (* means multiplication) does not have
'a modulo of 269696 when divided by one million. This means that the
'loop will stop when the smallest positive integer whose square ends
'in 269696 is found and stored in n. Before I forget, "<>" basically
'means "not equal to".
Do While ((n * n) Mod 1000000) <> 269696
n = n + 1 'Increment by 1.
Loop
'The function "WScript.Echo" displays the string to the monitor. The
'ampersand concatenates strings or variables to be displayed.
WScript.Echo("The smallest positive integer whose square ends in 269696 is " & n & ".")
WScript.Echo("Its square is " & n*n & ".")
'End of Program.