参考代码
- Clear[test, x, a, f, t]d = 4993; pell = 65; p = 1; q = 2;x[p_, q_] := (p + Sqrt[d])/q;a[p_, q_] := IntegerPart[x[p, q]];t = 1/(x[p, q] - a[p, q]);f[{_, {i_, p_, q_}}] := {{i, p, q}, {i + 1, q a[p, q] - p, (d - (q a[p, q] - p)^2)/q}}test[{_, {i_, p_, q_}}] := (x[p, q] != t && p != pell) || i == 1First@NestWhile[f, {Null, {0, p, q}}, test] // AbsoluteTiming
复制代码 |