mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
algorithm: Twin prime (#7980)
* feat: Add twin prime algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: Fix broken import statement Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7b2eca0243
commit
e1be882f72
45
maths/twin_prime.py
Normal file
45
maths/twin_prime.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
"""
|
||||
== 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):
|
||||
raise TypeError(f"Input value of [number={number}] must be an integer")
|
||||
if is_prime(number) and is_prime(number + 2):
|
||||
return number + 2
|
||||
else:
|
||||
return -1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user