nyy 发表于 2023-12-7 08:53:48

如何高效产生本原勾股数组?

比如3 4 5和5 12 13都是勾股数组,且互质,
那么我就想问如何高效产生这些数组呢?
比如产生边长都<=1000的勾股本原数组,
按照(a,b,c)其中a<=b<=c的方式排列!

nyy 发表于 2023-12-7 09:47:28

Clear["Global`*"];(*清除所有变量*)
aa=Flatten[#,1]&@Table[{i,j},{i,1,1000,2},{j,1,i,2}];(*产生二元数组(都是奇数),并且第一个元素大于等于第二个*)
bb=Select;(*选择互质的*)
cc={#1*#2,(#1^2-#2^2)/2,(#1^2+#2^2)/2}&@@@bb;(*产生勾股数*)
dd=Sort[#]&/@cc;(*排序,从小到大*)
ee=Select]<=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 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,...

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


输出结果
{{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}}

nyy 发表于 2023-12-7 11:38:05

nyy 发表于 2023-12-7 11:25
修正后的代码




Clear["Global`*"];(*清除所有变量*)
max=1000;(*边长的最大值*)
aa1=Flatten[#,1]&@Table[{i,j},{i,1,max,2},{j,2,i,2}];(*一个奇数,一个偶数,前者大*)
aa2=Flatten[#,1]&@Table[{i,j},{i,2,max,2},{j,1,i,2}];(*一个偶数,一个奇数,前者大*)
aa=Join;(*数据合并*)
bb=Select;(*选择互质*)
cc={2*#1*#2,(#1^2-#2^2),(#1^2+#2^2)}&@@@bb;(*产生勾股数*)
dd=Sort[#]&/@cc;(*排序,从小到大*)
ee=Select]<=max&](*最大的小于1000*)



勾股数组

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

northwolves 发表于 2023-12-7 15:54:00

Clear["Global`*"];SortBy}],1],GCD[#[],#[]]==1&],First]

nyy 发表于 2023-12-8 13:30:21

northwolves 发表于 2023-12-7 15:54


你的代码比我快多了!

nyy 发表于 2023-12-8 13:42:16

northwolves 发表于 2023-12-7 15:54


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


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

{{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}}

nyy 发表于 2023-12-8 14:35:16

northwolves 发表于 2023-12-7 15:54


简单换一个思路,

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


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

nyy 发表于 2023-12-8 14:42:37

northwolves 发表于 2023-12-7 15:54


计算到两百万吧
上代码

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

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


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

northwolves 发表于 2023-12-9 15:34:35

nyy 发表于 2023-12-8 14:42
计算到两百万吧
上代码



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

ClearAll["Global`*"];(*清除所有变量*)
Timing[
max=2*10^6;SortBy-1)/2},{m,Select],GCD[#,n]==1&&OddQ@(n+#)&]}],1],Last]]
页: [1] 2
查看完整版本: 如何高效产生本原勾股数组?