Created
November 24, 2019 17:13
-
-
Save Phoenix35/1acbabf36cea93cdb2c349e220074be3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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