Pascal's triangle/Puzzle: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Realize in MiniZinc)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 98:
5 11 13 4 8
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Standard - ''lu decomp'' and ''lu solve'' are from the [[ALGOL 68G]]/gsl library}}
Line 543 ⟶ 544:
</pre>
 
=={{header|C#}}==
This solution is based upon algebraic necessities, namely that a solution exists when (top - 4(a+b))/7 is integral. It also highlights the type difference between floating point numbers and integers in C.
 
<lang c>
/* Pascal's pyramid solver
*
* [top]
* [ ] [ ]
* [mid] [ ] [ ]
* [ ] [ ] [ ] [ ]
* [ x ] [ a ] [ y ] [ b ] [ z ]
* x + z = y
*
* This solution makes use of a little bit of mathematical observation,
* such as the fact that top = 4(a+b) + 7(x+z) and mid = 2x + 2a + z.
*/
 
#include <stdio.h>
#include <math.h>
 
void pascal(int a, int b, int mid, int top, int* x, int* y, int* z)
{
double ytemp = (top - 4 * (a + b)) / 7.;
if(fmod(ytemp, 1.) >= 0.0001)
{
x = 0;
return;
}
*y = ytemp;
*x = mid - 2 * a - *y;
*z = *y - *x;
}
int main()
{
int a = 11, b = 4, mid = 40, top = 151;
int x, y, z;
pascal(a, b, mid, top, &x, &y, &z);
if(x != 0)
printf("x: %d, y: %d, z: %d\n", x, y, z);
else printf("No solution\n");
 
return 0;
}
</lang>
{{Out}}
<pre>
x: 5, y: 13, z: 8
</pre>
 
 
 
===Field equation solver===
Treating relations between cells as if they were differential equations, and apply negative feedback to each cell at every iteration step. This is how field equations with boundary conditions are solved numerically. It is, of course, not the optimal solution for this particular task.
<lang c>#include <stdio.h>
#include <stdlib.h>
 
void show(int *x) {
int i, j;
 
for (i = 0; i < 5; i++)
for (j = 0; j <= i; j++)
printf("%4d%c", *(x++), j < i ? ' ' : '\n');
}
 
inline int sign(int i)
{
return i < 0 ? -1 : i > 0;
}
 
int iter(int *v, int *diff) {
int sum, i, j, e = 0;
 
# define E(x, row, col) x[(row) * ((row) + 1) / 2 + (col)]
/* enforce boundary conditions */
E(v, 0, 0) = 151;
E(v, 2, 0) = 40;
E(v, 4, 1) = 11;
E(v, 4, 3) = 4;
 
/* calculate difference from equilibrium */
for (i = 1; i < 5; i++) {
for (j = 0; j <= i; j++) {
E(diff, i, j) = 0;
if (j < i)
E(diff, i, j) += E(v, i - 1, j) -
E(v, i, j + 1) -
E(v, i, j);
if (j)
E(diff, i, j) += E(v, i - 1, j - 1) -
E(v, i, j - 1) -
E(v, i, j);
}
}
 
for (i = 0; i < 4; i++)
for (j = 0; j < i; j++)
E(diff, i, j) += E(v, i + 1, j) +
E(v, i + 1, j + 1) -
E(v, i, j);
 
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
# undef E
 
/* Do feedback, check if we are done. */
for (i = sum = 0; i < 15; i++) {
sum += !!sign(e = diff[i]);
 
/* 1/5-ish feedback strength on average. These numbers are highly
magical, depending on nodes' connectivities. */
if (e >= 4 || e <= -4) v[i] += e/5;
else if (rand() < RAND_MAX/4) v[i] += sign(e);
}
return sum;
}
 
int main() {
int v[15] = { 0 }, diff[15] = { 0 }, i, s;
 
for (i = s = 1; s; i++) {
s = iter(v, diff);
printf("pass %d: %d\n", i, s);
}
show(v);
 
return 0;
}</lang>
{{Out}}<pre>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</pre>
 
=={{header|C sharp|C#}}==
<lang c sharp>
using System;
Line 797 ⟶ 938:
 
</pre>
 
=={{header|C}}==
This solution is based upon algebraic necessities, namely that a solution exists when (top - 4(a+b))/7 is integral. It also highlights the type difference between floating point numbers and integers in C.
 
<lang c>
/* Pascal's pyramid solver
*
* [top]
* [ ] [ ]
* [mid] [ ] [ ]
* [ ] [ ] [ ] [ ]
* [ x ] [ a ] [ y ] [ b ] [ z ]
* x + z = y
*
* This solution makes use of a little bit of mathematical observation,
* such as the fact that top = 4(a+b) + 7(x+z) and mid = 2x + 2a + z.
*/
 
#include <stdio.h>
#include <math.h>
 
void pascal(int a, int b, int mid, int top, int* x, int* y, int* z)
{
double ytemp = (top - 4 * (a + b)) / 7.;
if(fmod(ytemp, 1.) >= 0.0001)
{
x = 0;
return;
}
*y = ytemp;
*x = mid - 2 * a - *y;
*z = *y - *x;
}
int main()
{
int a = 11, b = 4, mid = 40, top = 151;
int x, y, z;
pascal(a, b, mid, top, &x, &y, &z);
if(x != 0)
printf("x: %d, y: %d, z: %d\n", x, y, z);
else printf("No solution\n");
 
return 0;
}
</lang>
{{Out}}
<pre>
x: 5, y: 13, z: 8
</pre>
 
 
 
===Field equation solver===
Treating relations between cells as if they were differential equations, and apply negative feedback to each cell at every iteration step. This is how field equations with boundary conditions are solved numerically. It is, of course, not the optimal solution for this particular task.
<lang c>#include <stdio.h>
#include <stdlib.h>
 
void show(int *x) {
int i, j;
 
for (i = 0; i < 5; i++)
for (j = 0; j <= i; j++)
printf("%4d%c", *(x++), j < i ? ' ' : '\n');
}
 
inline int sign(int i)
{
return i < 0 ? -1 : i > 0;
}
 
int iter(int *v, int *diff) {
int sum, i, j, e = 0;
 
# define E(x, row, col) x[(row) * ((row) + 1) / 2 + (col)]
/* enforce boundary conditions */
E(v, 0, 0) = 151;
E(v, 2, 0) = 40;
E(v, 4, 1) = 11;
E(v, 4, 3) = 4;
 
/* calculate difference from equilibrium */
for (i = 1; i < 5; i++) {
for (j = 0; j <= i; j++) {
E(diff, i, j) = 0;
if (j < i)
E(diff, i, j) += E(v, i - 1, j) -
E(v, i, j + 1) -
E(v, i, j);
if (j)
E(diff, i, j) += E(v, i - 1, j - 1) -
E(v, i, j - 1) -
E(v, i, j);
}
}
 
for (i = 0; i < 4; i++)
for (j = 0; j < i; j++)
E(diff, i, j) += E(v, i + 1, j) +
E(v, i + 1, j + 1) -
E(v, i, j);
 
E(diff, 4, 2) += E(v, 4, 0) + E(v, 4, 4) - E(v, 4, 2);
# undef E
 
/* Do feedback, check if we are done. */
for (i = sum = 0; i < 15; i++) {
sum += !!sign(e = diff[i]);
 
/* 1/5-ish feedback strength on average. These numbers are highly
magical, depending on nodes' connectivities. */
if (e >= 4 || e <= -4) v[i] += e/5;
else if (rand() < RAND_MAX/4) v[i] += sign(e);
}
return sum;
}
 
int main() {
int v[15] = { 0 }, diff[15] = { 0 }, i, s;
 
for (i = s = 1; s; i++) {
s = iter(v, diff);
printf("pass %d: %d\n", i, s);
}
show(v);
 
return 0;
}</lang>
{{Out}}<pre>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</pre>
 
=={{header|Clojure}}==
Line 1,585 ⟶ 1,586:
{x = 5, y = 13, z = 8}
</pre>
 
=={{header|Mathematica}}==
We assign a variable to each block starting on top with a, then on the second row b,c et cetera. k,m, and o are replaced by X, Y, and Z. We can write the following equations:
Line 1,645 ⟶ 1,647:
----------
</pre>
 
=={{header|Nim}}==
Translation of Ada solution:
Line 1,790 ⟶ 1,793:
{Application.exit 0}
end</lang>
 
=={{header|PARI/GP}}==
[ 6y+x+z+4a[2]+4a[4]= 7y +4a[2]+4a[4]]
Line 1,807 ⟶ 1,811:
 
I'm thinking of one to solve all puzzles regardless of size and positions. but the objective was to solve this puzzle.
 
=={{header|Perl}}==
<lang perl># set up triangle
Line 1,857 ⟶ 1,862:
x=5, y=13, z=8
</pre>
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6># set up triangle
my $rows = 5;
my @tri = (1..$rows).map: { [ { x => 0, z => 0, v => 0, rhs => Nil } xx $_ ] }
@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 @tri - 2 ... 0 -> $row {
for 0 ..^ @tri[$row] -> $col {
@tri[$row][$col]{$_} = @tri[$row+1][$col]{$_} + @tri[$row+1][$col+1]{$_} for 'x','z','v';
}
}
 
# find equations
my @eqn = gather for @tri -> $row {
for @$row -> $cell {
take [ $cell<x>, $cell<z>, $cell<rhs> - $cell<v> ] if defined $cell<rhs>;
}
}
 
# print equations
say "Equations:";
say " x + z = y";
for @eqn -> [$x,$z,$y] { say "$x x + $z z = $y" }
 
# solve
my $f = @eqn[0][1] / @eqn[1][1];
@eqn[0][$_] -= $f * @eqn[1][$_] for 0..2;
$f = @eqn[1][0] / @eqn[0][0];
@eqn[1][$_] -= $f * @eqn[0][$_] for 0..2;
 
# print solution
say "Solution:";
my $x = @eqn[0][2] / @eqn[0][0];
my $z = @eqn[1][2] / @eqn[1][1];
my $y = $x + $z;
say "x=$x, y=$y, z=$z";</lang>
{{out}}
<pre>Equations:
x + z = y
7 x + 7 z = 91
2 x + 1 z = 18
Solution:
x=5, y=13, z=8</pre>
 
=={{header|Phix}}==
Line 2,342 ⟶ 2,295:
(Z 8)
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<lang perl6># set up triangle
my $rows = 5;
my @tri = (1..$rows).map: { [ { x => 0, z => 0, v => 0, rhs => Nil } xx $_ ] }
@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 @tri - 2 ... 0 -> $row {
for 0 ..^ @tri[$row] -> $col {
@tri[$row][$col]{$_} = @tri[$row+1][$col]{$_} + @tri[$row+1][$col+1]{$_} for 'x','z','v';
}
}
 
# find equations
my @eqn = gather for @tri -> $row {
for @$row -> $cell {
take [ $cell<x>, $cell<z>, $cell<rhs> - $cell<v> ] if defined $cell<rhs>;
}
}
 
# print equations
say "Equations:";
say " x + z = y";
for @eqn -> [$x,$z,$y] { say "$x x + $z z = $y" }
 
# solve
my $f = @eqn[0][1] / @eqn[1][1];
@eqn[0][$_] -= $f * @eqn[1][$_] for 0..2;
$f = @eqn[1][0] / @eqn[0][0];
@eqn[1][$_] -= $f * @eqn[0][$_] for 0..2;
 
# print solution
say "Solution:";
my $x = @eqn[0][2] / @eqn[0][0];
my $z = @eqn[1][2] / @eqn[1][1];
my $y = $x + $z;
say "x=$x, y=$y, z=$z";</lang>
{{out}}
<pre>Equations:
x + z = y
7 x + 7 z = 91
2 x + 1 z = 18
Solution:
x=5, y=13, z=8</pre>
 
=={{header|REXX}}==
Line 2,495 ⟶ 2,502:
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/UJF14fw/0 (JavaScript)]
or by [https://scastie.scala-lang.org/l0AlwpSdR7i801Fq8CWHEQ Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Perl 6}}
10,327

edits