帐号 自动登录 找回密码 密码 欢迎注册
 搜索

# [求助] 连分式PQa算法修改

### 马上注册，结交更多好友，享用更多功能，让你轻松玩转社区。

x

1. Clear[test, x, a, f, t]
2. d = 45113; pell = -1; P = 0; Q = 1;
3. x[P_, Q_] := (P + Sqrt[d])/Q;
4. a[P_, Q_] := IntegerPart[x[P, Q]];
5. t = 1/(x[P, Q] - a[P, Q]);
6. f[{_, {i_, P_, Q_}}] := {{i, P, Q}, {i + 1, Q a[P, Q] - P, (d - (Q a[P, Q] - P)^2)/Q}}
7. test[{_, {i_, P_, Q_}}] := (x[P, Q] != t && P != pell) || i == 1
8. First@NestWhile[f, {Null, {0, P, Q}}, test] // AbsoluteTiming

1. Clear[t, a]; d = 45113; pell = -1; P[0] = 0; Q[0] = 1;
2. t[0] = (P[0] + Sqrt[d])/Q[0]
3. a[0] = IntegerPart[t[0]];
4. i = 0; While[(t[i] != 1/(t[0] - a[0]) && P[i] != pell) || i == 1,
5. P[i + 1] = Q[i] a[i] - P[i];
6. Q[i + 1] = (d - P[i + 1]^2)/Q[i];
7. t[i + 1] = (P[i + 1] + Sqrt[d])/Q[i + 1];
8. a[i + 1] = IntegerPart[t[i + 1]];
9. If[Q[i + 1] == Q[i] || P[i + 1] == P[i], Break[]];
10. Print[{i, Q[i], P[i], a[i]}] i++];
11. {i, Q[i], P[i], a[i]}
12. {i + 1, Q[i + 1], P[i + 1], a[i + 1]}

 您需要登录后才可以回帖 登录 | 欢迎注册 本版积分规则 回帖后跳转到最后一页

GMT+8, 2020-10-20 05:22 , Processed in 0.124380 second(s), 16 queries .