Project Euler #9: Special Pythagorean triplet

Sort by

recency

|

167 Discussions

|

  • + 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;
        }
    }
    
  • + 0 comments

    where is the mistake in my code if any one find it please tell me

    include

    using namespace std;

    int main(){ long long n,m,x=0; cin>>n; while(n--){ cin>>m; long long a,b,c; for(a=m/2;a>2;a--){ for(b=m/3;b>1;b--){ c=m-(a+b); if(c>0){ if(a*a == b*b + c*c){ cout<