Task: Pascal's triangle_Puzzle


C++: #include <iostream>
#include <iomanip>

inline int sign(int i) {
    return i < 0 ? -1 : i > 0;
}

inline int& E(int *x, int row, int col) {
    return x[row * (row + 1) / 2 + col];
}

int iter(int *v, int *diff) {
    
    E(v, 0, 0) = 151;
    E(v, 2, 0) = 40;
    E(v, 4, 1) = 11;
    E(v, 4, 3) = 4;

    
    for (auto i = 1u; i < 5u; i++)
        for (auto j = 0u; 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 (auto i = 0u; i < 4u; i++)
        for (auto j = 0u; 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);

    
    uint sum;
    int e = 0;
    for (auto i = sum = 0u; i < 15u; i++) {
        sum += !!sign(e = diff[i]);

        
        if (e >= 4 || e <= -4)
            v[i] += e / 5;
        else if (rand() < RAND_MAX / 4)
            v[i] += sign(e);
    }
    return sum;
}

void show(int *x) {
    for (auto i = 0u; i < 5u; i++)
        for (auto j = 0u; j <= i; j++)
            std::cout << std::setw(4u) << *(x++) << (j < i ? ' ' : '\n');
}

int main() {
    int v[15] = { 0 }, diff[15] = { 0 };
    for (auto i = 1u, s = 1u; s; i++) {
        s = iter(v, diff);
        std::cout << "pass " << i << ": " << s << std::endl;
    }
    show(v);

    return 0;
}



Go: package main

import "fmt"


type expr struct {
    x, y, z float64 
    c       float64 
}


func addExpr(a, b expr) expr {
    return expr{a.x + b.x, a.y + b.y, a.z + b.z, a.c + b.c}
}   
    

func subExpr(a, b expr) expr {
    return expr{a.x - b.x, a.y - b.y, a.z - b.z, a.c - b.c}
}   


func mulExpr(a expr, c float64) expr {
    return expr{a.x * c, a.y * c, a.z * c, a.c * c}
}



func addRow(l []expr) []expr {
    if len(l) == 0 {
        panic("wrong")
    }
    r := make([]expr, len(l)-1)
    for i := range r {
        r[i] = addExpr(l[i], l[i+1])
    }
    return r
}   



func substX(a, b expr) expr {
    if b.x == 0 {
        panic("wrong")
    }
    return subExpr(a, mulExpr(b, a.x/b.x))
}

func substY(a, b expr) expr {
    if b.y == 0 {
        panic("wrong")
    }
    return subExpr(a, mulExpr(b, a.y/b.y))
}

func substZ(a, b expr) expr {
    if b.z == 0 {
        panic("wrong")
    }
    return subExpr(a, mulExpr(b, a.z/b.z))
}


func solveX(a expr) float64 {
    if a.x == 0 || a.y != 0 || a.z != 0 {
        panic("wrong")
    }
    return -a.c / a.x
}

func solveY(a expr) float64 {
    if a.x != 0 || a.y == 0 || a.z != 0 {
        panic("wrong")
    }
    return -a.c / a.y
}

func solveZ(a expr) float64 {
    if a.x != 0 || a.y != 0 || a.z == 0 {
        panic("wrong")
    }
    return -a.c / a.z
}

func main() {
    
    r5 := []expr{{x: 1}, {c: 11}, {y: 1}, {c: 4}, {z: 1}}
    fmt.Println("bottom row:", r5)

    
    r4 := addRow(r5)
    fmt.Println("next row up:", r4)
    r3 := addRow(r4)
    fmt.Println("middle row:", r3)

    
    xyz := subExpr(expr{y: 1}, expr{x: 1, z: 1})
    fmt.Println("xyz relation:", xyz)
    
    r3[2] = substZ(r3[2], xyz)
    fmt.Println("middle row after substituting for z:", r3)

    
    b := expr{c: 40}
    
    xy := subExpr(r3[0], b)
    fmt.Println("xy relation:", xy)
    
    r3[0] = b

    
    r3[2] = substX(r3[2], xy)
    fmt.Println("middle row after substituting for x:", r3)
    
    
    r2 := addRow(r3)
    fmt.Println("next row up:", r2)
    r1 := addRow(r2)
    fmt.Println("top row:", r1)

    
    y := subExpr(r1[0], expr{c: 151})
    fmt.Println("y relation:", y)
    
    x := substY(xy, y)
    fmt.Println("x relation:", x)
    
    z := substX(substY(xyz, y), x)
    fmt.Println("z relation:", z)

    
    fmt.Println("x =", solveX(x))
    fmt.Println("y =", solveY(y))
    fmt.Println("z =", solveZ(z)) 
}