開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 4.(Algebra and Symbolic Math with SymPy)、Programming Challenges #3: Summing a Series(No. 2922)を解いてみる。
#3: Summing a Series(No. 2922)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sympy
n = sympy.Symbol('n')
term = input('Enter the nth term: ')
num = input('Enter the number of terms: ')
try:
term = sympy.sympify(term)
except sympy.SympifyError as err:
print(err)
else:
s = sympy.summation(term, (n, 1, num))
sympy.pprint(s)
入出力結果(Terminal, IPython)
$ ./sample3.py Enter the nth term: s**n/n Enter the number of terms: 5 5 4 3 2 s s s s ── + ── + ── + ── + s 5 4 3 2 $ ./sample3.py Enter the nth term: a+(n-1)*d Enter the number of terms: 3 3⋅a + 3⋅d $
0 コメント:
コメントを投稿