Category

Project Euler

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!

Project Euler Problem 8

Project Euler Problem 8 Solution in JS

By | Programming, Project Euler | No Comments

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

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

So we have to solve this problem using JS.

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 largest product of the thirteen adjacent digits in the above given numbers.

Project Euler Problem 8 Solution JS | 

Lets start! 

var a = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

var index = 0;
var max = 0;

while(index!=988)
{
    var temp = parseInt(a[index]) * parseInt(a[index+1]) * parseInt(a[index+2]) * parseInt(a[index+3]) * parseInt(a[index+4]) * parseInt(a[index+5]) * parseInt(a[index+6]) * parseInt(a[index+7]) * parseInt(a[index+8]) * parseInt(a[index+9]) * parseInt(a[index+10]) * parseInt(a[index+11]) * parseInt(a[index+12]) ;
    
   	if(temp > max)
    {
    	max = temp;    
    }
    index++;
}


print(max);

We put the big giant number into a string variable. Now lets traverse the string element one by one. Take 13 numbers at one time by adding offset to the current index. Take the loop until we get the last 13 numbers.

Convert the each element after adding the offset into integer because initially it was in string. Now lets multiple all the elements and compare it with a max variable.

We need to update max variable in case if our product is larger then max.

Project Euler Problem 8 Solution JS

Yaay! We got this right. Thanks for reading.

Happy coding!

Project Euler Problem 7

Project Euler Problem 7 Solution in C#

By | Programming, Project Euler | No Comments

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

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

So we have to solve this problem using C#.

Lets first of all open Visual Studio.

If we analyze the problem statement given here, we can see that we are asked to find 10001st Prime Number

Project Euler Problem 7 Solution C# | 

Lets start! 

The first thought came across my mind is Brute Force!

Yes right. So lets create a while loop taking number one plus each time and checking for prime number. If the number is prime just update a counter. And upon reaching 10001th prime number we stop and print the prime number.

A prime number is that dont get divided by 1 or itself. If it gets fully divided by 1 or itself then it is not a prime number.

For big numbers we can use this mathematics rule that if the number does not divide upto its square root then it will never

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

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

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int count = 0;
            int num = 2;
            while(true){
                if(isPrime(num)) count++;
                if(count == 10001) break;
                num++;
            }
            Console.WriteLine(num);
            //Your code goes here
            Console.WriteLine("Hello, world!");
        }
        
        static bool isPrime(int num){
            for (int i = 2; i < num; i++){
                if(num % i == 0) return false;
            }
            return true;
        }
    }
}

Lets see if we got it right.

Project Euler Problem 7 Solution c#

Project Euler Problem 7 Solution c#

Yaay! We got this right. Thanks for reading.

project euler problem 8 solution javascript

Happy coding!

Project euler problem 6

Project Euler Problem 6 Solution in Python

By | Programming, Project Euler | No Comments

Do you need “Project Euler Problem 6 Solution Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 5 Python as well.

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to find difference between sum of squares and square of sum.

Project Euler Problem 6 Solution Python | 

Lets start! 

This is one of the easiest by far I think.

You simply create a list of squares from 1 to 100, sum them and save in a variable.

Now save the sum of 1 to 100 in a variable, square it.

Now print the difference, you get your answer.

num2 = sum([i*i for i in range(1,101)])
num = sum(range(1,101))
num = num * num
print num - num2

 

Lets see if we got it right.

project euler problem 6 solution python

project euler problem 6 solution python

 

Yaay! We got this right. Thanks for reading.

project euler problem 7 solution python

Happy coding!

project euler problem 5 solution python

Project Euler Problem 5 Solution in Python

By | Programming, Project Euler | No Comments

Do you need “Project Euler Problem 5 Solution Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 4 Python as well.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to find smallest number that completely divide all of numbers from 1 to 20.

Project Euler Problem 5 Solution Python | 

Lets start! 

The first thing that comes to my mind is brute force. I think that it is the only and simplest way to find that if a number divides every number from 1-20 or not by checking for each of them.

We make a function that returns true if the number is divisible by all numbers from 1-20 (2,20 technically because every number is divisible by 1 so there is no point of checking it for 1 😉 )

def ifDividesAll(num):
  for i in range(2,21):
    if num % i != 0:
      return False
  return True

Now we do a loop that keep on incrementing the number and check for each using the function ifDividesAll. If the function returns true the loop breaks and we print the number otherwise, it keeps on adding one to the number, check again and again and again…

num = 20
while True:
  if ifDividesAll(num):
    break
  else:
    num = num + 1
print num

Lets see if we got it right.

project euler problem 5 solution python

Yaay! We got this right. Thanks for reading.

project euler problem 6 solution python

Happy coding!

Project Euler Problem 4 Solution in Python

By | Programming, Project Euler | No Comments

Do you need “Project Euler Problem 4 Solution Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 3 Python as well.

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to calculate the largest palindrome number that is created by multiplying two 3 digit numbers.

Project Euler Problem 4 Solution Python | Lets start! 

To be a palindrome number the product answer must be a 6 digit number. So we will brute force from such numbers which result in 6 digit product answer by multiplying the range 100-999 to itself. We then check by converting the resulting number into string and checking if the length is 6.

Then we match first three digits with last three digits in reverse order. If all the three matches we will add the number into a list of palindromes.

At the end we will use Max() function to determine the largest palindrome.

palindromes = []
for x in range(100,999):
    for i in range(100,999):
        num = str(i*x).split()[0]
        if len(num) == 6:
            if num[0] == num[-1] and num[1] == num[-2] and num[2] == num[-3]:
                palindromes.append(i * x)
print max(palindromes)

Now lets display the number. It show that 906609 is the largest palindrome number created by product of two 3 digit numbers.

Lets see if we got it right.

project euler problem 4 solution python

Project Euler Problem 4 Solution

Yaay! We got this right. Thanks for reading.

project euler problem 5 solution python

Happy coding!

Project Euler Problem 3 Solution

Project Euler Problem 3 Solution in Python

By | Programming, Project Euler | No Comments

“Project Euler Problem 3 Solution Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 2 Python as well.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to calculate the largest prime factor of the given number.

Let’s say we call our number as x

We need to find the largest prime factor of x.

Lets start!

We need to start from 2 and keep on checking on different numbers and see it divides the given number fully or not.

If it divide it fully we start again from 2 otherwise we keep on checking by adding one to divisor. At the end we will have the largest factor.

def largest_prime_factor(num, div=2):
    while div < num:
        if num % div == 0 and num/div > 1:
            num = num /div
            div = 2
        else:
            div = div + 1
    return num

Now lets display the number. It show that 6857 is the largest prime factor of x.

Lets check by entering 6857 as answer in project euler website and see if we got it right.

project euler problem 3 solution python

Project Euler Problem 3 Solution

Yaay! We got this right. Thanks for reading.

Here is the Problem 4 Solution.

Happy coding!

Project Euler Problem 2 Solution in Python

Project Euler Problem 2 Solution in Python

By | Programming, Project Euler | No Comments

Project Euler Problem 2 Python

How to solve “Project Euler Problem 2 Python” . We will discuss all the problems in Project Euler and try to solve them using Python. I have solved Project Euler Problem 1 Python as well.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to calculate the sum of those even numbers who complies to the following conditions.

Let’s say we call our number to be summed as x

  • x is even
  • x is in fibonacci series
  • x is less than 4 million

We have to add these “x” numbers.

Lets start!

We need to save the sum of such x numbers into some variable.

sumOfEvenFibonacci  = 0

Now let’s make a list to store all the fibonacci series till 4 million. If you are not sure how we came up with list of Fibonacci Series you can read my this tutorial here How to make Fibonacci Series in Python.

nums = Fibonacci()
We have to iterate the list to find out which are even number then we will add them into our sumOfEvenFibonacci variable.

for i in nums:
  if i % 2 == 0:
    countOfEvenFibonacci = countOfEvenFibonacci + i

 

Now lets display the sum. It show that 4613732 is the number of such elements.

Lets see if we got it right.

Project Euler Problem 2 Solution

Project Euler Problem 2 Solution

Yaay! We got this right. Thanks for reading. Happy coding!

Project Euler Problem 1 Solution

Project Euler Problem 1 Solution in Python

By | Programming, Project Euler | No Comments

Project Euler Problem 1 Python

How to solve “Project Euler Problem 1 Python” . We will discuss all the problems in Project Euler and try to solve them using Python.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

So we have to solve this problem using Python.

I am assuming you have already installed Python 2.7.x. If not, you can download it from here.

Lets first of all open Python IDLE.

If we analyze the problem statement given here, we can see that we are asked to calculate the sum of those natural numbers who complies to the following conditions.

Let’s say we call our natural number to be summed as x

  • x is below 1000
  • x is multiple of 3
  • OR x is multiple of 5

We have to sum these “x” numbers.

Lets start!

We need to save the sum of such x numbers into some variable.

sum  = 0

Now let’s make a list to store all the natural numbers below 1000.

nums = range(1,1000)
We have to iterate the items of this list and see if the number is a multiple of 3 or 5. If it is we will add that number to the sum variable otherwise we will just skip.

 

for i in nums:
   for i in nums:
      if i % 3 == 0 or i % 5 == 0:
         sum = sum + i

 

Now lets display the sum. It show that 233168 is the number of such elements.

Lets see if we got it right.

Project Euler Problem 1 Solution

 

Yaay! We got this right. Thanks for reading. Happy coding!