美文网首页
和果子一起来做题-Project Euler-09-R语言版本

和果子一起来做题-Project Euler-09-R语言版本

作者: 9d760c7ce737 | 来源:发表于2018-01-28 18:22 被阅读17次

第9题:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

这题就是解方程
肯定能够求出a,b,c

实际上我没有什么思路,我脑子里还是上学那会的想法,想通过二项式变换直接得出abc

但是,不要忘了,这是在进行编程练习,无论是否巧妙,只要是遍历能做的,就交给计算机吧

for (a in 1:500){
  for (b in (a+1):500){
    c = 1000-a-b
    if (a^2 +b^2 == c^2 ){
      return(list(c(a,b,c),a*b*c))
    }
  }
}

最终得到a =200,b = 375, c =425
abc =31875000

相关文章

网友评论

      本文标题:和果子一起来做题-Project Euler-09-R语言版本

      本文链接:https://www.haomeiwen.com/subject/hzszaxtx.html