Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,12 @@
package Sum_To is
generic
with procedure Callback(Str: String; Int: Integer);
procedure Eval;
generic
Number: Integer;
with function Print_If(Sum, Number: Integer) return Boolean;
procedure Print(S: String; Sum: Integer);
end Sum_To;

View file

@ -0,0 +1,40 @@
with Ada.Text_IO, Ada.Containers.Ordered_Maps;
package body Sum_To is
procedure Eval is
procedure Rec_Eval(Str: String; Previous, Current, Next: Integer) is
Next_Image: String := Integer'Image(Next);
-- Next_Image(1) holds a blank, Next_Image(2) a digit
function Sign(N: Integer) return Integer is
(if N<0 then -1 elsif N>0 then 1 else 0);
begin
if Next = 10 then -- end of recursion
Callback(Str, Previous+Current);
else -- Next < 10
Rec_Eval(Str & Next_Image(2), -- concatenate current and Next
Previous, Sign(Current)*(10*abs(Current)+Next), Next+1);
Rec_Eval(Str & "+" & Next_Image(2), -- add Next
Previous+Current, Next, Next+1);
Rec_Eval(Str & "-" & Next_Image(2), -- subtract Next
Previous+Current, -Next, Next+1);
end if;
end Rec_Eval;
begin -- Eval
Rec_Eval("1", 0, 1, 2); -- unary "+", followed by "1"
Rec_Eval("-1", 0, -1, 2); -- unary "-", followed by "1"
end Eval;
procedure Print(S: String; Sum: Integer) is
-- print solution (S,N), if N=Number
begin
if Print_If(Sum, Number) then
Ada.Text_IO.Put_Line(Integer'Image(Sum) & " = " & S & ";");
end if;
end Print;
end Sum_To;

View file

@ -0,0 +1,10 @@
with Sum_To;
procedure Sum_To_100 is
procedure Print_100 is new Sum_To.Print(100, "=");
procedure Eval_100 is new Sum_To.Eval(Print_100);
begin
Eval_100;
end Sum_To_100;

View file

@ -0,0 +1,67 @@
with Sum_To, Ada.Containers.Ordered_Maps, Ada.Text_IO;
use Ada.Text_IO;
procedure Three_Others is
package Num_Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Integer, Element_Type => Positive);
use Num_Maps;
Map: Num_Maps.Map;
-- global Map stores how often a sum did occur
procedure Insert_Solution(S: String; Sum: Integer) is
-- inserts a solution into global Map
use Num_Maps;
-- use type Num_Maps.Cursor;
Position: Cursor := Map.Find(Sum);
begin
if Position = No_Element then -- first solutions for Sum
Map.Insert(Key => Sum, New_Item => 1); -- counter is 1
else -- increase counter for Sum
Map.Replace_Element(Position => Position,
New_Item => (Element(Position))+1);
end if;
end Insert_Solution;
procedure Generate_Map is new Sum_To.Eval(Insert_Solution);
Current: Cursor; -- Points into Map
Sum: Integer; -- current Sum of interest
Max: Natural;
begin
Generate_Map;
-- find Sum >= 0 with maximum number of solutions
Max := 0; -- number of solutions for Sum (so far, none)
Current := Map.Ceiling(0); -- first element in Map with Sum >= 0
while Has_Element(Current) loop
if Element(Current) > Max then
Max := Element(Current); -- the maximum of solutions, so far
Sum := Key(Current); -- the Sum with Max solutions
end if;
Next(Current);
end loop;
Put_Line("Most frequent result:" & Integer'Image(Sum));
Put_Line("Frequency of" & Integer'Image(Sum) & ":" &
Integer'Image(Max));
New_Line;
-- find smallest Sum >= 0 with no solution
Sum := 0;
while Map.Find(Sum) /= No_Element loop
Sum := Sum + 1;
end loop;
Put_Line("Smallest nonnegative impossible sum:" & Integer'Image(Sum));
New_Line;
-- find ten highest numbers with a solution
Current := Map.Last; -- highest element in Map with a solution
Put_Line("Highest sum:" & Integer'Image(Key(Current)));
Put("Next nine:");
for I in 1 .. 9 loop -- 9 steps backward
Previous(Current);
Put(Integer'Image(Key(Current)));
end loop;
New_Line;
end Three_others;