mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
4b79d771cd
* Add more ruff rules * Add more ruff rules * pre-commit: Update ruff v0.0.269 -> v0.0.270 * Apply suggestions from code review * Fix doctest * Fix doctest (ignore whitespace) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
"""
|
|
== 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()
|