-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy patharithmetic_progression_builder.py
38 lines (28 loc) · 1.05 KB
/
arithmetic_progression_builder.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Arithmetic progressions
# are special types of sets(more of a series)
# where the difference between every consecutive
# numbers of series is constant or common
# for more refer: https://en.wikipedia.org/wiki/Arithmetic_progression
# formula for general term of arithmetic prog.
# tn = a + (n - 1) * d
# where,
# tn -> is the n(th) term,
# a -> if the first term,
# d -> is the common difference
# Third param 'n_last' refers to limit or length or index of last term
def arithmetic_p_sequence_builder(a, d, n_last):
# To generate an A.P. length should be
# greater than or equal to 1
if n_last < 1:
return -1
seq = []
# Every item is obtained by
# applying the general term formula
for n in range(1, n_last + 1):
seq.append(a + (n - 1) * d)
return seq
# Test
print("Arithmetic progression: {}".format(arithmetic_p_sequence_builder(int(raw_input("A(a, first term): ")), int(raw_input("D(d, common difference): ")), int(raw_input("N(n, length): ")))))
# Although big line, but works just fine!