0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

(級数)「【Project Euler】Problem 6: 2乗の和の差」を参考にsympyでやってみた。(2)

Last updated at Posted at 2022-01-04

つづき

1から10までの2乗の和と和の2乗と差を求めよ
1から100までの2乗の和と和の2乗と差を求めよ

Pycharmで

from sympy import *
var('i n')
mySum=(summation(i, (i, 1, n)))**2-summation(i**2, (i, 1, n))
print("#        ",         mySum )
print("#expand  ",expand  (mySum))
print("#simplify",simplify(mySum))
print("#",mySum.subs({n:10}))
print("#",mySum.subs({n:100}))
#         -n**3/3 - n**2/2 - n/6 + (n**2/2 + n/2)**2
#expand   n**4/4 + n**3/6 - n**2/4 - n/6
#simplify n*(3*n**3 + 2*n**2 - 3*n - 2)/12
# 2640
# 25164150
from sympy import *
var('i n')
mySum=(Sum(i, (i, 1, n)).doit())**2-Sum(i**2, (i, 1, n)).doit()
print("#        ",         mySum )
print("#expand  ",expand  (mySum))
print("#simplify",simplify(mySum))
print("#",mySum.subs({n:10}))
print("#",mySum.subs({n:100}))
#         -n**3/3 - n**2/2 - n/6 + (n**2/2 + n/2)**2
#expand   n**4/4 + n**3/6 - n**2/4 - n/6
#simplify n*(3*n**3 + 2*n**2 - 3*n - 2)/12
# 2640
# 25164150

参考

summation

sum

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?