Project Euler Problem 9

Do you need “Project Euler Problem 9 Solution c sharp” . We will discuss all the problems in Project Euler and try to solve them using Python or C#. I have solved Project Euler Problem 8 JS as well.

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^2 + b^2 = c^2
For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

So we have to solve this problem using C#.

Lets first of all open Visual Code.

If we analyze the problem statement given here, we can see that we are asked to find the pythagorean py triplet number.

Project Euler Problem 9 Solution C Sharp| 

Lets start! 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine("Hello, world!");
            for(int a = 1; a < 998; a++)
                for (int b = a + 1; b < 998; b++)
                    for( int c = b + 1; c < 998; c++)
                        if (isPyTriple(a,b,c))
                            if(a + b + c == 1000)
                            {
                                Console.WriteLine(a);
                                Console.WriteLine(b);
                                Console.WriteLine(c);
                                Console.WriteLine(a*b*c);
                            }
            
            
            
            
        }
        
        static bool isPyTriple(int a, int b,int c){
            return (a * a) + (b * b) == (c * c);
        }
    }
}

For such type of problems the first thing that hit my mind was Brute Force! Obviously!

I put a for loop inside a for loop which is also inside a for loop so that we can brute force the three numbers a, b and c.

Then I made a special function to return a boolean by checking the mere condition of PyTriplet.

I checked for each element by brute force and got my answer right there and then.

Yaay! We got this right. Thanks for reading.

Project Euler Problem 10 Solution C#

Happy coding!