Wednesday, April 27, 2011

Problem 004

Problem Statement
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.

Solution
A simple problem with a simple solution. Multiply the numbers from 100 to 999 amongst themselves. Reverse the product and check if the original product is equal to the reversed product. If they are equal then save the product in the output variable. Before saving in the output variable check whether the original value of the output variable is less than the value of the product. If yes only then save the product to the output variable.

Code


Code Explanation
The code can be explained as follows:

1. Obtain the lower range and the upper range.
2. Initialize a loop starting with lower range entered above upto higher range.
3. Initialize a second loop with the exact same parameters as the above loop.
4. Find the product of the two loops and then reverse the product.
5. If product and reverse of product are equal then check store it in the output variable, provided that the value of the product is larger than the value of the output variable.

Short Code


How To Use
The call should be made like with the parameter ?l=100 and ?u=1000. The ?l is to specify the lower range and ?u to specify the upper range.

A sample call can be made like this http://www.Euler_004.php?l=100&?u=1000

Wednesday, April 13, 2011

Problem 003

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

What is the largest prime factor of the number 600851475143 ?

Solution
Find all the factors of the given number and check whether they are prime. To speed up operation check factors only up to the square root of the number.

A factor is found by dividing the number input by the value currently in the loop, if there is no remainder then the value in the loop is the factor. If a factor is found, find the quotient by dividing the input number and the factor.

A prime check has to be made on the factor as well as the quotient. Store the larger value in a variable only if the original value in the variable is less than the value of the factor or the quotient.

Code


Code Explanation
The code can be explained as follows:

1. Obtain the input number.
2. Initialize a loop up to the square root of the input number.
3. Start performing fmod operation to check if the number has any factor.
4. If the number has a factor, then check if the factor is prime, if yes then store factor as the highest prime factor.
5. Divide the number by the factor obtained above and check if the quotient is prime.
6. If yes then store the quotient as the highest prime factor.
7. At the end of the loop if the variable storing highest prime factor is 1 then the number itself is prime.

Short Code


How To Use
The call should be made like with the parameter ?n=600851475143. The ?n is to specify the number whose highest prime factor we intend to find out.

A sample call can be made like this http://www.Euler_003.php?n=600851475143

Tuesday, April 12, 2011

Problem 002

Problem Statement
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.

Solution
The solution here involves generating the Fibonacci sequence and checking if the terms are even. If we find any even term then we increment the counter with that term. We do this for all values of the Fibonacci series below 4 million.

Code


Code Explanation
The code can be explained as follows:

1. Initialize the counter variable to 2.
2. Perform normal Fibonacci generation and check if the term being generated is even.
3. If it is an even term add it to the counter.
4. Perform this operation while the terms are below 4 million.

Short Code


How To Use
The call should be made like with the parameter ?n=4000000. The ?n is to specify we are going to find the sum of even numbers less than 4 million.

A sample call can be made like this http://www.Euler_002.php?n=4000000

Monday, April 11, 2011

Problem 001

Problem Statement
Add all the natural numbers below one thousand that are multiples of 3 or 5.

Solution
This is a simple problem. Loop through all numbers starting from 1 and incrementing in steps of 1. Then check the following conditions:
1. If the number is divisible by 3, then add that number to a counter.
2. If the number is divisible by 5 and not by 15, then add that number to a counter.

The check for divisibility by 15 ensures that the counter is not incremented twice.

Code


Code Explanation
The code can be explained as follows:

1. Begin loop, starting from 1 and going up to the value entered by the user.
2. If the number is divisible by three then increment a counter with the value of that number.
3. If the number is divisible by five and not by fifteen then increment the counter with the value of that number.
4. Output the counter.

Short Code


How To Use
The call should be made like with the parameter ?n=1000. The ?n is to specify we are going to find the range up to 1000. This is general code and can be used to find multiples of 3 and 5 up to any number.

A sample call can be made like this http://www.Euler_001.php?n=1000

Sunday, April 10, 2011

Introduction

This blog has solutions to all the problems that I have solved. The solutions are in PHP. There are two snippets given. The first one is made as short as possible and this is done by sacrificing readability while the second one is the one that should be used if you want to understand what the code is doing. I plan to make a sister site with solutions for all the problems in PERL and Python, but that is after I learn those two.

To run the code and view the results, you can use this website http://www.ideone.com Copy the code from here and execute to obtain the result.