From c78fdcd01a65b3f57143f0f81633df44abea3ff3 Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Thu, 26 May 2022 14:54:42 -0700 Subject: [PATCH 1/9] Create TwinPrime.js --- Maths/TwinPrime.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Maths/TwinPrime.js diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js new file mode 100644 index 0000000000..1b45a2ec12 --- /dev/null +++ b/Maths/TwinPrime.js @@ -0,0 +1,33 @@ +function isPrime (n) { + let prime = false + + if (n > 1) { + for (let i = 2; i < n; i++) { + if (n % i === 0) { + prime = true + break + } + } + } + + return !prime +} + +function twinPrime (n) { + const result = [] + const prime = prime(n) + + if (!prime) { + return [] + } + + result.push(n) + + if (!isPrime(n + 2)) { + return [] + } + + result.push(n + 2) + + return result +} From d568da1ba0db7c6db6ce57c8affa7f49bcef1d8a Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Thu, 26 May 2022 14:56:08 -0700 Subject: [PATCH 2/9] Update TwinPrime.js --- Maths/TwinPrime.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index 1b45a2ec12..7796a841ae 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -20,14 +20,16 @@ function twinPrime (n) { if (!prime) { return [] } - + result.push(n) - + if (!isPrime(n + 2)) { return [] } - + result.push(n + 2) - + return result } + +export { isPrime, twinPrime } From 70d1f4f90068f45b3ef381fa0cf943d25047a97c Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Thu, 26 May 2022 14:58:12 -0700 Subject: [PATCH 3/9] Create TwinPrime.test.js --- Maths/test/TwinPrime.test.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Maths/test/TwinPrime.test.js diff --git a/Maths/test/TwinPrime.test.js b/Maths/test/TwinPrime.test.js new file mode 100644 index 0000000000..5f31019e28 --- /dev/null +++ b/Maths/test/TwinPrime.test.js @@ -0,0 +1,7 @@ +import { twinPrime } from '../TwinPrime.js' + +describe('Twin Primes', () => { + it('Should be valid twin primes', () => { + expect(twinPrime(5)).toStrictEqual([5, 7]) + }) +}) From 2f828284141eb411e9a94a9cd2ee5f1841122ca8 Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Thu, 26 May 2022 15:00:46 -0700 Subject: [PATCH 4/9] Update TwinPrime.js --- Maths/TwinPrime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index 7796a841ae..bbd398e27d 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -15,7 +15,7 @@ function isPrime (n) { function twinPrime (n) { const result = [] - const prime = prime(n) + const prime = isPrime(n) if (!prime) { return [] From 44d2b33f27941d9221bea77b686cf49d273dc646 Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Thu, 26 May 2022 15:06:47 -0700 Subject: [PATCH 5/9] Update TwinPrime.js --- Maths/TwinPrime.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index bbd398e27d..71343fc89d 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -13,6 +13,14 @@ function isPrime (n) { return !prime } +/** + * @function twinPrime + * Gets the 'twin prime' of a prime number. + * @returns {Array} Either an array with the original [0], and the twin [1], or an empty array if one of the numbers are not prime. + * @see https://en.wikipedia.org/wiki/Twin_prime + * @example twinPrime(5) = [5, 7] + * @example twinPrime(4) = [] +*/ function twinPrime (n) { const result = [] const prime = isPrime(n) From 3e7f6abd5a608b257fa58cbe0f03ea9cee93f4be Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Fri, 27 May 2022 07:52:59 -0700 Subject: [PATCH 6/9] Add suggestions --- Maths/TwinPrime.js | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index 71343fc89d..d3772cbcc3 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -1,43 +1,29 @@ -function isPrime (n) { - let prime = false - - if (n > 1) { - for (let i = 2; i < n; i++) { - if (n % i === 0) { - prime = true - break - } - } - } - - return !prime -} +import { PrimeCheck } from './PrimeCheck' /** * @function twinPrime * Gets the 'twin prime' of a prime number. - * @returns {Array} Either an array with the original [0], and the twin [1], or an empty array if one of the numbers are not prime. + * + * @param {Integer} n The number to find the twin prime of. + * @returns {Integer} Either the twin, or -1 if n or n + 2 is not prime. + * * @see https://en.wikipedia.org/wiki/Twin_prime - * @example twinPrime(5) = [5, 7] - * @example twinPrime(4) = [] + * + * @example twinPrime(5) = 7 + * @example twinPrime(4) = -1 */ function twinPrime (n) { - const result = [] const prime = isPrime(n) if (!prime) { - return [] + return -1 } - result.push(n) - if (!isPrime(n + 2)) { - return [] + return -1 } - result.push(n + 2) - - return result + return n + 2 } -export { isPrime, twinPrime } +export { twinPrime } From 4560c8fd2415c0d791dd43223a9c283cc500b6f1 Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Fri, 27 May 2022 07:54:28 -0700 Subject: [PATCH 7/9] Update TwinPrime.test.js --- Maths/test/TwinPrime.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Maths/test/TwinPrime.test.js b/Maths/test/TwinPrime.test.js index 5f31019e28..c3e057e10e 100644 --- a/Maths/test/TwinPrime.test.js +++ b/Maths/test/TwinPrime.test.js @@ -2,6 +2,9 @@ import { twinPrime } from '../TwinPrime.js' describe('Twin Primes', () => { it('Should be valid twin primes', () => { - expect(twinPrime(5)).toStrictEqual([5, 7]) + expect(twinPrime(3)).toBe(5) + expect(twinPrime(5)).toBe(7) + expect(twinPrime(4)).toBe(-1) + expect(twinPrime(17)).toBe(19) }) }) From 8e3389d4ed4896479c1cdc357b4dc8f17f23de6b Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Fri, 27 May 2022 07:56:48 -0700 Subject: [PATCH 8/9] Update TwinPrime.js --- Maths/TwinPrime.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index d3772cbcc3..638fd15b28 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -13,13 +13,13 @@ import { PrimeCheck } from './PrimeCheck' * @example twinPrime(4) = -1 */ function twinPrime (n) { - const prime = isPrime(n) + const prime = PrimeCheck(n) if (!prime) { return -1 } - if (!isPrime(n + 2)) { + if (!PrimeCheck(n + 2)) { return -1 } From f4cc0d7bacc08b7eb0dad8189fd0270fca35a52e Mon Sep 17 00:00:00 2001 From: Exortions <75327059+Exortions@users.noreply.github.com> Date: Fri, 27 May 2022 07:59:25 -0700 Subject: [PATCH 9/9] Styling --- Maths/TwinPrime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/TwinPrime.js b/Maths/TwinPrime.js index 638fd15b28..871b1eab70 100644 --- a/Maths/TwinPrime.js +++ b/Maths/TwinPrime.js @@ -3,7 +3,7 @@ import { PrimeCheck } from './PrimeCheck' /** * @function twinPrime * Gets the 'twin prime' of a prime number. - * + * * @param {Integer} n The number to find the twin prime of. * @returns {Integer} Either the twin, or -1 if n or n + 2 is not prime. *