Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,110 +0,0 @@
with Ada.Text_IO;
procedure Calkin_Wilf is
type Rational is
record
Numerator : Integer;
Denominator : Positive;
end record;
-- A generic solution would use Ada.Containers.Vectors, but
-- these puzzles are written to avoid numbers larger than
-- what fit in 64 bits, so we can assume a limit of 64 terms
type Term_Array is array (1 .. 64) of Positive;
type Continued_Fraction is
record
Count : Positive;
Terms : Term_Array;
end record;
-- Don't bother with reducing or negatives
function "+" (A, B : Rational) return Rational is
(A.Numerator * B.Denominator + B.Numerator * A.Denominator,
A.Denominator * B.Denominator);
function "-" (A : Rational) return Rational is
(-A.Numerator, A.Denominator);
function "-" (A, B : Rational) return Rational is
(A + (-B));
function "*" (A, B : Rational) return Rational is
(A.Numerator * B.Numerator,
A.Denominator * B.Denominator);
function Invert (A : Rational) return Rational is
(A.Denominator,
A.Numerator);
function Floor (A : Rational) return Rational is
(A.Numerator / A.Denominator,
1);
function Image (A : Rational) return String is
(A.Numerator'Image & " /" & A.Denominator'Image);
function "=" (A, B : Rational) return Boolean is
(A.Numerator = B.Numerator and A.Denominator = B.Denominator);
function Next_Calkin_Wilf_Term (R : Rational) return Rational is
(Invert ((2, 1) * Floor (R) + (1, 1) - R));
procedure Put_First_Terms (N : Positive) is
R : Rational := (1, 1);
begin
Ada.Text_IO.Put_Line (
"The first " & N'Image & " terms of the Calkin-Wilf Sequence are:");
for I in 1 .. N loop
Ada.Text_IO.Put_Line (I'Image & ": " & Image (R));
R := Next_Calkin_Wilf_Term (R);
end loop;
end Put_First_Terms;
function To_Continued_Fraction (R : Rational) return Continued_Fraction is
Count : Natural := 0;
Terms : Term_Array;
N : Natural := R.Numerator;
D : Natural := R.Denominator;
M : Natural;
begin
while D > 0 loop
Count := Count + 1;
Terms (Count) := N / D;
M := N mod D;
N := D;
D := M;
end loop;
if Count mod 2 = 0 then
Terms (Count .. Count + 1) := (Terms (Count) - 1, 1);
Count := Count + 1;
end if;
return (Count, Terms);
end To_Continued_Fraction;
function To_Index (R : Rational) return Natural is
Cont_Frac : Continued_Fraction := To_Continued_Fraction (R);
Terms : Term_Array renames Cont_Frac.Terms;
Index : Natural := 0;
begin
for I in reverse 1 .. Cont_Frac.Count loop
for J in 1 .. Terms (I) loop
Index := Index * 2 + (I mod 2);
end loop;
end loop;
return Index;
end To_Index;
procedure Put_Term_Index (R : Rational) is
Index : Natural := To_Index (R);
begin
Ada.Text_IO.Put_Line ("Term " & Image (R) & " is at index " & Index'Image);
end Put_Term_Index;
begin
Put_First_Terms (20);
Put_Term_Index((83116, 51639));
end Calkin_Wilf;

View file

@ -1,5 +1,5 @@
n: new 1
d: new 1
n: 1
d: 1
calkinWilf: function [] .export:[n,d] [
n: (d - n) + 2 * (n/d) * d
tmp: d
@ -12,9 +12,9 @@ first20: [[1 1]] ++ map 1..19 => calkinWilf
print "The first 20 terms of the Calkwin-Wilf sequence are:"
print map first20 'f -> ~"|f\0|/|f\1|"
n: new 1
d: new 1
indx: new 1
n: 1
d: 1
indx: 1
target: [83116, 51639]

View file

@ -1,23 +1,27 @@
subr first
fastfunc next n d .
n = 2 * (n div d) * d + d - n
return n
.
fastfunc search n0 d0 .
n = 1
d = 1
.
proc next .
n = 2 * (n div d) * d + d - n
swap n d
i = 1
while n <> n0 or d <> d0
dp = d
d = next n d
n = dp
i += 1
.
return i
.
print "The first 20 terms of the Calkwin-Wilf sequence are:"
first
n = 1
d = 1
for i to 20
write n & "/" & d & " "
next
dp = d
d = next n d
n = dp
.
print ""
#
first
i = 1
while n <> 83116 or d <> 51639
next
i += 1
.
print "83116/51639 is at position " & i
print "83116/51639 is at position " & search 83116 51639

View file

@ -42,7 +42,7 @@ fn to_continued(r fractions.Fraction) []int {
return res
}
fn get_term_number(cf []int) ?int {
fn get_term_number(cf []int) !int {
mut b := ""
mut d := "1"
for n in cf {
@ -53,7 +53,7 @@ fn get_term_number(cf []int) ?int {
d = "1"
}
}
i := strconv.parse_int(b, 2, 64)?
i := strconv.parse_int(b, 2, 64)!
return int(i)
}
@ -74,7 +74,7 @@ fn commatize(n int) string {
fn main() {
cw := calkin_wilf(20)
println("The first 20 terms of the Calkin-Wilf sequnence are:")
println("The first 20 terms of the Calkin-Wilf sequence are:")
for i := 1; i <= 20; i++ {
println("${i:2}: ${cw[i-1]}")
}