找回密码
 欢迎注册
查看: 4007|回复: 42

[求助] 如何高效产生本原勾股数组?

[复制链接]
发表于 2023-12-7 08:53:48 | 显示全部楼层 |阅读模式

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

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

×
比如3 4 5和5 12 13都是勾股数组,且互质,
那么我就想问如何高效产生这些数组呢?
比如产生边长都<=1000的勾股本原数组,
按照(a,b,c)其中a<=b<=c的方式排列!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-7 09:47:28 | 显示全部楼层
  1. Clear["Global`*"];(*清除所有变量*)
  2. aa=Flatten[#,1]&@Table[{i,j},{i,1,1000,2},{j,1,i,2}];(*产生二元数组(都是奇数),并且第一个元素大于等于第二个*)
  3. bb=Select[aa,GCD@@#==1&];(*选择互质的*)
  4. cc={#1*#2,(#1^2-#2^2)/2,(#1^2+#2^2)/2}&@@@bb;(*产生勾股数*)
  5. dd=Sort[#]&/@cc;(*排序,从小到大*)
  6. ee=Select[dd,#[[3]]<=1000&](*最大的小于1000*)
复制代码



求解结果
{{0, 1, 1}, {3, 4, 5}, {5, 12, 13}, {8, 15, 17}, {7, 24, 25}, {20, 21,
   29}, {12, 35, 37}, {9, 40, 41}, {28, 45, 53}, {16, 63, 65}, {11,
  60, 61}, {33, 56, 65}, {48, 55, 73}, {36, 77, 85}, {20, 99,
  101}, {13, 84, 85}, {39, 80, 89}, {65, 72, 97}, {60, 91, 109}, {44,
  117, 125}, {24, 143, 145}, {15, 112, 113}, {88, 105, 137}, {52, 165,
   173}, {28, 195, 197}, {17, 144, 145}, {51, 140, 149}, {85, 132,
  157}, {119, 120, 169}, {104, 153, 185}, {84, 187, 205}, {60, 221,
  229}, {32, 255, 257}, {19, 180, 181}, {57, 176, 185}, {95, 168,
  193}, {133, 156, 205}, {140, 171, 221}, {120, 209, 241}, {96, 247,
  265}, {68, 285, 293}, {36, 323, 325}, {21, 220, 221}, {105, 208,
  233}, {160, 231, 281}, {136, 273, 305}, {76, 357, 365}, {40, 399,
  401}, {23, 264, 265}, {69, 260, 269}, {115, 252, 277}, {161, 240,
  289}, {207, 224, 305}, {204, 253, 325}, {180, 299, 349}, {152, 345,
  377}, {120, 391, 409}, {84, 437, 445}, {44, 483, 485}, {25, 312,
  313}, {75, 308, 317}, {175, 288, 337}, {225, 272, 353}, {252, 275,
  373}, {228, 325, 397}, {168, 425, 457}, {132, 475, 493}, {92, 525,
  533}, {48, 575, 577}, {27, 364, 365}, {135, 352, 377}, {189, 340,
  389}, {297, 304, 425}, {280, 351, 449}, {220, 459, 509}, {184, 513,
  545}, {100, 621, 629}, {52, 675, 677}, {29, 420, 421}, {87, 416,
  425}, {145, 408, 433}, {203, 396, 445}, {261, 380, 461}, {319, 360,
  481}, {336, 377, 505}, {308, 435, 533}, {276, 493, 565}, {240, 551,
  601}, {200, 609, 641}, {156, 667, 685}, {108, 725, 733}, {56, 783,
  785}, {31, 480, 481}, {93, 476, 485}, {155, 468, 493}, {217, 456,
  505}, {279, 440, 521}, {341, 420, 541}, {396, 403, 565}, {368, 465,
  593}, {336, 527, 625}, {300, 589, 661}, {260, 651, 701}, {216, 713,
  745}, {168, 775, 793}, {116, 837, 845}, {60, 899, 901}, {33, 544,
  545}, {165, 532, 557}, {231, 520, 569}, {429, 460, 629}, {400, 561,
  689}, {364, 627, 725}, {280, 759, 809}, {232, 825, 857}, {124, 957,
  965}, {35, 612, 613}, {105, 608, 617}, {315, 572, 653}, {385, 552,
  673}, {455, 528, 697}, {468, 595, 757}, {432, 665, 793}, {348, 805,
  877}, {248, 945, 977}, {37, 684, 685}, {111, 680, 689}, {185, 672,
  697}, {259, 660, 709}, {333, 644, 725}, {407, 624, 745}, {481, 600,
  769}, {555, 572, 797}, {540, 629, 829}, {504, 703, 865}, {464, 777,
  905}, {420, 851, 949}, {372, 925, 997}, {39, 760, 761}, {195, 748,
  773}, {273, 736, 785}, {429, 700, 821}, {616, 663, 905}, {580, 741,
  941}, {41, 840, 841}, {123, 836, 845}, {205, 828, 853}, {287, 816,
  865}, {369, 800, 881}, {451, 780, 901}, {533, 756, 925}, {615, 728,
  953}, {696, 697, 985}, {43, 924, 925}, {129, 920, 929}, {215, 912,
  937}, {301, 900, 949}, {387, 884, 965}, {473, 864, 985}}

一共159组

点评

nyy
代码有漏洞!  发表于 2023-12-7 10:02
nyy
应该是158,第一组不是勾股数  发表于 2023-12-7 10:01
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-7 11:25:50 | 显示全部楼层
nyy 发表于 2023-12-7 09:47
求解结果
{{0, 1, 1}, {3, 4, 5}, {5, 12, 13}, {8, 15, 17}, {7, 24, 25}, {20, 21,
   29}, {12, 35,  ...

修正后的代码
  1. Clear["Global`*"];(*清除所有变量*)
  2. aa=Flatten[#,1]&@Table[{i,j},{i,1,1000,2},{j,1,i,2}];(*产生二元数组(都是奇数),并且第一个元素大于等于第二个*)
  3. bb=Select[aa,GCD@@#==1&&(#[[1]]>#[[2]])&];(*选择互质并且前者大的*)
  4. cc={#1*#2,(#1^2-#2^2)/2,(#1^2+#2^2)/2}&@@@bb;(*产生勾股数*)
  5. dd=Sort[#]&/@cc;(*排序,从小到大*)
  6. ee=Select[dd,#[[3]]<=1000&](*最大的小于1000*)
复制代码


输出结果
  1. {{3,4,5},
  2. {5,12,13},
  3. {8,15,17},
  4. {7,24,25},
  5. {20,21,29},
  6. {12,35,37},
  7. {9,40,41},
  8. {28,45,53},
  9. {16,63,65},
  10. {11,60,61},
  11. {33,56,65},
  12. {48,55,73},
  13. {36,77,85},
  14. {20,99,101},
  15. {13,84,85},
  16. {39,80,89},
  17. {65,72,97},
  18. {60,91,109},
  19. {44,117,125},
  20. {24,143,145},
  21. {15,112,113},
  22. {88,105,137},
  23. {52,165,173},
  24. {28,195,197},
  25. {17,144,145},
  26. {51,140,149},
  27. {85,132,157},
  28. {119,120,169},
  29. {104,153,185},
  30. {84,187,205},
  31. {60,221,229},
  32. {32,255,257},
  33. {19,180,181},
  34. {57,176,185},
  35. {95,168,193},
  36. {133,156,205},
  37. {140,171,221},
  38. {120,209,241},
  39. {96,247,265},
  40. {68,285,293},
  41. {36,323,325},
  42. {21,220,221},
  43. {105,208,233},
  44. {160,231,281},
  45. {136,273,305},
  46. {76,357,365},
  47. {40,399,401},
  48. {23,264,265},
  49. {69,260,269},
  50. {115,252,277},
  51. {161,240,289},
  52. {207,224,305},
  53. {204,253,325},
  54. {180,299,349},
  55. {152,345,377},
  56. {120,391,409},
  57. {84,437,445},
  58. {44,483,485},
  59. {25,312,313},
  60. {75,308,317},
  61. {175,288,337},
  62. {225,272,353},
  63. {252,275,373},
  64. {228,325,397},
  65. {168,425,457},
  66. {132,475,493},
  67. {92,525,533},
  68. {48,575,577},
  69. {27,364,365},
  70. {135,352,377},
  71. {189,340,389},
  72. {297,304,425},
  73. {280,351,449},
  74. {220,459,509},
  75. {184,513,545},
  76. {100,621,629},
  77. {52,675,677},
  78. {29,420,421},
  79. {87,416,425},
  80. {145,408,433},
  81. {203,396,445},
  82. {261,380,461},
  83. {319,360,481},
  84. {336,377,505},
  85. {308,435,533},
  86. {276,493,565},
  87. {240,551,601},
  88. {200,609,641},
  89. {156,667,685},
  90. {108,725,733},
  91. {56,783,785},
  92. {31,480,481},
  93. {93,476,485},
  94. {155,468,493},
  95. {217,456,505},
  96. {279,440,521},
  97. {341,420,541},
  98. {396,403,565},
  99. {368,465,593},
  100. {336,527,625},
  101. {300,589,661},
  102. {260,651,701},
  103. {216,713,745},
  104. {168,775,793},
  105. {116,837,845},
  106. {60,899,901},
  107. {33,544,545},
  108. {165,532,557},
  109. {231,520,569},
  110. {429,460,629},
  111. {400,561,689},
  112. {364,627,725},
  113. {280,759,809},
  114. {232,825,857},
  115. {124,957,965},
  116. {35,612,613},
  117. {105,608,617},
  118. {315,572,653},
  119. {385,552,673},
  120. {455,528,697},
  121. {468,595,757},
  122. {432,665,793},
  123. {348,805,877},
  124. {248,945,977},
  125. {37,684,685},
  126. {111,680,689},
  127. {185,672,697},
  128. {259,660,709},
  129. {333,644,725},
  130. {407,624,745},
  131. {481,600,769},
  132. {555,572,797},
  133. {540,629,829},
  134. {504,703,865},
  135. {464,777,905},
  136. {420,851,949},
  137. {372,925,997},
  138. {39,760,761},
  139. {195,748,773},
  140. {273,736,785},
  141. {429,700,821},
  142. {616,663,905},
  143. {580,741,941},
  144. {41,840,841},
  145. {123,836,845},
  146. {205,828,853},
  147. {287,816,865},
  148. {369,800,881},
  149. {451,780,901},
  150. {533,756,925},
  151. {615,728,953},
  152. {696,697,985},
  153. {43,924,925},
  154. {129,920,929},
  155. {215,912,937},
  156. {301,900,949},
  157. {387,884,965},
  158. {473,864,985}}
复制代码

点评

nyy
第三列不是按升序来的  发表于 2023-12-7 13:30
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-7 11:38:05 | 显示全部楼层
  1. Clear["Global`*"];(*清除所有变量*)
  2. max=1000;(*边长的最大值*)
  3. aa1=Flatten[#,1]&@Table[{i,j},{i,1,max,2},{j,2,i,2}];(*一个奇数,一个偶数,前者大*)
  4. aa2=Flatten[#,1]&@Table[{i,j},{i,2,max,2},{j,1,i,2}];(*一个偶数,一个奇数,前者大*)
  5. aa=Join[aa1,aa2];(*数据合并*)
  6. bb=Select[aa,GCD@@#==1&];(*选择互质*)
  7. cc={2*#1*#2,(#1^2-#2^2),(#1^2+#2^2)}&@@@bb;(*产生勾股数*)
  8. dd=Sort[#]&/@cc;(*排序,从小到大*)
  9. ee=Select[dd,#[[3]]<=max&](*最大的小于1000*)
复制代码



勾股数组

输出结果
  1. {{5, 12, 13}, {20, 21, 29}, {9, 40, 41}, {28, 45, 53}, {33, 56,
  2.   65}, {13, 84, 85}, {36, 77, 85}, {65, 72, 97}, {17, 144, 145}, {44,
  3.   117, 125}, {88, 105, 137}, {85, 132, 157}, {57, 176, 185}, {21, 220,
  4.    221}, {52, 165, 173}, {104, 153, 185}, {133, 156, 205}, {105, 208,
  5.   233}, {69, 260, 269}, {25, 312, 313}, {60, 221, 229}, {120, 209,
  6.   241}, {161, 240, 289}, {29, 420, 421}, {68, 285, 293}, {136, 273,
  7.   305}, {204, 253, 325}, {225, 272, 353}, {189, 340, 389}, {145, 408,
  8.   433}, {93, 476, 485}, {33, 544, 545}, {76, 357, 365}, {152, 345,
  9.   377}, {228, 325, 397}, {297, 304, 425}, {261, 380, 461}, {217, 456,
  10.   505}, {165, 532, 557}, {105, 608, 617}, {37, 684, 685}, {84, 437,
  11.   445}, {168, 425, 457}, {336, 377, 505}, {341, 420, 541}, {185, 672,
  12.   697}, {41, 840, 841}, {92, 525, 533}, {184, 513, 545}, {276, 493,
  13.   565}, {368, 465, 593}, {429, 460, 629}, {385, 552, 673}, {333, 644,
  14.   725}, {273, 736, 785}, {205, 828, 853}, {129, 920, 929}, {100, 621,
  15.   629}, {200, 609, 641}, {300, 589, 661}, {400, 561, 689}, {481, 600,
  16.   769}, {429, 700, 821}, {369, 800, 881}, {301, 900, 949}, {108, 725,
  17.   733}, {216, 713, 745}, {432, 665, 793}, {540, 629, 829}, {533, 756,
  18.   925}, {473, 864, 985}, {116, 837, 845}, {232, 825, 857}, {348, 805,
  19.   877}, {464, 777, 905}, {580, 741, 941}, {696, 697, 985}, {124, 957,
  20.   965}, {248, 945, 977}, {372, 925, 997}, {3, 4, 5}, {8, 15, 17}, {7,
  21.   24, 25}, {12, 35, 37}, {11, 60, 61}, {16, 63, 65}, {48, 55,
  22.   73}, {39, 80, 89}, {15, 112, 113}, {20, 99, 101}, {60, 91,
  23.   109}, {51, 140, 149}, {19, 180, 181}, {24, 143, 145}, {119, 120,
  24.   169}, {95, 168, 193}, {23, 264, 265}, {28, 195, 197}, {84, 187,
  25.   205}, {140, 171, 221}, {115, 252, 277}, {75, 308, 317}, {27, 364,
  26.   365}, {32, 255, 257}, {96, 247, 265}, {160, 231, 281}, {207, 224,
  27.   305}, {175, 288, 337}, {135, 352, 377}, {87, 416, 425}, {31, 480,
  28.   481}, {36, 323, 325}, {180, 299, 349}, {252, 275, 373}, {203, 396,
  29.   445}, {155, 468, 493}, {35, 612, 613}, {40, 399, 401}, {120, 391,
  30.   409}, {280, 351, 449}, {319, 360, 481}, {279, 440, 521}, {231, 520,
  31.   569}, {111, 680, 689}, {39, 760, 761}, {44, 483, 485}, {132, 475,
  32.   493}, {220, 459, 509}, {308, 435, 533}, {396, 403, 565}, {315, 572,
  33.   653}, {259, 660, 709}, {195, 748, 773}, {123, 836, 845}, {43, 924,
  34.   925}, {48, 575, 577}, {240, 551, 601}, {336, 527, 625}, {455, 528,
  35.   697}, {407, 624, 745}, {287, 816, 865}, {215, 912, 937}, {52, 675,
  36.   677}, {156, 667, 685}, {260, 651, 701}, {364, 627, 725}, {468, 595,
  37.   757}, {555, 572, 797}, {451, 780, 901}, {387, 884, 965}, {56, 783,
  38.   785}, {168, 775, 793}, {280, 759, 809}, {504, 703, 865}, {616, 663,
  39.   905}, {615, 728, 953}, {60, 899, 901}, {420, 851, 949}}
复制代码

点评

nyy
第三列不升序  发表于 2023-12-7 13:30
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2023-12-7 15:54:00 | 显示全部楼层
  1. Clear["Global`*"];SortBy[Select[Flatten[Table[Sort@{m^2-n^2,2 m n,m^2+n^2},{n,21},{m,n+1,Sqrt[1000-n^2]}],1],GCD[#[[1]],#[[2]]]==1&],First]
复制代码

点评

nyy
你的代码比我快太多了  发表于 2023-12-8 13:43
GCD@@#==1&可以,GCD@#==1&应该不行  发表于 2023-12-8 10:50
1000^2-n^2返回100万的数据了  发表于 2023-12-8 10:49
nyy
1000-n^2,应该是1000^2-n^2?  发表于 2023-12-8 10:28
nyy
GCD[#[[1]],#[[2]]]==1&,应该能修改成GCD@@#==1&,GCD@#==1&估计也行  发表于 2023-12-8 09:54
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-8 13:30:21 | 显示全部楼层

你的代码比我快多了!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-8 13:42:16 | 显示全部楼层
  1. Clear["Global`*"];(*清除所有变量*)
  2. max=2000;(*边长的最大值*)
  3. nmax=1/2*(-1+Sqrt[-1+2*max]);(*由n^2+(n+1)^2<=max计算出来的n的最大值*)
  4. aa=Flatten[#,1]&@Table[Sort@{m^2-n^2,2*m*n,m^2+n^2},{n,1,nmax},{m,n+1,Sqrt[max-n^2]}];(*产生勾股数*)
  5. bb=Select[aa,GCD@@#==1&];(*选择互质*)
  6. cc=Sort[bb,#1[[3]]<#2[[3]]&](*按照第三列升序排列*)
复制代码


把你的代码肢解一下,并且添加上注释,这样更容易看明白、更好维护

  1. {{3,4,5},{5,12,13},{8,15,17},{7,24,25},{20,21,29},{12,35,37},{9,40,41},{28,45,53},{11,60,61},{33,56,65},{16,63,65},{48,55,73},{13,84,85},{36,77,85},{39,80,89},{65,72,97},{20,99,101},{60,91,109},{15,112,113},{44,117,125},{88,105,137},{17,144,145},{24,143,145},{51,140,149},{85,132,157},{119,120,169},{52,165,173},{19,180,181},{57,176,185},{104,153,185},{95,168,193},{28,195,197},{133,156,205},{84,187,205},{21,220,221},{140,171,221},{60,221,229},{105,208,233},{120,209,241},{32,255,257},{23,264,265},{96,247,265},{69,260,269},{115,252,277},{160,231,281},{161,240,289},{68,285,293},{207,224,305},{136,273,305},{25,312,313},{75,308,317},{204,253,325},{36,323,325},{175,288,337},{180,299,349},{225,272,353},{27,364,365},{76,357,365},{252,275,373},{135,352,377},{152,345,377},{189,340,389},{228,325,397},{40,399,401},{120,391,409},{29,420,421},{87,416,425},{297,304,425},{145,408,433},{203,396,445},{84,437,445},{280,351,449},{168,425,457},{261,380,461},{31,480,481},{319,360,481},{93,476,485},{44,483,485},{155,468,493},{132,475,493},{217,456,505},{336,377,505},{220,459,509},{279,440,521},{308,435,533},{92,525,533},{341,420,541},{33,544,545},{184,513,545},{165,532,557},{396,403,565},{276,493,565},{231,520,569},{48,575,577},{368,465,593},{240,551,601},{35,612,613},{105,608,617},{336,527,625},{429,460,629},{100,621,629},{200,609,641},{315,572,653},{300,589,661},{385,552,673},{52,675,677},{37,684,685},{156,667,685},{111,680,689},{400,561,689},{185,672,697},{455,528,697},{260,651,701},{259,660,709},{333,644,725},{364,627,725},{108,725,733},{407,624,745},{216,713,745},{468,595,757},{39,760,761},{481,600,769},{195,748,773},{273,736,785},{56,783,785},{432,665,793},{168,775,793},{555,572,797},{280,759,809},{429,700,821},{540,629,829},{41,840,841},{123,836,845},{116,837,845},{205,828,853},{232,825,857},{287,816,865},{504,703,865},{348,805,877},{369,800,881},{451,780,901},{60,899,901},{616,663,905},{464,777,905},{43,924,925},{533,756,925},{129,920,929},{215,912,937},{580,741,941},{301,900,949},{420,851,949},{615,728,953},{387,884,965},{124,957,965},{248,945,977},{473,864,985},{696,697,985},{372,925,997},{559,840,1009},{45,1012,1013},{660,779,1021},{496,897,1025},{64,1023,1025},{192,1015,1033},{315,988,1037},{645,812,1037},{320,999,1049},{620,861,1061},{731,780,1069},{495,952,1073},{448,975,1073},{132,1085,1093},{585,928,1097},{47,1104,1105},{744,817,1105},{576,943,1105},{264,1073,1105},{141,1100,1109},{235,1092,1117},{329,1080,1129},{423,1064,1145},{704,903,1145},{528,1025,1153},{765,868,1157},{68,1155,1157},{517,1044,1165},{204,1147,1165},{340,1131,1181},{611,1020,1189},{660,989,1189},{832,855,1193},{49,1200,1201},{147,1196,1205},{476,1107,1205},{245,1188,1213},{705,992,1217},{140,1221,1229},{612,1075,1237},{441,1160,1241},{280,1209,1241},{799,960,1249},{539,1140,1261},{420,1189,1261},{748,1035,1277},{637,1116,1285},{893,924,1285},{560,1161,1289},{72,1295,1297},{51,1300,1301},{255,1288,1313},{735,1088,1313},{360,1271,1321},{357,1276,1325},{884,987,1325},{833,1056,1345},{504,1247,1345},{561,1240,1361},{840,1081,1369},{148,1365,1373},{931,1020,1381},{663,1216,1385},{296,1353,1385},{53,1404,1405},{444,1333,1405},{159,1400,1409},{265,1392,1417},{792,1175,1417},{371,1380,1429},{592,1305,1433},{477,1364,1445},{76,1443,1445},{228,1435,1453},{583,1344,1465},{936,1127,1465},{740,1269,1469},{380,1419,1469},{969,1120,1481},{689,1320,1489},{532,1395,1493},{55,1512,1513},{888,1225,1513},{165,1508,1517},{795,1292,1517},{684,1363,1525},{156,1517,1525},{385,1488,1537},{312,1505,1537},{901,1260,1549},{495,1472,1553},{1036,1173,1565},{836,1323,1565},{1007,1224,1585},{624,1457,1585},{715,1428,1597},{80,1599,1601},{240,1591,1609},{988,1275,1613},{780,1421,1621},{57,1624,1625},{1113,1184,1625},{285,1612,1637},{399,1600,1649},{560,1551,1649},{935,1368,1657},{1140,1219,1669},{720,1519,1681},{627,1564,1685},{164,1677,1685},{1045,1332,1693},{328,1665,1697},{741,1540,1709},{1092,1325,1717},{492,1645,1717},{880,1479,1721},{1155,1292,1733},{59,1740,1741},{177,1736,1745},{656,1617,1745},{295,1728,1753},{413,1716,1765},{84,1763,1765},{969,1480,1769},{1040,1431,1769},{1248,1265,1777},{531,1700,1781},{820,1581,1781},{420,1739,1789},{649,1680,1801},{767,1656,1825},{984,1537,1825},{885,1628,1853},{172,1845,1853},{61,1860,1861},{183,1856,1865},{344,1833,1865},{305,1848,1873},{1148,1485,1877},{427,1836,1885},{1003,1596,1885},{924,1643,1885},{516,1813,1885},{1311,1360,1889},{549,1820,1901},{688,1785,1913},{671,1800,1921},{1121,1560,1921},{1092,1595,1933},{1312,1425,1937},{88,1935,1937},{793,1776,1945},{264,1927,1945},{860,1749,1949},{1239,1520,1961},{440,1911,1961},{915,1748,1973},{63,1984,1985},{616,1887,1985},{1032,1705,1993},{315,1972,1997}}
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-8 14:35:16 | 显示全部楼层

简单换一个思路,

  1. Clear["Global`*"];(*清除所有变量*)
  2. max=1000;(*边长的最大值*)
  3. jmax=-1+Sqrt[-1+max];(*由j^2+(j+2)^2<=2*max计算出j的最大值*)
  4. aa=Flatten[#,1]&@Table[Sort@{(i*j),(i^2-j^2)/2,(i^2+j^2)/2},{j,1,jmax,2},{i,j+2,Sqrt[2*max-j^2],2}];
  5. bb=Select[aa,GCD@@#==1&];(*选择互质*)
  6. cc=Sort[bb,#1[[3]]<#2[[3]]&](*按照第三列升序排列*)
复制代码


应该比你的代码稍微快一点点

点评

nyy
不要安慰我了,我知道只是快了一点点而已!  发表于 2023-12-8 14:45
嗯,快多了  发表于 2023-12-8 14:43
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2023-12-8 14:42:37 | 显示全部楼层

计算到两百万吧
上代码

  1. Clear["Global`*"];(*清除所有变量*)
  2. Timing[
  3.     max=2*10^6;(*边长的最大值*)
  4.     nmax=1/2*(-1+Sqrt[-1+2*max]);(*由n^2+(n+1)^2<=max计算出来的n的最大值*)
  5.     aa=Flatten[#,1]&@Table[Sort@{m^2-n^2,2*m*n,m^2+n^2},{n,1,nmax},{m,n+1,Sqrt[max-n^2]}];(*产生勾股数*)
  6.     bb=Select[aa,GCD@@#==1&];(*选择互质*)
  7.     cc=Sort[bb,#1[[3]]<#2[[3]]&](*按照第三列升序排列*)
  8. ]

  9. Clear["Global`*"];(*清除所有变量*)
  10. Timing[
  11.     max=2*10^6;(*边长的最大值*)
  12.     jmax=-1+Sqrt[-1+max];(*由j^2+(j+2)^2<=2*max计算出j的最大值*)
  13.     aa=Flatten[#,1]&@Table[Sort@{(i*j),(i^2-j^2)/2,(i^2+j^2)/2},{j,1,jmax,2},{i,j+2,Sqrt[2*max-j^2],2}];
  14.     bb=Select[aa,GCD@@#==1&];(*选择互质*)
  15.     cc=Sort[bb,#1[[3]]<#2[[3]]&](*按照第三列升序排列*)
  16. ]
复制代码


输出时间
7.05125 这个是你的代码的时间
5.78764 这个是我修改后的代码的时间
从这个上来看,还是节省了一点时间的,我感觉我的代码就比你好一点点

点评

nyy
第一个是你的,第二个是我改写的  发表于 2023-12-8 14:42
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2023-12-9 15:34:35 | 显示全部楼层
nyy 发表于 2023-12-8 14:42
计算到两百万吧
上代码

这个还可以快上几倍,勾股数组的斜边的通项公式

ClearAll["Global`*"];(*清除所有变量*)
Timing[
max=2*10^6;SortBy[Sort/@Flatten[Table[{m^2-n^2,2m n,m^2+n^2},{n,(Sqrt[2max-1]-1)/2},{m,Select[Range[n+1,Sqrt[max-n^2]],GCD[#,n]==1&&OddQ@(n+#)&]}],1],Last]]

点评

nyy
没看懂5楼的意思  发表于 2023-12-9 21:31
nyy
我好像看明白什么意思了  发表于 2023-12-9 21:30
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

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

GMT+8, 2024-11-21 17:41 , Processed in 0.028928 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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