LoginSignup
0
0

More than 5 years have passed since last update.

sympyで「数列の漸化式」大学入学共通テスト>平成30年度...正解等_1111>数学Ⅱ・数学B>第4問(1) をやってみたい

Posted at

まずは、一般項からやってみた。

偶然うごいている気がします。何かいい方法ないですか?
(参考)フィボナッチ数を求める
http://izmmr.blog.jp/archives/10271522.html
(参考)sympyの連結の使い方を教えて下さい
https://ja.stackoverflow.com/questions/33769/sympy%e3%81%ae%e9%80%a3%e7%b5%90%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%82%92%e6%95%99%e3%81%88%e3%81%a6%e4%b8%8b%e3%81%95%e3%81%84/33773#33773
(参考)SymPy can print Exponentiation base and exponent?
https://stackoverflow.com/questions/53431596/sympy-can-print-exponentiation-base-and-exponent
(参考)SymPy simplify cannot 2*3*n/3 ---> 2*3*(n-1)
https://stackoverflow.com/questions/53481402/sympy-simplify-cannot-23n-3-23n-1

fullscript.py
from sympy import *
i,n=symbols('i n',integer=True,positive=True)
def ps(e, *args):
 x = Dummy(integer=True)
 t=list(Add.make_args(e))
 for i, ti in enumerate(t):
  c, r = ti.as_coeff_Mul()
  if c.is_Rational and not c.is_Integer:
    t[i] = Mul(c.p, r, Pow(c.q, x), evaluate=False)
    t[i] = powsimp(t[i], *args).xreplace({x: -1})
  else:
    t[i] = powsimp(t[i], *args)
 return Add(*t)
def Mycoefficient(h):
    Mypoly = poly(h)
    aa = Mypoly.all_coeffs()[1]
    bb = h - aa
    n, d = fraction(bb)
    nn0 = n.as_base_exp()[0]
    nn1 = poly(nn0)
    import re
    rese1 = re.search('^Poly\((.+?),(.+?),', str(nn1)).group(1)
    rese2 = re.search('^Poly\((.+?),(.+?),', str(nn1)).group(2)
    two=sympify(sympify(rese1)/sympify(rese2))
    return two

a = Function('a')
f=a(n+1)-3*a(n)+8
fib=rsolve(f, a(n),{a(1):6})
Mypoly = poly(simplify(fib))
print("a(n)=",ps(fib))
print("解答記号:正解")
print("イ      :",Mycoefficient(fib))
print("ウ      :",simplify((fib-Mypoly.all_coeffs()[1])/Mycoefficient(fib)).as_base_exp()[0])
print("エ      :",Mypoly.all_coeffs()[1])
# a(n)= 2*3**(n - 1) + 4
# 解答記号:正解
# イ      : 2
# ウ      : 3
# エ      : 4

正規表現で、文字列をあてた方がわかりやすいですか?

募集中

解答欄の形から、値が1桁の整数になることはわかるわけです・・・ということは、0から9まで全部試してしまえばいいのではないでしょうか。

(参考)2015年センター試験数学IAを全てプログラム(Python)で解く
https://qiita.com/Akai_Banana/items/b328fe0116d248127a36
募集中

問題文の手順通りにやってみたい。

ア:
募集中

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