project Euler309巷道交叉梯子问题(要求全部整数解)
问题来源:http://projecteuler.net/index.php?section=problems&id=309问题描述(请参考下面的图):
我还是稍微翻译一下,凭借我个人的理解,
在一个巷道里,放置了两个梯子.
这两个梯子的长度都是正整数,
短的梯子的长度是x,
长的梯子的长度是y,(x小于等于y)
两个梯子的交点到地面的距离是h,h也是整数,
此时存在在巷道的宽度w也是整数的情况,因此
要求所有的(x,y,h,w)(这四个量都是整数).
当然,这两个梯子所在的平面垂直于巷道的地面.
我写了一个mathematica的求解的子函数,但是只是求解巷道的宽度等的子函数,
可以使用三重循环(长梯子的宽度,短梯子的宽度,交叉点高度)来穷举,
但是我觉得太慢了,不知道谁有高招!Clear["Global`*"];(*Clearallvariables*)
(*定义子函数求解方程*)
(*子函数的输出量依次为a,b,w,c,d,其意义见图所示*)
mySolu:=
Module[{x=x0,y=y0,h=h0,(*已知的初始值*)
a,b,c,d,w, (*待求解的变量的值*)
eq1,eq2,eq3}, (*中间变量*)
eq1=NSolve[{c/w==h/b,
d/w==h/a,
a+b==w,
c^2+w^2==x^2,
d^2+w^2==y^2
},{a,b,w,c,d},10];
eq2={a,b,w,c,d}/.eq1; (*只保留数据,去掉变量符号*)
eq3=Select=={0,0,0,0,0}&](*提取结果都大于等于零的结果*)
]
mySolu(*用来验证子函数是否正确*)
{{16.0000000, 40.00000000, 56.0000000, 42.00000000, 105.0000000}}
(*上面的结果告诉我们,巷道的宽度是56*) 上面的代码! mySolu
mySolu
mySolu
mySolu
mySolu
{{16.0000000, 40.00000000, 56.0000000, 42.00000000, 105.0000000}}
{{8.7500000, 61.2500000, 70.0000000, 24.00000000, 168.0000000}}
{{26.2500000, 36.75000000, 63.0000000, 60.0000000, 84.0000000}}
{{33.3333333, 46.66666667, 80.0000000, 60.0000000, 84.0000000}}
{{30.0000000, 75.0000000, 105.0000000, 56.0000000, 140.0000000}}
由上面的求解结果直到,巷道的宽度依次是56,70,63,80,105 2# mathematica
没必要,总是发代码的截图,因为它不会给阅读者带来更多的信息。 2# mathematica
没必要,总是发代码的截图,因为它不会给阅读者带来更多的信息。
gxqcn 发表于 2011-4-10 11:36 http://bbs.emath.ac.cn/images/common/back.gif
有必要截图的,如果直接把代码粘贴上去,在论坛上看还是很费劲的,
如果截图了,看起来更好看的,可是如果好看却又不能复制,
所以两者兼考虑一下,我就把代码和图全部传上来了,
当然图还有防止代码被转换错误的功能. {40, {{401, 58, 38}}}
{56, {{119, 70, 30}}}
{63, {{663, 87, 55}, {105, 87, 35}}}
{70, {{182, 74, 21}}}
{80, {{116, 100, 35}}}
{96, {{296, 104, 35}}}
{105, {{273, 175, 90}, {175, 119, 40}}}
{112, {{238, 113, 14}}}
{117, {{533, 195, 120}}}
{119, {{7081, 169, 118}}}
{140, {{364, 175, 80}}}
{144, {{219, 156, 44}}}
{165, {{915, 429, 275}, {429, 187, 72}}}
{168, {{210, 182, 45}}}
{224, {{1799, 476, 340}}}
{225, {{1695, 255, 112}}}
{252, {{1148, 273, 96}, {420, 273, 80}}}
{260, {{1700, 676, 455}}}
{264, {{814, 275, 70}, {561, 286, 90}}}
{275, {{715, 365, 176}}}
{300, {{500, 375, 144}}}
{315, {{1435, 357, 150}}}
{364, {{1300, 455, 224}}}
{385, {{1375, 407, 120}}}
{400, {{1625, 500, 252}}}
{408, {{442, 425, 70}}}
{448, {{3152, 848, 585}}}
{455, {{7969, 481, 153}, {2137, 679, 406}}}
{460, {{1196, 667, 336}}}
{462, {{2562, 770, 495}}}
{483, {{1725, 667, 360}}}
{495, {{3729, 825, 560}, {825, 583, 210}}}
{496, {{2015, 1054, 630}}}
{520, {{1105, 754, 350}, {650, 533, 90}}}
{525, {{1875, 875, 504}}}
{528, {{1628, 660, 315}, {1122, 822, 385}}}
{552, {{1702, 1173, 630}}}
{555, {{4181, 1443, 1008}}}
{576, {{1224, 776, 351}}}
{585, {{4407, 689, 336}, {689, 663, 168}}}
{595, {{3637, 757, 414}, {2125, 1547, 840}, {1547, 1099, 561}}}
{600, {{870, 845, 306}, {750, 625, 126}}}
{612, {{1887, 1020, 560}}}
{616, {{4334, 1309, 910}, {1166, 770, 315}}}
{630, {{6630, 1306, 975}}}
{637, {{28987, 763, 414}}}
{651, {{6851, 1085, 770}}}
{660, {{4972, 1199, 832}}}
{663, {{1937, 1105, 595}}}
{672, {{4728, 840, 455}, {2353, 1022, 574}}}
{675, {{9125, 1125, 819}}}
{680, {{986, 697, 126}}}
{693, {{3843, 707, 135}}}
{728, {{2422, 1547, 858}, {1547, 1378, 630}}}
{735, {{2625, 959, 495}}}
{741, {{4845, 1235, 819}}}
{759, {{1495, 809, 230}}}
{760, {{3838, 950, 495}, {1976, 1615, 800}}}
{765, {{1989, 901, 378}}}
{775, {{9703, 2015, 1560}}}
{792, {{6558, 2442, 1705}, {2442, 1067, 546}}}
{800, {{4040, 1160, 693}}}
{819, {{15981, 3731, 2964}, {15981, 1869, 1520}, {5355, 981, 490}}}
{825, {{1625, 1375, 616}}}
{880, {{1276, 884, 77}, {1100, 979, 260}}}
{918, {{23418, 2682, 2275}}}
{920, {{4646, 943, 198}}}
{931, {{22819, 3325, 2800}}}
{936, {{2886, 2210, 1155}, {1560, 975, 224}}}
{945, {{21273, 2639, 2208}, {1233, 1071, 308}}}
{952, {{1802, 1073, 374}}}
{975, {{1455, 1105, 351}}}
{980, {{4949, 2548, 1584}}}
{992, {{7967, 1240, 680}}} {40, {{401, 58, 38}}}
{56, {{119, 70, 30}}}
{63, {{663, 87, 55}, {105, 87, 35}}}
{70, {{182, 74, 21}}}
{80, {{116, 100, 35}}}
{96, {{296, 104, 35}}}
{105, {{273, 175, 90}, {175, 119, 40}}}
{112 ...
wayne 发表于 2011-4-10 14:15 http://bbs.emath.ac.cn/images/common/back.gif
只有结果的回复没有太大的意义,过程很重要 http://bbs.emath.ac.cn/thread-2048-1-1.html http://bbs.emath.ac.cn/thread-2048-1-1.html
wayne 发表于 2011-4-11 09:22 http://bbs.emath.ac.cn/images/common/back.gif
这个问题我在论坛上也研究过,具体请看
http://bbs.emath.ac.cn/viewthread.php?tid=2987&page=1#pid35308 http://bbs.emath.ac.cn/viewthread.php?tid=2987&page=1&fromuid=865#pid35309
看这个回复吧
页:
[1]