Considérons un triangle rectangle avec une longueur de côté de {a, b, c} et un triplet d'entiers, et soit p la longueur qui l'entoure. Quand p = 120, il y a trois solutions:
   {20,48,52}, {24,45,51}, {30,40,50}
Quel est le nombre maximum de solutions lorsque p ≤ 1000? http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2039
Pour chaque p, j'ai écrit le code pour vérifier si ʻa ^ 2 + b ^ 2est égal à(p-a-b) ^ 2` en changeant a et b. Je l'ai écrit en quelque sorte, mais je pense qu'il vaut mieux l'écrire pour.
def main():
  MAX = 1000
  sq = [x**2 for x in range(MAX)]
  p = MAX-1
  d_max = 0
  ans = 0
  while p > 0:
    (a, b) = (p//2,p//2)
    d = 0
    while a > 0:
      if sq[a]+sq[b] == sq[p-a-b]:
        d+=1
      elif sq[a]+sq[b]< sq[p-a-b]:
        break
      while b > 0:
        if sq[a]+sq[b] == sq[p-a-b]:
          d += 1
        elif sq[a]+sq[b] < sq[p-a-b]:
          break 
        b -=1
      a, b = a-1, a-1
    if d > d_max:
      ans, d_max = p, d
      print ans, d_max
    p -= 1
  print ans
  
main()
        Recommended Posts