Tag

# programming tutorial Archives - Mir Imad Ahmed

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!

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)
{
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!

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.

Yaay! We got this right. Thanks for reading.

Happy coding!

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.

```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

Yaay! We got this right. Thanks for reading.

project euler problem 7 solution python

Happy coding!

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.

Yaay! We got this right. Thanks for reading.

project euler problem 6 solution python

Happy coding!

# Fibonacci Series in Python

Are you finding it hard to code Fibonacci Series in Python? Let me help you with it here. First of all we will introduce our readers a bit about Fibonacci Series and then continue with the Python Code.

## What is Fibonacci Series?

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. Written as a rule, the expression is xn = xn1 + xn2.

## Code Fibonacci Series in Python

As you can understand from the explanation given above that we need to add two numbers and get the number of sequence then we have to swap and then move forward again.

There are multiple ways of doing this in Python. We will keep it short and simple for our readers. We will code to get sequence of Fibonacci series in python upto a specific number.

```nums = []
a = 0
b = 1
findTill = 10000
while True:
c = a + b
if c >= findTill:
break
nums.append(c)
a = b
b = c
```

With the code above, we are basically starting from 0 and 1 as our starting members and moving up to 10000.

We are first of all adding both numbers and then checking them if they are exceeding our set limit. If they are exceeding we have break statement to finish the loop there. Other wise, we will append it to our list, swap the numbers and move forward.

I have solved Project Euler Problem 2 Solution which includes Fibonacci Series upto 4 million.

Share the word to keep supporting me.