Skip to content

Instantly share code, notes, and snippets.

@migreva
Created August 30, 2019 12:43
Show Gist options
  • Save migreva/1b4e333e066b8d645f939da8457d056b to your computer and use it in GitHub Desktop.
Save migreva/1b4e333e066b8d645f939da8457d056b to your computer and use it in GitHub Desktop.
Even Fib Numbers
/**
* Project Euler, Problem 2
* https://projecteuler.net/problem=2
*
* Each new term in the Fibonacci sequence is generated by adding the previous
* two terms. By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* By considering the terms in the Fibonacci sequence whose values do not
* exceed four million, find the sum of the even-valued terms.
*/
// max fibonacci value is 4 million for this
const MAX_FIB_VALUE = 4000000
/**
* fibEven takes a value (capped at 4 million), and sums the total of all even
* fibonacci values that are less than that value.
*
* e.g fibEven(10) === 10
*
* @param {Number} maxValue - an integer, representing the maximum value
* of the fibonacci value to compute until
* @returns {Number} - the sum of the even values, defaulting to 0 in any error case
*/
function fibEven(maxValue) {
// because its JavaScript, anything can happen
if (isNaN(maxValue)) return 0;
// clamp the value
if (maxValue > MAX_FIB_VALUE) maxValue = MAX_FIB_VALUE;
// fibonacci things
let nMinusOne = 1;
let nMinusTwo = 0;
let fibValue = 0;
// return value
let evenSum = 0;
// loop time (better perf than recursion)
while (fibValue <= maxValue) {
// if this is an even fibonacci value, add it to our sum
if (fibValue % 2 === 0) evenSum += fibValue;
// reassign the fibonacci values
nMinusTwo = nMinusOne;
nMinusOne = fibValue;
// add up the next fibonacci value
fibValue = nMinusOne + nMinusTwo;
}
return evenSum;
}
module.exports = { fibEven };
// this file assumes you're using mocha
var assert = require('assert');
var { fibEven } = require('./fib-even');
const MAX_EVEN_VALUE = 4613732;
describe('Testing the fib even values', function() {
it('Tests to make sure fibEven() returns the correct value', function() {
let testCases = [
// some basic test cases
{
maxValue: 0,
sumValue: 0,
},
{
maxValue: 2,
sumValue: 2,
},
{
maxValue: 10,
sumValue: 10,
},
{
maxValue: 100,
sumValue: 44,
},
{
maxValue: 1000,
sumValue: 798,
},
// lets push the envelope
// (i wonder where that saying comes from)
{
maxValue: 4000000,
sumValue: MAX_EVEN_VALUE,
},
{
maxValue: 4000000000,
sumValue: MAX_EVEN_VALUE,
},
{
maxValue: Infinity,
sumValue: MAX_EVEN_VALUE,
},
{
maxValue: Math.pow(10, 1000),
sumValue: MAX_EVEN_VALUE,
},
// corner cases
{
maxValue: undefined,
sumValue: 0,
},
{
maxValue: null,
sumValue: 0,
},
{
maxValue: -1,
sumValue: 0,
},
{
maxValue: 1.2345,
sumValue: 0,
},
{
maxValue: "abc123",
sumValue: 0,
},
{
maxValue: ["ha", "ha", "ha"],
sumValue: 0,
},
{
maxValue: { foo: 'bar' },
sumValue: 0,
},
{
maxValue: function() {}, // why not
sumValue: 0,
}
];
for (var testCase of testCases) {
// get the sum value
const actualSumValue = fibEven(testCase.maxValue);
// assert the values
assert.equal(
actualSumValue,
testCase.sumValue,
`Sum values do not match for max value: ${testCase.maxValue}`
);
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment