Project Euler #9: Special Pythagorean triplet

Sort by

recency

|

168 Discussions

|

  • + 1 comment

    We know that c = n - a - b

    But also we know that a^2 + b^2 = c^2

    So after substutions we can calculate relation between a and b given that n=const.

    So we can substitute everything into a*b*c and get some complicated function. After some more math magic on paper I've came up with neat solution. It's guaranteed that the first solution is already the best one.

    def bound(n):
        return int(n*(1-math.sqrt(2)/2))
    
    def calc_b(n, a):
        return n*(2*a-n)/(2*a-2*n)
    
    def pit(n):
        for a in range(bound(n), 0, -1):
            if (f_b := calc_b(n, a)) == (b := int(f_b)):
                return a*b*(n-a-b)
        return -1
    
  • + 0 comments

    Do they want the primitive triplet or the triplet multiplied by the factor n/(a+b+c)

  • + 0 comments

    Maybe it can still be optimized:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    class Solution {
    
        static void Main(String[] args) {
            int[] products = new int[30001]; //we store repeated inputs here
            
            int t = Convert.ToInt32(Console.ReadLine());
            for(int a0 = 0; a0 < t; a0++)
            {
                int n = Convert.ToInt32(Console.ReadLine());
                int product;
                if (products[n] == 0)
                {
                    products[n] = -1;
                    product = -1;
                    for (int c = n/3; c <= n/2; c++)
                    {
                        for (int b = 1; b < c; b++)
                        {
                            int a = n - c - b;
                            if (a <= b) //we want a <= b < c
                            {
                                if (a * a + b * b == c * c)
                                {
                                    if (a * b * c > product)
                                    {
                                        product = a * b * c;
                                        products[n] = product;
                                    }
                                }
                            }
                        }
                    }
                }
                Console.WriteLine(products[n]);
    
            }
        }
    }
    
  • + 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;
    }
    

    }

  • + 0 comments
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int t = in.nextInt();
            for (int i=0; i<t; i++) {
                long n = in.nextLong();
                System.out.println(pythagoreanTriplet(n));
            }
            in.close();
        }
        
        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;
        }
    }