September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,13 +0,0 @@
|
|||
pass 1: 12
|
||||
pass 2: 12
|
||||
pass 3: 14
|
||||
pass 4: 14
|
||||
...
|
||||
pass 113: 4
|
||||
pass 114: 7
|
||||
pass 115: 0
|
||||
151
|
||||
81 70
|
||||
40 41 29
|
||||
16 24 17 12
|
||||
5 11 13 4 8
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.3
|
||||
|
||||
data class Solution(val x: Int, val y: Int, val z: Int)
|
||||
|
||||
fun Double.isIntegral(tolerance: Double = 0.0) =
|
||||
(this - Math.floor(this)) <= tolerance || (Math.ceil(this) - this) <= tolerance
|
||||
|
||||
fun pascal(a: Int, b: Int, mid: Int, top: Int): Solution {
|
||||
val yd = (top - 4 * (a + b)) / 7.0
|
||||
if (!yd.isIntegral(0.0001)) return Solution(0, 0, 0)
|
||||
val y = yd.toInt()
|
||||
val x = mid - 2 * a - y
|
||||
return Solution(x, y, y - x)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val (x, y, z) = pascal(11, 4, 40, 151)
|
||||
if (x != 0)
|
||||
println("Solution is: x = $x, y = $y, z = $z")
|
||||
else
|
||||
println("There is no solutuon")
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
import math, strutils
|
||||
|
||||
var B_X, B_Y, B_Z : int = 0
|
||||
|
||||
type
|
||||
Block_Value = object
|
||||
Known : int
|
||||
X, Y, Z : int
|
||||
|
||||
let
|
||||
X: Block_Value = Block_Value(Known:0, X:1, Y:0, Z:0)
|
||||
Y: Block_Value = Block_Value(Known:0, X:0, Y:1, Z:0)
|
||||
Z: Block_Value = Block_Value(Known:0, X:0, Y:0, Z:1)
|
||||
|
||||
proc Add (L : var Block_Value, R : Block_Value) =
|
||||
# Symbolically adds one block to another
|
||||
L.Known = L.Known + R.Known
|
||||
L.X = L.X + R.X - R.Z # Z is excluded as n(Y - X - Z) = 0
|
||||
L.Y = L.Y + R.Y + R.Z
|
||||
|
||||
proc Add (L: var Block_Value, R: int) =
|
||||
# Symbolically adds a value to the block
|
||||
L.Known = L.Known + R
|
||||
|
||||
proc Image (N : Block_Value): string =
|
||||
# The block value, when X,Y,Z are known
|
||||
result = $(N.Known + N.X * B_X + N.Y * B_Y + N.Z * B_Z)
|
||||
|
||||
proc Solve_2x2 (A11: int, A12:int, B1:int, A21:int, A22:int, B2: int) =
|
||||
# Don't care about things, supposing an integer solution exists
|
||||
if A22 == 0:
|
||||
B_X = toInt(B2 / A21)
|
||||
B_Y = toInt((B1 - (A11*B_X)) / A12)
|
||||
else:
|
||||
B_X = toInt((B1*A22 - B2*A12) / (A11*A22 - A21*A12))
|
||||
B_Y = toInt((B1 - A11*B_X) / A12)
|
||||
B_Z = B_Y - B_X
|
||||
|
||||
var B : array [1..5, array[1..5, Block_Value]] # The lower triangle contains blocks
|
||||
|
||||
# The bottom blocks
|
||||
Add(B[5][1],X)
|
||||
Add(B[5][2],11)
|
||||
Add(B[5][3],Y)
|
||||
Add(B[5][4],4)
|
||||
Add(B[5][5],Z)
|
||||
|
||||
# Upward run
|
||||
for Row in countdown(4,1):
|
||||
for Column in 1 .. Row:
|
||||
Add (B[Row][Column], B[Row + 1][Column])
|
||||
Add (B[Row][Column], B[Row + 1][Column + 1])
|
||||
|
||||
# Now have known blocks 40=[3][1], 151=[1][1] and Y=X+Z to determine X,Y,Z
|
||||
Solve_2x2( B[1][1].X,
|
||||
B[1][1].Y,
|
||||
151 - B[1][1].Known,
|
||||
B[3][1].X,
|
||||
B[3][1].Y,
|
||||
40 - B[3][1].Known)
|
||||
|
||||
#Print the results
|
||||
for Row in 1..5:
|
||||
writeln(stdout,"")
|
||||
for Column in 1..Row:
|
||||
write(stdout, Image(B[Row][Column]), " ")
|
||||
|
|
@ -1 +0,0 @@
|
|||
[5, 13, 8]
|
||||
|
|
@ -1,24 +1,24 @@
|
|||
# set up triangle
|
||||
var rows = 5;
|
||||
var tri = rows.of {|i| i.of { Hash(x => 0, z => 0, v => 0, rhs => nil) } }
|
||||
tri[0][0]{:rhs} = 151;
|
||||
tri[2][0]{:rhs} = 40;
|
||||
tri[4][0]{:x} = 1;
|
||||
tri[4][1]{:v} = 11;
|
||||
tri[4][2]{:x} = 1;
|
||||
tri[4][2]{:z} = 1;
|
||||
tri[4][3]{:v} = 4;
|
||||
tri[4][4]{:z} = 1;
|
||||
|
||||
var rows = 5
|
||||
var tri = rows.of {|i| (i+1).of { Hash(x => 0, z => 0, v => 0, rhs => nil) } }
|
||||
tri[0][0]{:rhs} = 151
|
||||
tri[2][0]{:rhs} = 40
|
||||
tri[4][0]{:x} = 1
|
||||
tri[4][1]{:v} = 11
|
||||
tri[4][2]{:x} = 1
|
||||
tri[4][2]{:z} = 1
|
||||
tri[4][3]{:v} = 4
|
||||
tri[4][4]{:z} = 1
|
||||
|
||||
# aggregate from bottom to top
|
||||
for row in (tri.end -> downto(1)) {
|
||||
for col in (tri[row-1].range) {
|
||||
for row in (tri.len ^.. 1) {
|
||||
for col in (^tri[row-1]) {
|
||||
[:x, :z, :v].each { |key|
|
||||
tri[row-1][col]{key} = (tri[row][col]{key} + tri[row][col+1]{key})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# find equations
|
||||
var eqn = gather {
|
||||
for r in tri {
|
||||
|
|
@ -27,21 +27,21 @@ var eqn = gather {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# print equations
|
||||
say "Equations:";
|
||||
say " x + z = y";
|
||||
say "Equations:"
|
||||
say " x + z = y"
|
||||
for x,z,y in eqn { say "#{x}x + #{z}z = #{y}" }
|
||||
|
||||
|
||||
# solve
|
||||
var f = (eqn[0][1] / eqn[1][1]);
|
||||
for i in (0..2) { eqn[0][i] -= (f * eqn[1][i]) }
|
||||
f = (eqn[1][0] / eqn[0][0]);
|
||||
for i in (0..2) { eqn[1][i] -= (f * eqn[0][i]) }
|
||||
|
||||
var f = (eqn[0][1] / eqn[1][1])
|
||||
{|i| eqn[0][i] -= (f * eqn[1][i]) } << ^3
|
||||
f = (eqn[1][0] / eqn[0][0])
|
||||
{|i| eqn[1][i] -= (f * eqn[0][i]) } << ^3
|
||||
|
||||
# print solution
|
||||
say "Solution:";
|
||||
var x = (eqn[0][2] / eqn[0][0]);
|
||||
var z = (eqn[1][2] / eqn[1][1]);
|
||||
var y = (x + z);
|
||||
say "x=#{x}, y=#{y}, z=#{z}";
|
||||
say "Solution:"
|
||||
var x = (eqn[0][2] / eqn[0][0])
|
||||
var z = (eqn[1][2] / eqn[1][1])
|
||||
var y = (x + z)
|
||||
say "x=#{x}, y=#{y}, z=#{z}"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
# Pyramid solver
|
||||
# [151]
|
||||
# [ ] [ ]
|
||||
# [ 40] [ ] [ ]
|
||||
# [ ] [ ] [ ] [ ]
|
||||
#[ X ] [ 11] [ Y ] [ 4 ] [ Z ]
|
||||
# Known: X - Y + Z = 0
|
||||
|
||||
p:=T( L(151), L(Void,Void), L(40,Void,Void), L(Void,Void,Void,Void),
|
||||
L("X", 11, "Y", 4, "Z") );
|
||||
addlConstraint:=Dictionary( "X",1, "Y",-1, "Z",1, "1",0 );
|
||||
solvePyramid(p, addlConstraint);
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
fcn solvePyramid([List]vl,[Dictionary]cnstr){ //ListOfLists,Hash-->zip
|
||||
vl=vl.reverse();
|
||||
constraints:=L(cnstr);
|
||||
lvls:=vl.len();
|
||||
foreach lvln in ([1..lvls-1]){
|
||||
lvd:=vl[lvln];
|
||||
foreach k in (lvls-lvln){
|
||||
sn:=lvd[k];
|
||||
ll:=vl[lvln-1];
|
||||
vn:=combine(ll[k], ll[k+1]);
|
||||
if(Void==sn) lvd[k]=vn;
|
||||
else constraints.append(constrainK(sn,vn));
|
||||
}
|
||||
}
|
||||
println("Constraint Equations:");
|
||||
constraints.pump(Console.println,fcn(hash){
|
||||
hash.pump(List,fcn([(k,v)]){"%d*%s".fmt(v,k)}).concat(" + ") + " = 0"
|
||||
});
|
||||
|
||||
mtx,vmap:=makeMatrix(constraints);
|
||||
mtxSolve(mtx);
|
||||
|
||||
d:=vmap.len();
|
||||
foreach j in (d){ println(vmap[j]," = ", mtx[j][d]); }
|
||||
}
|
||||
|
||||
fcn [mixin=Dictionary] constrainK([Int]nsum,[Dictionary]vn){ //-->new hash of old hash, sum K
|
||||
nn:=vn.copy(); nn["1"]=nn.find("1",0) - nsum;
|
||||
return(nn.makeReadOnly());
|
||||
}
|
||||
|
||||
fcn combine(snl,snr){ //Int|String|Hash *2 --> new Hash
|
||||
cl:=Dictionary();
|
||||
if(snl.isInstanceOf(Int)) cl["1"]=snl;
|
||||
else if(snl.isInstanceOf(String)) cl[snl]=1;
|
||||
else cl =snl.copy();
|
||||
|
||||
if(snr.isInstanceOf(Int)) cl["1"]=cl.find("1",0) + snr;
|
||||
else if(snr.isInstanceOf(String)) cl[snr]=cl.find(snr,0) + 1;
|
||||
else{ foreach k,v in (snr){ cl[k] =cl.find(k,0) + v; } }
|
||||
return(cl.makeReadOnly())
|
||||
}
|
||||
|
||||
//-->(listMatrix(row(X,Y,Z,c),row...),List("X","Y","Z"))
|
||||
fcn makeMatrix([Dictionary]constraints){
|
||||
vmap:=Dictionary();// create a sorted list of the variable names in constraints
|
||||
foreach c in (constraints){ vmap.extend(c) } // no duplicate names
|
||||
vmap.del("1"); vmap=vmap.keys.sort(); # sort here so output is in sorted order
|
||||
|
||||
mtx:=constraints.pump(List,'wrap(c){ // create list of [writeable] rows
|
||||
vmap.pump(List, c.find.fp1(0),"toFloat").copy()
|
||||
.append(-c.find("1",0).toFloat())
|
||||
}).copy();
|
||||
|
||||
nvars:=vmap.len();
|
||||
if(constraints.len()==nvars) println("System appears solvable");
|
||||
else if(constraints.len()<nvars)
|
||||
println("System is not solvable - needs more constraints.");
|
||||
return(mtx,vmap);
|
||||
}
|
||||
|
||||
fcn mtxSolve([List]mtx){ //munge mtx # Simple Matrix solver...
|
||||
mDim:=mtx.len(); # num rows
|
||||
foreach j in (mDim){
|
||||
rw0:=mtx[j];
|
||||
f:=1.0/rw0[j];
|
||||
foreach k in ([j..mDim]){ rw0[k]=rw0[k]*f }
|
||||
foreach l in ([j+1..mDim-1]){
|
||||
rwl:=mtx[l]; f:=-rwl[j];
|
||||
foreach k in ([j..mDim]){ rwl[k]+=f*rw0[k] }
|
||||
}
|
||||
}
|
||||
# backsolve part ---
|
||||
foreach j1 in ([1..mDim-1]){
|
||||
j:=mDim - j1; rw0:=mtx[j];
|
||||
foreach l in (j){
|
||||
rwl:=mtx[l]; f:=-rwl[j];
|
||||
rwl[j] +=f*rw0[j];
|
||||
rwl[mDim]+=f*rw0[mDim];
|
||||
}
|
||||
}
|
||||
return(mtx);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue