Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e7f6ab

Browse files
Add suggestions
1 parent 44d2b33 commit 3e7f6ab

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

‎Maths/TwinPrime.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
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'
152

163
/**
174
* @function twinPrime
185
* 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+
*
2010
* @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
2314
*/
2415
function twinPrime (n) {
25-
const result = []
2616
const prime = isPrime(n)
2717

2818
if (!prime) {
29-
return []
19+
return -1
3020
}
3121

32-
result.push(n)
33-
3422
if (!isPrime(n + 2)) {
35-
return []
23+
return -1
3624
}
3725

38-
result.push(n + 2)
39-
40-
return result
26+
return n + 2
4127
}
4228

43-
export { isPrime,twinPrime }
29+
export { twinPrime }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /