File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { PrimeCheck } from './PrimeCheck'
2
+
3
+ /**
4
+ * @function twinPrime
5
+ * Gets the 'twin prime' of a prime number.
6
+ *
7
+ * @param {Integer } n The number to find the twin prime of.
8
+ * @returns {Integer } Either the twin, or -1 if n or n + 2 is not prime.
9
+ *
10
+ * @see https://en.wikipedia.org/wiki/Twin_prime
11
+ *
12
+ * @example twinPrime(5) = 7
13
+ * @example twinPrime(4) = -1
14
+ */
15
+ function twinPrime ( n ) {
16
+ const prime = PrimeCheck ( n )
17
+
18
+ if ( ! prime ) {
19
+ return - 1
20
+ }
21
+
22
+ if ( ! PrimeCheck ( n + 2 ) ) {
23
+ return - 1
24
+ }
25
+
26
+ return n + 2
27
+ }
28
+
29
+ export { twinPrime }
Original file line number Diff line number Diff line change
1
+ import { twinPrime } from '../TwinPrime.js'
2
+
3
+ describe ( 'Twin Primes' , ( ) => {
4
+ it ( 'Should be valid twin primes' , ( ) => {
5
+ expect ( twinPrime ( 3 ) ) . toBe ( 5 )
6
+ expect ( twinPrime ( 5 ) ) . toBe ( 7 )
7
+ expect ( twinPrime ( 4 ) ) . toBe ( - 1 )
8
+ expect ( twinPrime ( 17 ) ) . toBe ( 19 )
9
+ } )
10
+ } )
You can’t perform that action at this time.
0 commit comments