-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathtwin.go
More file actions
22 lines (20 loc) · 637 Bytes
/
twin.go
File metadata and controls
22 lines (20 loc) · 637 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// twin.go
// description: Returns Twin Prime of n
// details:
// For any integer n, twin prime is (n + 2)
// if and only if both n and (n + 2) both are prime
// wikipedia: https://en.wikipedia.org/wiki/Twin_prime
// time complexity: O(log n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see twin_test.go
package prime
// This function returns twin prime for given number
// returns (n + 2) if both n and (n + 2) are prime
// -1 otherwise
func Twin(n int) (int, bool) {
if OptimizedTrialDivision(int64(n)) && OptimizedTrialDivision(int64(n+2)) {
return n + 2, true
}
return -1, false
}