File tree Expand file tree Collapse file tree 1 file changed +12
-26
lines changed Expand file tree Collapse file tree 1 file changed +12
-26
lines changed Original file line number Diff line number Diff line change 1
- function isPrime ( n ) {
2
- let prime = false
3
-
4
- if ( n > 1 ) {
5
- for ( let i = 2 ; i < n ; i ++ ) {
6
- if ( n % i === 0 ) {
7
- prime = true
8
- break
9
- }
10
- }
11
- }
12
-
13
- return ! prime
14
- }
1
+ import { PrimeCheck } from './PrimeCheck'
15
2
16
3
/**
17
4
* @function twinPrime
18
5
* Gets the 'twin prime' of a prime number.
19
- * @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.
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
+ *
20
10
* @see https://en.wikipedia.org/wiki/Twin_prime
21
- * @example twinPrime(5) = [5, 7]
22
- * @example twinPrime(4) = []
11
+ *
12
+ * @example twinPrime(5) = 7
13
+ * @example twinPrime(4) = -1
23
14
*/
24
15
function twinPrime ( n ) {
25
- const result = [ ]
26
16
const prime = isPrime ( n )
27
17
28
18
if ( ! prime ) {
29
- return [ ]
19
+ return - 1
30
20
}
31
21
32
- result . push ( n )
33
-
34
22
if ( ! isPrime ( n + 2 ) ) {
35
- return [ ]
23
+ return - 1
36
24
}
37
25
38
- result . push ( n + 2 )
39
-
40
- return result
26
+ return n + 2
41
27
}
42
28
43
- export { isPrime , twinPrime }
29
+ export { twinPrime }
You can’t perform that action at this time.
0 commit comments