Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

https://www.dnc.ac.jp/albums/abm.php?f=abm00035515.pdf&n=02-03_%E5%95%8F%E9%A1%8C%E5%86%8A%E5%AD%90_%E6%95%B0%E5%AD%A6%E2%91%A1.pdf#page=20

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

偶然うごいている気がします。何かいい方法ないですか?
(参考)フィボナッチ数を求める
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
募集中

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

ア:
募集中

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away