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/7] 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/7] 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/7] 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 From f09aef16d8d940180c7fb151f53b1beb667dcdbe Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Mon, 25 Oct 2021 19:47:56 +0530 Subject: [PATCH 4/7] Added Aliquot Sum and tests --- Maths/AliquotSum.js | 27 +++++++++++++++++++++++++++ Maths/test/AliquotSum.test.js | 15 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Maths/AliquotSum.js create mode 100644 Maths/test/AliquotSum.test.js diff --git a/Maths/AliquotSum.js b/Maths/AliquotSum.js new file mode 100644 index 0000000000..9d5429c02f --- /dev/null +++ b/Maths/AliquotSum.js @@ -0,0 +1,27 @@ +/* + A program to calculate the Aliquot Sum of a number. + Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum + */ + +/** + * @param {Number} input The number whose aliquot sum you want to calculate + */ +function aliquotSum (input) { + // input can't be negative + if (input < 0) throw new TypeError('Input cannot be Negative') + + // input can't be a decimal + if (Math.floor(input) !== input) throw new TypeError('Input cannot be a Decimal') + + // Dealing with 1, which isn't a prime + if (input === 1) return 0 + + let sum = 0 + for (let i = 1; i <= (input / 2); i++) { + if (input % i === 0) sum += i + } + + return sum +} + +export { aliquotSum } diff --git a/Maths/test/AliquotSum.test.js b/Maths/test/AliquotSum.test.js new file mode 100644 index 0000000000..cfb5919d3a --- /dev/null +++ b/Maths/test/AliquotSum.test.js @@ -0,0 +1,15 @@ +import { aliquotSum } from '../AliquotSum' + +describe('Aliquot Sum of a Number', () => { + it('Aliquot Sum of 6', () => { + expect(aliquotSum(6)).toBe(6) + }) + + it('Aliquot Sum of -1', () => { + expect(aliquotSum(-1)).toThrow() + }) + + it('Aliquot Sum of 10.5', () => { + expect(aliquotSum(10.5)).toThrow() + }) +}) From e76aae2b2ed7d1da78af1e8f06fe3e856b4fc1ed Mon Sep 17 00:00:00 2001 From: Dumby <71999854+SpiderMath@users.noreply.github.com> Date: Tue, 26 Oct 2021 00:47:23 +0530 Subject: [PATCH 5/7] Fix ALiquot tests, need to learn how to use Jest --- Maths/test/AliquotSum.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/test/AliquotSum.test.js b/Maths/test/AliquotSum.test.js index cfb5919d3a..88a64299de 100644 --- a/Maths/test/AliquotSum.test.js +++ b/Maths/test/AliquotSum.test.js @@ -5,11 +5,11 @@ describe('Aliquot Sum of a Number', () => { expect(aliquotSum(6)).toBe(6) }) - it('Aliquot Sum of -1', () => { - expect(aliquotSum(-1)).toThrow() + it('Aliquot Sum of 1', () => { + expect(aliquotSum(1)).toBe(0) }) - it('Aliquot Sum of 10.5', () => { - expect(aliquotSum(10.5)).toThrow() + it('Aliquot Sum of 28', () => { + expect(aliquotSum(28)).toBe(28) }) }) From 0551b723ffb72f796690deb6e0294f5bcc9e40d9 Mon Sep 17 00:00:00 2001 From: Dumby <71999854+SpiderMath@users.noreply.github.com> Date: Tue, 26 Oct 2021 12:12:06 +0530 Subject: [PATCH 6/7] Added some explanation for the Aliquot sum --- Maths/AliquotSum.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Maths/AliquotSum.js b/Maths/AliquotSum.js index 9d5429c02f..5f61d87c55 100644 --- a/Maths/AliquotSum.js +++ b/Maths/AliquotSum.js @@ -1,5 +1,10 @@ /* A program to calculate the Aliquot Sum of a number. + The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself + For example, for the number 6 + The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6 + 1 is the only number whose aliquot sum is 0 (since its only divisor is 1 and aliquot sum of a number couldn't have itself) + For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1 Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum */ From dd1a2083b8f9987251d43f967c2d95c108c931ce Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Tue, 26 Oct 2021 16:42:37 +0530 Subject: [PATCH 7/7] Change file names of DecimalIsolate & IsOdd to Pascal case --- Maths/{decimalIsolate.js => DecimalIsolate.js} | 0 Maths/{isOdd.js => IsOdd.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Maths/{decimalIsolate.js => DecimalIsolate.js} (100%) rename Maths/{isOdd.js => IsOdd.js} (100%) diff --git a/Maths/decimalIsolate.js b/Maths/DecimalIsolate.js similarity index 100% rename from Maths/decimalIsolate.js rename to Maths/DecimalIsolate.js diff --git a/Maths/isOdd.js b/Maths/IsOdd.js similarity index 100% rename from Maths/isOdd.js rename to Maths/IsOdd.js