Skip to content

Commit 076193e

Browse files
feat: Add pronic number implementation (TheAlgorithms#7979)
* feat: Add pronic number implementation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3f9aae1 commit 076193e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

maths/pronic_number.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
== Pronic Number ==
3+
A number n is said to be a Proic number if
4+
there exists an integer m such that n = m * (m + 1)
5+
6+
Examples of Proic Numbers: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110 ...
7+
https://en.wikipedia.org/wiki/Pronic_number
8+
"""
9+
10+
# Author : Akshay Dubey (https://github.com/itsAkshayDubey)
11+
12+
13+
def is_pronic(number: int) -> bool:
14+
"""
15+
# doctest: +NORMALIZE_WHITESPACE
16+
This functions takes an integer number as input.
17+
returns True if the number is pronic.
18+
>>> is_pronic(-1)
19+
False
20+
>>> is_pronic(0)
21+
True
22+
>>> is_pronic(2)
23+
True
24+
>>> is_pronic(5)
25+
False
26+
>>> is_pronic(6)
27+
True
28+
>>> is_pronic(8)
29+
False
30+
>>> is_pronic(30)
31+
True
32+
>>> is_pronic(32)
33+
False
34+
>>> is_pronic(2147441940)
35+
True
36+
>>> is_pronic(9223372033963249500)
37+
True
38+
>>> is_pronic(6.0)
39+
Traceback (most recent call last):
40+
...
41+
TypeError: Input value of [number=6.0] must be an integer
42+
"""
43+
if not isinstance(number, int):
44+
raise TypeError(f"Input value of [number={number}] must be an integer")
45+
if number < 0 or number % 2 == 1:
46+
return False
47+
number_sqrt = int(number**0.5)
48+
return number == number_sqrt * (number_sqrt + 1)
49+
50+
51+
if __name__ == "__main__":
52+
import doctest
53+
54+
doctest.testmod()

0 commit comments

Comments
 (0)