-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_series.py
45 lines (37 loc) · 1.14 KB
/
arithmetic_series.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
""" common arithmetic series """
def triangular(n):
""" returns nth triangular number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return (n*n+1) //2
def square(n):
""" returns nth square number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return n**2
def pentagonal(n):
""" returns nth pentagonal number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return (n*(3*n-1))//2
def hexagonal(n):
"""returns nth hexagonal number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return n*(2*n-1)
def heptagonal(n):
""" returns nth heptagonal number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return (n*((5*n)-3))//2
def octagonal(n):
""" returns nth octagonal number """
if n < 1 or n != n // 1:
raise(ValueError('input must be a positive integer'))
else:
return (n*(3*n-2))