Project Euler #9: Special Pythagorean triplet

  • + 0 comments

    C# code :

    using System;

    public class Solution { public static void Main(string[] args) { int t = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < t; i++) {
            long n = Convert.ToInt64(Console.ReadLine());
            Console.WriteLine(PythagoreanTriplet(n));
        }
    }
    
    public static long PythagoreanTriplet(long n) {
        long result = -1;
    
        for (long k = 2; k <= n / 3; k++) {
            long j = ((n * n) - (2 * n * k)) / (2 * (n - k));
            long i = n - k - j;
    
            if (k * k + j * j == i * i) {
                result = Math.Max(result, k * j * i);
            }
        }
    
        return result;
    }
    

    }