Continuation of Jupyter's trick
As a preparation, first do the following:
jupyter_notebook
import re, IPython.core.getipython
from math import log
class NumberSequence:
def __init__(self, s):
ss = [t.replace(' ','') for t in re.sub('(.)-', r'\1+-', s).split('+')]
assert len(ss) == 3 or (len(ss) > 3 and ss[3] == '...')
tt = [int(i) for i in ss[:3]]
d, e = tt[1] - tt[0], tt[1]//(tt[0] if tt[0] else 1)
isratio = tt[2]/(tt[1] if tt[1] else 1) == e
assert isratio or tt[2]-tt[1] == d
ssum, self.value = r'\sum_{i=0}', None
if len(ss) > 4:
last = int(ss[-1])
if isratio:
n = int(round(log(last / tt[0], abs(e)))) + 1
assert last == tt[0]*e**(n-1)
self.value = sum(tt[0]*e**i for i in range(n))
else:
n = (last-tt[0]) // d + 1
assert last == tt[0]+d*(n-1)
self.value = sum(tt[0]+d*i for i in range(n))
ssum += '^{%d}'%(n-1)
if isratio:
self.form = '$%s{%s%d^i}$'%(ssum, '' if tt[0]==1 else r'%s \times '%tt[1], e)
else:
self.form = r'$%s{%s%s i}$'%(ssum, '' if tt[0]==0 else '%s + '%tt[0],
'' if d==1 else '%d \times'%d)
def _repr_html_(self):
if self.value is not None:
print(self.value)
return self.form
def S_impl(s):
return NumberSequence(s)
ip = IPython.core.getipython.get_ipython()
if ip:
ip.register_magic_function(S_impl, magic_name='S')
jupyter_notebook
%S 1+4+7+...
The calculation formula is displayed.
jupyter_notebook
%S 1+4+7+...+19
>>>
70
With the last term, the sum and formula are displayed.
jupyter_notebook
%S 1-3+9+...
You can omit the "%" of "% S".
jupyter_notebook
S 1 - 3 + 9 + ... + 81
>>>
61
jupyter_notebook
S {'+'.join(['0','1','2','...','100'])}
>>>
5050
You can eval it first by enclosing it in "{}".
that's all
Recommended Posts