Skip to content

Instantly share code, notes, and snippets.

@Phoenix35
Created November 24, 2019 17:13
Show Gist options
  • Save Phoenix35/1acbabf36cea93cdb2c349e220074be3 to your computer and use it in GitHub Desktop.
Save Phoenix35/1acbabf36cea93cdb2c349e220074be3 to your computer and use it in GitHub Desktop.
"use strict";
/**
* Generate numbers in the Fibonacci sequence in a given interval.
*
* @generator
* @function fibInInterval
* @param {bigint|number} min - The lower limit of the interval.
* @param {bigint|number} max - The upper limit of the interval.
* @yields {bigint} The next number in the Fibonacci sequence respecting the interval.
*/
function *fibInInterval (min, max) {
if (
min < 1n ||
max < 1n
)
throw new TypeError(`min (${min}) and max (${max}) must be superior or equal to 1.`);
if (max < min)
throw new TypeError(`max (${max}) must be superior to min (${min}).`);
let n1 = 1n,
n2 = 1n;
for (; n2 < min; [n1, n2] = [n2, n1 + n2])
;
for (; n2 <= max; [n1, n2] = [n2, n1 + n2])
yield n2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment