Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -2,29 +2,18 @@ BEGIN
|
|||
# find the numbers the string 123456789 ( with "+/-" optionally inserted #
|
||||
# before each digit ) can generate #
|
||||
|
||||
# experimentation shows that the largest hundred numbers that can be #
|
||||
# generated are are greater than or equal to 56795 #
|
||||
# as we can't declare an array with bounds -123456789 : 123456789 in #
|
||||
# Algol 68G, we use -60000 : 60000 and keep counts for the top hundred #
|
||||
PR read "sort.incl.a68" PR # include sorting utilities #
|
||||
|
||||
INT max number = 60 000;
|
||||
[ - max number : max number ]STRING solutions;
|
||||
[ - max number : max number ]INT count;
|
||||
FOR i FROM LWB solutions TO UPB solutions DO solutions[ i ] := ""; count[ i ] := 0 OD;
|
||||
# calculate the numbers we can generate and the strings leading to them #
|
||||
# as noted by other solutions, at most 13122 (2*3^8) can be generated #
|
||||
|
||||
# calculate the numbers ( up to max number ) we can generate and the strings leading to them #
|
||||
# also determine the largest numbers we can generate #
|
||||
[ 100 ]INT largest;
|
||||
[ 100 ]INT largest count;
|
||||
INT impossible number = - 999 999 999;
|
||||
FOR i FROM LWB largest TO UPB largest DO
|
||||
largest [ i ] := impossible number;
|
||||
largest count[ i ] := 0
|
||||
OD;
|
||||
INT max expr = 2 * 3^8;
|
||||
[ 1 : max expr ]INT value;
|
||||
[ 1 : max expr ]STRING expression;
|
||||
INT expr pos := 0;
|
||||
[ 1 : 18 ]CHAR sum string := ".1.2.3.4.5.6.7.8.9";
|
||||
[]CHAR sign char = []CHAR( "-", " ", "+" )[ AT -1 ];
|
||||
# we don't distinguish between strings starting "+1" and starting " 1" #
|
||||
FOR s1 FROM -1 TO 0 DO
|
||||
FOR s1 FROM -1 TO 0 DO # we don't distinguish between strings starting "+1" and starting " 1" #
|
||||
sum string[ 1 ] := sign char[ s1 ];
|
||||
FOR s2 FROM -1 TO 1 DO
|
||||
sum string[ 3 ] := sign char[ s2 ];
|
||||
|
|
@ -52,28 +41,10 @@ BEGIN
|
|||
IF s7 = 0 THEN part *:= 10 +:= 7 * SIGN part ELSE number +:= part; part := 7 * s7 FI;
|
||||
IF s8 = 0 THEN part *:= 10 +:= 8 * SIGN part ELSE number +:= part; part := 8 * s8 FI;
|
||||
IF s9 = 0 THEN part *:= 10 +:= 9 * SIGN part ELSE number +:= part; part := 9 * s9 FI;
|
||||
number +:= part;
|
||||
IF number >= LWB solutions AND number <= UPB solutions THEN
|
||||
solutions[ number ] +:= ";" + sum string;
|
||||
count [ number ] +:= 1
|
||||
FI;
|
||||
BOOL inserted := FALSE;
|
||||
FOR l pos FROM LWB largest TO UPB largest WHILE NOT inserted DO
|
||||
IF number > largest[ l pos ] THEN
|
||||
# found a new larger number #
|
||||
FOR m pos FROM UPB largest BY -1 TO l pos + 1 DO
|
||||
largest [ m pos ] := largest [ m pos - 1 ];
|
||||
largest count[ m pos ] := largest count[ m pos - 1 ]
|
||||
OD;
|
||||
largest [ l pos ] := number;
|
||||
largest count[ l pos ] := 1;
|
||||
inserted := TRUE
|
||||
ELIF number = largest[ l pos ] THEN
|
||||
# have another way of generating this number #
|
||||
largest count[ l pos ] +:= 1;
|
||||
inserted := TRUE
|
||||
FI
|
||||
OD
|
||||
number +:= part;
|
||||
expr pos +:= 1;
|
||||
expression[ expr pos ] := sum string;
|
||||
value[ expr pos ] := number
|
||||
OD
|
||||
OD
|
||||
OD
|
||||
|
|
@ -85,45 +56,66 @@ BEGIN
|
|||
OD;
|
||||
|
||||
# show the solutions for 100 #
|
||||
print( ( "100 has ", whole( count[ 100 ], 0 ), " solutions:" ) );
|
||||
STRING s := solutions[ 100 ];
|
||||
FOR s pos FROM LWB s TO UPB s DO
|
||||
IF s[ s pos ] = ";" THEN print( ( newline, " " ) )
|
||||
ELIF s[ s pos ] /= " " THEN print( ( s[ s pos ] ) )
|
||||
INT count 100 := 0;
|
||||
FOR x pos FROM LWB value TO UPB value DO
|
||||
IF value[ x pos ] = 100 THEN count 100 +:= 1 FI
|
||||
OD;
|
||||
print( ( "100 has ", whole( count 100, 0 ), " solutions:", newline ) );
|
||||
FOR x pos FROM LWB value TO UPB value DO
|
||||
IF value[ x pos ] = 100 THEN
|
||||
print( ( " " ) );
|
||||
STRING s = expression[ x pos ];
|
||||
FOR s pos FROM LWB s TO UPB s DO IF s[ s pos ] /= " " THEN print( ( s[ s pos ] ) ) FI OD;
|
||||
print( ( newline ) )
|
||||
FI
|
||||
OD;
|
||||
print( ( newline ) );
|
||||
|
||||
QUICKSORT value; # sort the values (the expressions will now be out of order) #
|
||||
|
||||
# find the number with the most solutions #
|
||||
INT max solutions := 0;
|
||||
INT number with max := LWB count - 1;
|
||||
FOR n FROM 0 TO max number DO
|
||||
IF count[ n ] > max solutions THEN
|
||||
max solutions := count[ n ];
|
||||
number with max := n
|
||||
INT this count := 1, max count := 1, max count value := value[ LWB value ];
|
||||
FOR v pos FROM LWB value + 1 TO UPB value DO
|
||||
IF value[ v pos ] = value[ v pos - 1 ] THEN
|
||||
this count +:= 1
|
||||
ELSE
|
||||
IF this count > max count AND value[ v pos - 1 ] >= 0 THEN
|
||||
max count := this count;
|
||||
max count value := value[ v pos - 1 ]
|
||||
FI;
|
||||
this count := 1
|
||||
FI
|
||||
OD;
|
||||
FOR n FROM LWB largest count TO UPB largest count DO
|
||||
IF largest count[ n ] > max solutions THEN
|
||||
max solutions := largest count[ n ];
|
||||
number with max := largest[ n ]
|
||||
FI
|
||||
OD;
|
||||
print( ( whole( number with max, 0 ), " has the maximum number of solutions: " ) );
|
||||
print( ( whole( max solutions, 0 ), newline ) );
|
||||
# find the smallest positive number that has no solutions #
|
||||
BOOL have solutions := TRUE;
|
||||
FOR n FROM 0 TO max number
|
||||
WHILE IF NOT ( have solutions := count[ n ] > 0 )
|
||||
THEN print( ( whole( n, 0 ), " is the lowest positive number with no solutions", newline ) )
|
||||
FI;
|
||||
have solutions
|
||||
DO SKIP OD;
|
||||
IF have solutions
|
||||
THEN print( ( "All positive numbers up to ", whole( max number, 0 ), " have solutions", newline ) )
|
||||
IF this count > max count THEN
|
||||
# the final value has the maximum count #
|
||||
max count := this count;
|
||||
max count value := value[ UPB value ]
|
||||
FI;
|
||||
print( ( whole( max count value, 0 ), " has the maximum number of solutions: " ) );
|
||||
print( ( whole( max count, 0 ), newline ) );
|
||||
|
||||
# find the smallest positive number that has no solutions #
|
||||
BOOL have solution := TRUE;
|
||||
INT missing value := 1;
|
||||
FOR v pos FROM LWB value TO UPB value WHILE have solution DO
|
||||
IF value[ v pos ] > 0 THEN
|
||||
IF value[ v pos ] /= value[ v pos - 1 ] THEN
|
||||
have solution := value[ v pos ] = missing value;
|
||||
IF have solution THEN
|
||||
missing value +:= 1
|
||||
FI
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
print( ( whole( missing value, 0 ), " is the lowest positive number with no solutions", newline ) );
|
||||
|
||||
# show the ten largest numbers that can be generated #
|
||||
print( ( "The 10 largest numbers that can be generated are:", newline ) );
|
||||
FOR t pos FROM 1 TO 10 DO
|
||||
print( ( " ", whole( largest[ t pos ], 0 ) ) )
|
||||
INT shown := 0;
|
||||
FOR v pos FROM UPB value - 1 BY -1 TO LWB value WHILE shown < 10 DO
|
||||
IF value[ v pos ] /= value[ v pos + 1 ] THEN
|
||||
print( ( " ", whole( value[ v pos + 1 ], 0 ) ) );
|
||||
shown +:= 1
|
||||
FI
|
||||
OD;
|
||||
print( ( newline ) )
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
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;
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
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;
|
||||
|
|
@ -29,7 +29,6 @@ void main() {
|
|||
writeln(value);
|
||||
|
||||
comment("Show the ten highest numbers that can be expressed");
|
||||
const int n = stat.countSum.keys.length;
|
||||
auto sums = stat.countSum.keys;
|
||||
sums.sort!"a>b"
|
||||
.take(10)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
ADD = 0
|
||||
SUB = 1
|
||||
nexpr = 13122 - 1
|
||||
len f[] nexpr + 1
|
||||
arrbase f[] 0
|
||||
#
|
||||
func evaluate code .
|
||||
fastfunc evaluate code .
|
||||
power = 1
|
||||
for k = 9 downto 1
|
||||
numb += power * k
|
||||
|
|
@ -24,21 +20,21 @@ func evaluate code .
|
|||
.
|
||||
return value
|
||||
.
|
||||
proc init .
|
||||
nexpr = 13122 - 1
|
||||
len f[] nexpr + 1
|
||||
arrbase f[] 0
|
||||
fastproc .
|
||||
for i = 0 to nexpr
|
||||
f[i] = evaluate i
|
||||
.
|
||||
.
|
||||
init
|
||||
proc out code .
|
||||
a = 19683
|
||||
b = 6561
|
||||
for k = 1 to 9
|
||||
h = code mod a div b
|
||||
if h = ADD
|
||||
if k > 1
|
||||
s$ &= "+"
|
||||
.
|
||||
if k > 1 : s$ &= "+"
|
||||
elif h = SUB
|
||||
s$ &= "-"
|
||||
.
|
||||
|
|
@ -48,55 +44,55 @@ proc out code .
|
|||
.
|
||||
print evaluate code & " = " & s$
|
||||
.
|
||||
#
|
||||
print "Show all solutions that sum to 100\n"
|
||||
for i = 0 to nexpr
|
||||
if f[i] = 100
|
||||
out i
|
||||
.
|
||||
proc .
|
||||
for i = 0 to nexpr : if f[i] = 100 : out i
|
||||
.
|
||||
#
|
||||
print "\nShow the sum that has the maximum number of solutions\n"
|
||||
for i = 0 to nexpr
|
||||
test = f[i]
|
||||
if test > 0
|
||||
ntest = 0
|
||||
for j = 0 to nexpr
|
||||
if f[j] = test
|
||||
ntest += 1
|
||||
global best nbest .
|
||||
fastproc .
|
||||
for i = 0 to nexpr
|
||||
test = f[i]
|
||||
if test > 0
|
||||
ntest = 0
|
||||
for j = 0 to nexpr
|
||||
if f[j] = test : ntest += 1
|
||||
.
|
||||
if ntest > nbest
|
||||
best = test
|
||||
nbest = ntest
|
||||
.
|
||||
.
|
||||
if ntest > nbest
|
||||
best = test
|
||||
nbest = ntest
|
||||
.
|
||||
.
|
||||
.
|
||||
print best & " has " & nbest & " solutions"
|
||||
#
|
||||
print "\nShow the lowest positive number that can't be expressed\n"
|
||||
for i = 0 to 123456789
|
||||
for j = 0 to nexpr
|
||||
if i = f[j]
|
||||
break 1
|
||||
proc .
|
||||
for i = 0 to 123456789
|
||||
for j = 0 to nexpr
|
||||
if i = f[j] : break 1
|
||||
.
|
||||
if j > nexpr : break 1
|
||||
.
|
||||
if j > nexpr
|
||||
break 1
|
||||
.
|
||||
print i
|
||||
.
|
||||
print i
|
||||
print "\nShow the ten highest numbers that can be expressed\n"
|
||||
limit = 123456789 + 1
|
||||
for i = 1 to 10
|
||||
best = 0
|
||||
for j = 0 to nexpr
|
||||
test = f[j]
|
||||
if test < limit and test > best
|
||||
best = test
|
||||
proc .
|
||||
limit = 123456789 + 1
|
||||
for i = 1 to 10
|
||||
best = 0
|
||||
for j = 0 to nexpr
|
||||
test = f[j]
|
||||
if test < limit and test > best
|
||||
best = test
|
||||
.
|
||||
.
|
||||
.
|
||||
for j = 0 to nexpr
|
||||
if f[j] = best
|
||||
out j
|
||||
for j = 0 to nexpr
|
||||
if f[j] = best : out j
|
||||
.
|
||||
limit = best
|
||||
.
|
||||
limit = best
|
||||
.
|
||||
|
|
|
|||
|
|
@ -1,21 +1,22 @@
|
|||
defmodule Sum do
|
||||
def to(val) do
|
||||
generate
|
||||
generate()
|
||||
|> Enum.map(&{eval(&1), &1})
|
||||
|> Enum.filter(fn {v, _s} -> v==val end)
|
||||
|> Enum.each(&IO.inspect &1)
|
||||
end
|
||||
|
||||
def max_solve do
|
||||
generate
|
||||
generate()
|
||||
|> Enum.group_by(&eval &1)
|
||||
|> Enum.filter_map(fn {k,_} -> k>=0 end, fn {k,v} -> {length(v),k} end)
|
||||
|> Enum.filter(fn {k,_} -> k>=0 end)
|
||||
|> Enum.map(fn {k,v} -> {length(v),k} end)
|
||||
|> Enum.max
|
||||
|> fn {len,sum} -> IO.puts "sum of #{sum} has the maximum number of solutions : #{len}" end.()
|
||||
end
|
||||
|
||||
def min_solve do
|
||||
solve = generate |> Enum.group_by(&eval &1)
|
||||
solve = generate() |> Enum.group_by(&eval &1)
|
||||
Stream.iterate(1, &(&1+1))
|
||||
|> Enum.find(fn n -> solve[n]==nil end)
|
||||
|> fn sum -> IO.puts "lowest positive sum that can't be expressed : #{sum}" end.()
|
||||
|
|
@ -23,7 +24,7 @@ defmodule Sum do
|
|||
|
||||
def highest_sums(n\\10) do
|
||||
IO.puts "highest sums :"
|
||||
generate
|
||||
generate()
|
||||
|> Enum.map(&eval &1)
|
||||
|> Enum.uniq
|
||||
|> Enum.sort_by(fn sum -> -sum end)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
//
|
||||
// Using FutureBasic 7.0.34, July 2025 R.W.
|
||||
//
|
||||
// Sum to 100 solutions
|
||||
//
|
||||
// Using 123456789 digits so the resulting
|
||||
|
|
|
|||
|
|
@ -1,86 +1,81 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #000000;">javascript_semantics</span>
|
||||
<span style="color: #008080;">enum</span> <span style="color: #000000;">SUB</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">NOP</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ADD</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
|
||||
with javascript_semantics
|
||||
enum SUB=-1, NOP=0, ADD=1
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">evaluate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ADD</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: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">NOP</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmp</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: #008080;">else</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">op</span><span style="color: #0000FF;">*</span><span style="color: #000000;">tmp</span>
|
||||
<span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">op</span><span style="color: #0000FF;">*</span><span style="color: #000000;">tmp</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function evaluate(sequence s)
|
||||
integer res = 0, tmp = 0, op = ADD
|
||||
for i=1 to length(s) do
|
||||
if s[i]=NOP then
|
||||
tmp = tmp*10+i
|
||||
else
|
||||
res += op*tmp
|
||||
tmp = i
|
||||
op = s[i]
|
||||
end if
|
||||
end for
|
||||
return res + op*tmp
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</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: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">NOP</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">','</span><span style="color: #0000FF;">-</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"%s = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">evaluate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
procedure show(sequence s)
|
||||
string res = ""
|
||||
for i=1 to length(s) do
|
||||
if s[i]!=NOP then
|
||||
res &= ','-s[i]
|
||||
end if
|
||||
res &= '0'+i
|
||||
end for
|
||||
printf(1,"%s = %d\n",{res,evaluate(s)})
|
||||
end procedure
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Logically this intersperses -/nop/+ between each digit, but you do not actually need the digit.</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">SUB</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (==> ..nop+add*8)</span>
|
||||
-- Logically this intersperses -/nop/+ between each digit, but you do not actually need the digit.
|
||||
sequence s = repeat(SUB,9) -- (==> ..nop+add*8)
|
||||
|
||||
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">maxl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxr</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">evaluate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">solns</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?{}:</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)))</span>
|
||||
<span style="color: #000000;">solns</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solns</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solns</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">maxl</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">maxl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solns</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">maxr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">NOP</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
|
||||
<span style="color: #008080;">exit</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">ADD</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">exit</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">SUB</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
bool done = false
|
||||
integer maxl = 0, maxr, count = 0
|
||||
while not done do
|
||||
count += 1
|
||||
integer r = evaluate(s), k = getd_index(r)
|
||||
sequence solns = iff(k=0?{}:deep_copy(getd_by_index(k)))
|
||||
solns = append(solns,deep_copy(s))
|
||||
setd(r,solns)
|
||||
if r>0 and maxl<length(solns) then
|
||||
maxl = length(solns)
|
||||
maxr = r
|
||||
end if
|
||||
for i=length(s) to 1 by -1 do
|
||||
if i=1 and s[i]=NOP then
|
||||
done = true
|
||||
exit
|
||||
elsif s[i]!=ADD then
|
||||
s[i] += 1
|
||||
exit
|
||||
end if
|
||||
s[i] = SUB
|
||||
end for
|
||||
end while
|
||||
|
||||
<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;">"%d solutions considered (dictionary size: %d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">dict_size</span><span style="color: #0000FF;">()})</span>
|
||||
printf(1,"%d solutions considered (dictionary size: %d)\n",{count,dict_size()})
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s100</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</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;">"There are %d sums to 100:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s100</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
|
||||
sequence s100 = getd(100)
|
||||
printf(1,"There are %d sums to 100:\n",{length(s100)})
|
||||
papply(s100,show)
|
||||
|
||||
<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;">"The positive sum of %d has the maximum number of solutions: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">maxr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxl</span><span style="color: #0000FF;">})</span>
|
||||
printf(1,"The positive sum of %d has the maximum number of solutions: %d\n",{maxr,maxl})
|
||||
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">missing</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000080;font-style:italic;">/*data*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*pkey*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000080;font-style:italic;">/*user_data=-2*/</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">key</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #7060A8;">traverse_dict_partial_key</span><span style="color: #0000FF;">(</span><span style="color: #000000;">missing</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;">"The lowest positive sum that cannot be expressed: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
|
||||
integer prev = 0
|
||||
function missing(integer key, sequence /*data*/, integer /*pkey*/, object /*user_data=-2*/)
|
||||
if key!=prev+1 then
|
||||
return 0
|
||||
end if
|
||||
prev = key
|
||||
return 1
|
||||
end function
|
||||
traverse_dict_partial_key(missing,1)
|
||||
printf(1,"The lowest positive sum that cannot be expressed: %d\n",{prev+1})
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">highest</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">top10</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000080;font-style:italic;">/*data*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000080;font-style:italic;">/*user_data*/</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">highest</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">key</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">highest</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">10</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #000080;font-style:italic;">--DEV named params on builtins need pre-loading...:
|
||||
--traverse_dict(top10,rev:=1)</span>
|
||||
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">top10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</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;">"The 10 highest sums: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">highest</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
sequence highest = {}
|
||||
function top10(integer key, sequence /*data*/, object /*user_data*/)
|
||||
highest &= key
|
||||
return length(highest)<10
|
||||
end function
|
||||
traverse_dict(top10,rev:=1)
|
||||
printf(1,"The 10 highest sums: %v\n",{highest})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue