In [1]:
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display,Math
In [2]:
print('Arithmetic and geometric sequences')
display(Math('a_n = a_0 + d(n-1)'))
display(Math('g_n = g_0r^{(n-1)}'))
Arithmetic and geometric sequences
$\displaystyle a_n = a_0 + d(n-1)$
$\displaystyle g_n = g_0r^{(n-1)}$
In [3]:
a = 2
d = 3
maxn = 10

print(np.arange(0,maxn))

ariseq = a + d*np.arange(0,maxn)
print(ariseq)
[0 1 2 3 4 5 6 7 8 9]
[ 2  5  8 11 14 17 20 23 26 29]
In [4]:
a = 2
r = 3
maxn = 10

geoseq = a * r**np.arange(0,maxn)
print(geoseq)
[    2     6    18    54   162   486  1458  4374 13122 39366]
In [5]:
plt.plot(ariseq,'ks',label='arithmetic')
plt.plot(geoseq,'ro',label='geometric')

plt.legend()
plt.show()

Exercise

In [6]:
a = 2
d = 3
maxn = 10
nth = 6

ariseq = a + d*np.arange(0,maxn)
geoseq = a * d**np.arange(0,maxn)

ariDirect = a + d*(nth-1)
geoDirect = a * d**(nth-1)

print(ariDirect,ariseq[nth-1])
print(geoDirect,geoseq[nth-1])
17 17
486 486