Tag

c sharp Archives - Mir Imad Ahmed

Project Euler Problem 10 Solution

Project Euler Problem 10 Solution in C#

By | Programming, Project Euler | No Comments

Do you need “Project Euler Problem 10 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 9 C Sharp as well.

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

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 sum of all the prime numbers below two million.

Project Euler Problem 10 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)
        {
            double sum = 0;
            int count = 2;
            while(count < 2000000){
                if(isPrime(count))
                    sum += count;
                count++;
            }
            
            Console.WriteLine(sum);
            
        }
        
        static bool isPrime(int a){
            for(int i = 2; i<=Math.Sqrt(a); i++) 
                if (a % i == 0) 
                    return false;
            return true;
        }
    }
}

 

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

I put a while loop from 2 (because two is the smallest prime number;) ) till count < 2 Million.

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

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

Yaay! We got this right. Thanks for reading.

Happy coding!

Project Euler Problem 9

Project Euler Problem 9 Solution in C#

By | Programming, Project Euler | No Comments

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!