From ec34a04678df80aad99088f94a129649ddef3015 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 24 Oct 2021 01:58:06 +0530 Subject: [PATCH 1/3] Added LucasSeries --- Maths/LucasSeries.js | 33 +++++++++++++++++++++++++++++++++ Maths/test/LucasSeries.test.js | 7 +++++++ 2 files changed, 40 insertions(+) create mode 100644 Maths/LucasSeries.js create mode 100644 Maths/test/LucasSeries.test.js diff --git a/Maths/LucasSeries.js b/Maths/LucasSeries.js new file mode 100644 index 0000000000..b6c2099f8a --- /dev/null +++ b/Maths/LucasSeries.js @@ -0,0 +1,33 @@ +/* + Program to get the Nth Lucas Number + Article on Lucas Number: https://en.wikipedia.org/wiki/Lucas_number + Examples: + > loopLucas(1) + 1 + > loopLucas(20) + 15127 + > loopLucas(100) + 792070839848372100000 +*/ + +/** + * @param {Number} index The position of the number you want to get from the Lucas Series + */ +function loopLucas (index) { + // index can't be negative + if (index < 0) throw new RangeError('Index cannot be Negative') + + // index can't be a decimal + if (Math.floor(index) !== index) throw new RangeError('Index cannot be a Decimal') + + let a = 2 + let b = 1 + for (let i = 0; i < index; i++) { + const temp = a + b + a = b + b = temp + } + return a +} + +export { loopLucas } diff --git a/Maths/test/LucasSeries.test.js b/Maths/test/LucasSeries.test.js new file mode 100644 index 0000000000..7482b2c56e --- /dev/null +++ b/Maths/test/LucasSeries.test.js @@ -0,0 +1,7 @@ +import { loopLucas } from '../LucasSeries' + +describe('Nth Lucas Number', () => { + it('should return the 20th Lucas Number', () => { + expect(loopLucas(20)).toBe(15127) + }) +}) From cc9f0bfd2a2263c853005a47f447a4f9e905d0df Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 24 Oct 2021 23:49:04 +0530 Subject: [PATCH 2/3] Added more tests and renamed function --- Maths/LucasSeries.js | 4 ++-- Maths/test/LucasSeries.test.js | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Maths/LucasSeries.js b/Maths/LucasSeries.js index b6c2099f8a..e3a57b73a4 100644 --- a/Maths/LucasSeries.js +++ b/Maths/LucasSeries.js @@ -13,7 +13,7 @@ /** * @param {Number} index The position of the number you want to get from the Lucas Series */ -function loopLucas (index) { +function lucas (index) { // index can't be negative if (index < 0) throw new RangeError('Index cannot be Negative') @@ -30,4 +30,4 @@ function loopLucas (index) { return a } -export { loopLucas } +export { lucas } diff --git a/Maths/test/LucasSeries.test.js b/Maths/test/LucasSeries.test.js index 7482b2c56e..21d6d09042 100644 --- a/Maths/test/LucasSeries.test.js +++ b/Maths/test/LucasSeries.test.js @@ -1,7 +1,15 @@ -import { loopLucas } from '../LucasSeries' +import { lucas } from '../LucasSeries' describe('Nth Lucas Number', () => { it('should return the 20th Lucas Number', () => { - expect(loopLucas(20)).toBe(15127) + expect(lucas(20)).toBe(15127) + }) + + it('should return the 20th Lucas Number', () => { + expect(lucas(0)).toBe(2) + }) + + it('should return the 20th Lucas Number', () => { + expect(lucas(100)).toBe(792070839848372100000) }) }) From a43a01e856640950563a296f90581d7418334fff Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Mon, 25 Oct 2021 09:39:23 +0530 Subject: [PATCH 3/3] Changed RangeError to TypeError --- Maths/LucasSeries.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/LucasSeries.js b/Maths/LucasSeries.js index e3a57b73a4..11156d7210 100644 --- a/Maths/LucasSeries.js +++ b/Maths/LucasSeries.js @@ -15,10 +15,10 @@ */ function lucas (index) { // index can't be negative - if (index < 0) throw new RangeError('Index cannot be Negative') + if (index < 0) throw new TypeError('Index cannot be Negative') // index can't be a decimal - if (Math.floor(index) !== index) throw new RangeError('Index cannot be a Decimal') + if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal') let a = 2 let b = 1