找回密码
 欢迎注册
查看: 22025|回复: 5

[求助] Mathematica代码修改

[复制链接]
发表于 2020-10-27 21:29:48 | 显示全部楼层 |阅读模式

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

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
  1. Clear[t, a]; d = 5*(2^32 + 1); pell = -1; P[0] = 5; Q[0] = 11;
  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 < 200,
  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]}
复制代码


以上代码出现无限运算模式,如何修改使得最大迭代长度 i 不超过200
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2020-10-28 08:30:02 | 显示全部楼层
代码太差了,连个注释都没有,谁愿意看?
懂得换位思考吗?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2020-10-28 11:06:46 | 显示全部楼层
本帖最后由 dlpg070 于 2020-10-28 14:09 编辑

没有看算法,只想帮忙解决200次迭代问题,能帮则帮,举手之劳
谁都经过入门阶段.
在i++之前加一句
;If[i>=200,Break[]];
即可
  1. Clear[t, a]; d = 5*(2^32 + 1); pell = -1; P[0] = 5; Q[0] = 11;
  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 < 200,
  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]}]; If[i >= 200, Break[]]; i++];
  11. {i, Q[i], P[i], a[i]}
  12. {i + 1, Q[i + 1], P[i + 1], a[i + 1]}
复制代码


输出:

{0,11,5,13322}
{1,158556,146537,1}
{2,134529,12019,1}
{3,48065,122510,5}
{4,158004,117815,1}
{5,125691,40189,1}
{6,112691,85502,2}
{7,16935,139880,16}
{8,253491,131080,1}
{9,25604,122411,10}
{10,141311,133629,1}
{11,151551,7682,1}
{12,5124,143869,56}
{13,196015,143075,1}
{14,95259,52940,2}
{15,26739,137578,10}
{16,172919,129812,1}
{17,113444,43107,1}
{18,145689,70337,1}
{19,108429,75352,2}
{20,13381,141506,21}
{21,150660,139495,1}
{22,141711,11165,1}
{23,31279,130546,8}
{24,228591,119686,1}
{25,42060,108905,6}
{26,21291,143455,13}
{27,173711,133328,1}
{28,114236,40383,1}
{29,140241,73853,1}
{30,121701,66388,1}
{31,151316,55313,1}
{32,81011,96003,2}
{33,211284,66019,1}
{34,1765,145265,165}
{35,96609,145960,3}
{36,8044,143867,36}
{37,30009,145717,9}
{38,200221,124364,1}
{39,78516,75857,2}
{40,189585,81175,1}
{41,51281,108410,4}
{42,236369,96714,1}
{43,8340,139655,34}
{44,91869,143905,3}
{45,44949,131702,6}
{46,54129,137992,5}
{47,71644,132653,3}
{48,205251,82279,1}
{49,30951,122972,8}
{50,191939,124636,1}
{51,88284,67303,2}
{52,108015,109265,2}
{53,93284,106765,2}
{54,161939,79803,1}
{55,90951,82136,2}
{56,126679,99766,1}
{57,163804,26913,1}
{58,16701,136891,16}
{59,268860,130325,1}
{60,8491,138535,33}
{61,165471,141668,1}
{62,126356,23803,1}
{63,86721,102553,2}
{64,189684,70889,1}
{65,38815,118795,6}
{66,217884,114095,1}
{67,49121,103789,5}
{68,27749,141816,10}
{69,110541,135674,2}
{70,128281,85408,1}
{71,153076,42873,1}
{72,60951,110203,4}
{73,59484,133601,4}
{74,178015,104335,1}
{75,90139,73680,2}
{76,112179,106598,2}
{77,67815,117760,3}
{78,208404,85685,1}
{79,30781,122719,8}
{80,201924,123529,1}
{81,75915,78395,2}
{82,211844,73435,1}
{83,10941,138409,26}
{84,12996,146057,22}
{85,147385,139855,1}
{86,145321,7530,1}
{87,17124,137791,16}
{88,170889,136193,1}
{89,118621,34696,1}
{90,121660,83925,1}
{91,164811,37735,1}
{92,32319,127076,8}
{93,129611,131476,2}
{94,39779,127746,6}
{95,230519,110928,1}
{96,31116,119591,8}
{97,152551,129337,1}
{98,137239,23214,1}
{99,61740,114025,4}
{100,61599,132935,4}
{101,139636,113461,1}
{102,148885,26175,1}
{103,43101,122710,6}
{104,69769,135896,4}
{105,13965,143180,20}
{106,210969,136120,1}
{107,75236,74849,2}
{108,209421,75623,1}
{109,17061,133798,16}
{110,123341,139178,2}
{111,80409,107504,3}
{112,44684,133723,6}
{113,76461,134381,3}
{114,162821,95002,1}
{115,103644,67819,2}
{116,19521,139469,14}
{117,182660,133825,1}
{118,104511,48835,1}
{119,175819,55676,1}
{120,40044,120143,6}
{121,175951,120121,1}
{122,104335,55830,1}
{123,183276,48505,1}
{124,18069,134771,15}
{125,160881,136264,1}
{126,129716,24617,1}
{127,80399,105099,3}
{128,36719,136098,7}
{129,186540,120935,1}
{130,92049,65605,2}
{131,80764,118493,3}
{132,76131,123799,3}
{133,138379,104594,1}
{134,146940,33785,1}
{135,59009,113155,4}
{136,108036,122881,2}
{137,118389,93191,2}
{138,7244,143587,40}
{139,14949,146173,19}
{140,165229,137858,1}
{141,125436,27371,1}
{142,94535,98065,2}
{143,139556,91005,1}
{144,136989,48551,1}
{145,99669,88438,2}
{146,92065,110900,2}
{147,175009,73230,1}
{148,63516,101779,3}
{149,214039,88769,1}
{150,27015,125270,10}
{151,17939,144880,16}
{152,70791,142144,4}
{153,22435,141020,12}
{154,224631,128200,1}
{155,54204,96431,4}
{156,128815,120385,2}
{157,20484,137245,13}
{158,235389,129047,1}
{159,43189,106342,5}
{160,219084,109603,1}
{161,43311,109481,5}
{162,231119,107074,1}
{163,26340,124045,10}
{164,78019,139355,3}
{165,160299,94702,1}
{166,107124,65597,1}
{167,184369,41527,1}
{168,5809,142842,49}
{169,235476,141799,1}
{170,53931,93677,4}
{171,121996,122047,2}
{172,54135,121945,4}
{173,231396,94595,1}
{174,11929,136801,23}
{175,213801,137566,1}
{176,73260,76235,3}
{177,11871,143545,24}
{178,125724,141359,2}
{179,74411,110089,3}
{180,116559,113144,2}
{181,60751,119974,4}
{182,104335,123030,2}
{183,135531,85640,1}
{184,140084,49891,1}
{185,95229,90193,2}
{186,119940,100265,2}
{187,16529,139615,17}
{188,89969,141378,3}
{189,55076,128529,4}
{190,236985,91775,1}
{191,1641,145210,177}
{192,230436,145247,1}
{193,61699,85189,3}
{194,186279,99908,1}
{195,75236,86371,3}
{196,27381,139337,10}
{197,123876,134473,2}
{198,69769,113279,3}
{199,175629,96028,1}
{200,86196,79601,2}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2020-10-28 13:42:26 | 显示全部楼层
强烈反对没注释、没缩进、多层嵌套、一行的任何语言的代码!
你看看你占了多少条!
没注释、没缩进
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2020-10-28 19:33:09 | 显示全部楼层
本帖最后由 wsc810 于 2020-10-28 19:41 编辑

代码修改好了,这样更简洁

  1. Clear[ t, a]; d = 5*(2^32 + 1); pell = -1; P[0] = 5; Q[0] = 11;
  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] || i > 100, 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]}
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2020-10-28 20:36:57 | 显示全部楼层
本帖最后由 wsc810 于 2020-10-28 20:42 编辑
mathematica 发表于 2020-10-28 13:42
强烈反对没注释、没缩进、多层嵌套、一行的任何语言的代码!
你看看你占了多少条!
没注释、没缩进


代码就是连分式展开 (P+Sqrt[d])/Q  ,遇到迭代中点或者超过迭代长度即结束。迭代过程中 P 等于事先预定值

pell 时也结束,以上方法也可以用于因子分解 ,假设待分解的数等于n=Q*Q'  ,加上一个数 P^2,就可以得到d ,  

任取迭代起点 Q0,P0,  运算过程中如果 P=pell ,此时正好将 n 分解

示例

n=12079;
  1. Clear[t, a]; d = 12079 + pell^2; pell = 202; P[0] = 8; Q[0] = 13;
复制代码

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-12-22 19:00 , Processed in 0.025647 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表