Project Euler 2 - Even Fibonacci Numbers - Solved with TypeScript

The second Project Euler problem - Even Fibonacci Numbers - is stated as follows - By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

function isEven(n:number): boolean {
    return n % 2 === 0;
}

function *fibonacci(target:number) {
    let current = 1;
    let next = 2;

    while (current < target) {
        yield current;
        [current, next] = [next, current + next];
    }
}

function sum(previous:number, current:number):number {
    return previous + current;
}

function solve(target:number):number {
    // Generate a fibonacci sequence with all
    // numbers below 4 000 000.
    return [...fibonacci(target)]
        // Keep all even numbers.
        .filter(isEven)
        // Sum the remaining numbers.
        .reduce(sum);
}


solve(4000000)

Project Euler 1 - Multiples of 3 or 5 - Solved with TypeScript

The first Project Euler problem - Multiples of 3 and 5 - is stated as follows. Find the sum of all the multiples of 3 or 5 below 1000.

function isMultiple(n:number): boolean {
    return n % 3 === 0 || n % 5 === 0;
}

function sum(previous: number, current: number): number {
    return previous + current;
}

function range(size:number):number[] {
    return [...Array.from({length: size})]
        .map((_:any, index:number):number => index + 1);
}

function solve(target: number): number {
    // Create a range between 1 and 999.
    return range(target)
        // Keep all multiples of 3 or 5.
        .filter(isMultiple)
        // Sum the remaining numbers.
        .reduce(sum);
}

solve(999);

Multiplication Table Based Mandalas Generator

Inspired by Mathologer’s video Times Tables, Mandelbrot and the Heart of Mathematics I have done a lot of mandala-like drawings on my Axidraw A3/V3 plotter.

Almost all my pieces are generated using the Javascript tool below I created. The source code is on Github.

Play around with the sliders or try these configurations Wave, Lighthouse, Sunrise, Lotus Flower, Ring, Overlapping Cardioids, Flower, Flower II. Reset.

An Interactive Implementation of the Fire Effect in Doom

The fire effect in the Doom intro recently got renewed attention due to Fabien Sanglard’s excellent book Game Engine Black Book: DOOM and blog post How Doom Fire was Done.

After looking through a couple of implementations, and realizing that the effect is achieved using very little code I decided create my own.

The original algorithm can be used to create a lot of things if you feed it with different variables and have a bit of imagination.

Play around with the sliders or try these configurations Umbrella, Rain, Fire, Mona Lisa, Fireball, Beam me up, Matrix

The mask is a 256x256 gif image where bright pixels marks the source(s) of the effect. The fire, has just a line in the bottom and the rain is just a line in the top.

The source code is on Github.

Seven Segment Display - Riddle by FiveThirtyEight - Solved with JavaScript

The FiveThirtyEight riddle Tyler Barron’s Seven Segment Display is stated as follows.

Given a two-character, seven-segment display, like you might find on a microwave clock, how many numbers can you make that are not ambiguous if the display happens to be upside down?

For example, the number 81 on that display would not fit this criterion — if the display were upside down it’d appear like 18. The number 71, however, would be OK. It’d appear something like 1L — not a number.

// Rotates a single seven-segment display digit 180 degrees.
// Non number results are converted to NaN.. eg. 2 --> 2, 6 --> 9, 3 --> NaN
const rotateDigit = n => [0, 1, 2, NaN, NaN, 5, 9, NaN, 8, 6][n];

// Rotates a seven-segment display number 180 degrees.
// eg. 10 --> 01, 81 --> 18, 71 --> 1NaN.
const rotateNumber = n =>
    // Convert the number to a string.
    n.toString()
    // Split the string on every character.
    .split('')
    // Reverse the order of the characters
    .reverse()
    // Rotate the digits.
    .map(rotateDigit)
    // Convert the array back to a string.
    .join('');


const numbers =
    // Generate a range from 0 to 99.
    [...Array(100).keys()]
    // Rotate every number.
    .map(rotateNumber)
    // Filter every number that is not a number. :)
    .filter(isNaN)

const result = numbers.length;

Archive