Project Euler

Project Euler Problem 39

steloflute 2012. 6. 9. 00:08

Problem 39

14 March 2003

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p 1000, is the number of solutions maximised?


Answer:
840

 

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.IO;

namespace Euler {
    class Program {
        static int numRightAngleTriangle(int perimeter) {
            var count = 0;
            for (var a = 1; a < perimeter; a++) {
                for (var b = a; b < perimeter; b++) {
                    var c = perimeter - a - b;
                    if (c < b) continue;
                    if (a * a + b * b == c * c) {
                        count++;
                    }
                }
            }
            return count;

        }
        static void Main(string[] args) {
            var maxNum = 0;
            var maxP = 0;
            for (var p = 1; p <= 1000; p++) {
                var num = numRightAngleTriangle(p);
                if (num > maxNum) {
                    maxNum = num;
                    maxP = p;
                }
            }
            Console.WriteLine(maxP + " " + maxNum);
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 41  (0) 2012.06.09
Project Euler Problem 40  (0) 2012.06.09
Project Euler Problem 38  (0) 2012.06.09
Project Euler Problem 37  (0) 2012.06.09
Project Euler Problem 36  (0) 2012.06.09