forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twin_prime.py
46 lines (39 loc) · 1.1 KB
/
twin_prime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
== Twin Prime ==
A number n+2 is said to be a Twin prime of number n if
both n and n+2 are prime.
Examples of Twin pairs: (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), ...
https://en.wikipedia.org/wiki/Twin_prime
"""
# Author : Akshay Dubey (https://github.com/itsAkshayDubey)
from maths.prime_check import is_prime
def twin_prime(number: int) -> int:
"""
# doctest: +NORMALIZE_WHITESPACE
This functions takes an integer number as input.
returns n+2 if n and n+2 are prime numbers and -1 otherwise.
>>> twin_prime(3)
5
>>> twin_prime(4)
-1
>>> twin_prime(5)
7
>>> twin_prime(17)
19
>>> twin_prime(0)
-1
>>> twin_prime(6.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=6.0] must be an integer
"""
if not isinstance(number, int):
msg = f"Input value of [number={number}] must be an integer"
raise TypeError(msg)
if is_prime(number) and is_prime(number + 2):
return number + 2
else:
return -1
if __name__ == "__main__":
import doctest
doctest.testmod()