Mark Grealish

the life and code of a crazy cat man


I’m not sure what kids consider hip these days, except that it involves concussions

in family

¯\_(ツ)_/¯

Caira melts Garrett's brain

Mount Usher Gardens

in family

Found during a cleanup of photographs from the summer. :)

Eadaoin and I at Mount Usher Gardens in
Wicklow

Project Euler Problem #30

in code

Problem #30 was solved on new ground: my first run through Project Euler was in rigid order of first to last ending at #25. The recent round of solutions mostly cover old ground, which works well for me by itself because it shows off how my code has improved since 2012.

Cookie in a basket

Also, the run of text post has led to a sharp reduction in cute cat photographs, so here’s one of Cookie in a basket.

Problem #30 has two components:

  1. Find the upper bounds for the problem.
  2. Find the sum of appropriate numbers.

So, uh, to paraphrase, possible values for the problem fall into a range with a top end somewhere roundabouts n * 9n, where n is the power you want to raise all the digits:

5 * 9^5 = 354294

After that it is simple and easy to test numbers in the range:

function probableMax(power) {
    return power * Math.pow(9, power);
}

function powerDigits(number, power) {
    return (number).toString().split('').map(digit => {
        return Math.pow(parseInt(digit), power);
    }).reduce((sum, current) => {
        return sum + current;
    });
}

const power = 5;
let sum = 0;

for (let i = 2; i < probableMax(power); i++) {
    if (powerDigits(i, power) === i) {
        sum += i;
    }
}

console.log(sum);

Project Euler Problems #18 and #67

in code

The new solution for Problem #18 (compared to the old one) is more elegant: I reverse and reduce the triangle in a manner that doesn’t require me to check. The solution for #67 is a bit more longform, so I turned the method I use here into an NPM package: Triangutron.

const eighteen = `
    75
    95 64
    17 47 82
    18 35 87 10
    20 04 82 47 65
    19 01 23 75 03 34
    88 02 77 73 07 63 67
    99 65 04 28 06 16 70 92
    41 41 26 56 83 40 80 70 33
    41 48 72 33 47 32 37 16 94 29
    53 71 44 65 25 43 91 52 97 51 14
    70 11 33 28 77 73 17 78 39 68 17 57
    91 71 52 38 17 14 91 43 58 50 27 29 48
    63 66 04 68 89 53 67 30 73 16 69 87 40 31
    04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
`;


function parse(string) {
    return string.trim().split('\n').map(row => {
        return row.trim().split(' ').map(parseFloat);
    }).filter(row => {
        return row.every(Number.isInteger);
    });
}

function max(lastRow, row) {
    return row.map((cell, index) => {
        return cell + Math.max(lastRow[index], lastRow[index + 1]);
    });
}

const triangle = parse(eighteen).reverse();
console.log(triangle.slice(1).reduce(max, triangle[0])[0]);

Project Euler Problem #36

in code

function range(start, end) {
    return [...Array(end + 1).keys()].slice(start);
}

function palindrome(number) {
    return number.toString() === number.toString().split('').reverse().join('');
}

function toBinary(number) {
    return (number >>> 0).toString(2);
}

function add(sum, current) {
    return sum + current;
}

const palindromes = range(1, 999999).filter(number => {
    return palindrome(number) && palindrome(toBinary(number));
});

console.log(palindromes.reduce(add));

Lexicographer

in code

I went back and owned the fuck out of Problem 24. I present to you a fully-functional functional lexicographic permutation generator generator. Hail Satan! \m/

#!/usr/bin/env node

const lexicographer = require('lexicographer');
const array = [...Array(10).keys()];
const breakpoint = 1000000;
let permutations = 0;

for (let value of lexicographer.permute(array)) {
    permutations++;

    if (permutations === breakpoint) {
        console.log(value.join(''));
        break;
    }
}

https://github.com/bhalash/lexicographer
https://www.npmjs.com/package/lexicographic-generator