forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexagonal_number.py
More file actions
Latest commit
49 lines (42 loc) · 1.32 KB
/
Copy pathhexagonal_number.py
File metadata and controls
49 lines (42 loc) · 1.32 KB
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
47
48
49
"""
== Hexagonal Number ==
The nth hexagonal number hn is the number of distinct dots
in a pattern of dots consisting of the outlines of regular
hexagons with sides up to n dots, when the hexagons are
overlaid so that they share one vertex.
https://en.wikipedia.org/wiki/Hexagonal_number
"""
# Author : Akshay Dubey (https://github.com/itsAkshayDubey)
defhexagonal(number: int) ->int:
"""
:param number: nth hexagonal number to calculate
:return: the nth hexagonal number
Note: A hexagonal number is only defined for positive integers
>>> hexagonal(4)
28
>>> hexagonal(11)
231
>>> hexagonal(22)
946
>>> hexagonal(0)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> hexagonal(-1)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> hexagonal(11.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=11.0] must be an integer
"""
ifnotisinstance(number, int):
msg=f"Input value of [number={number}] must be an integer"
raiseTypeError(msg)
ifnumber<1:
raiseValueError("Input must be a positive integer")
returnnumber* (2*number-1)
if__name__=="__main__":
importdoctest
doctest.testmod()