Showing posts with label projecteuler. Show all posts
Showing posts with label projecteuler. Show all posts

Friday, June 19, 2009

Project Euler Problem 5

Problem 5

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 number that is evenly divisible by all of the numbers from 1 to 20?


public class ProblemFive {
public static void main(String[] args) {
// Any number will be always divisible by 1
// To be divisible by 2 it has to be an even number
// Number will be greater than or equal to at least 20
int start = 20;

while (true) {
int i = 3;

for (; i <= 20; ++i) {
if (0 != (start % i)) {
break;
}
}

if (i >= 20) {
break;
}

start += 2;
}

System.out.println(start);
}
}

Monday, June 15, 2009

Project Euler Problem 4

Problem 4

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.

The solution is to multiply each three digit number and check if its a palindrome or not.


public class ProblemFour {
public static void main(String[] args) {
int palindrome = 9009;

for (int i = 999; i > 99; --i) {
for (int j = i; j > 99; --j) {
int product = i * j;

if (isPalindrome(String.valueOf(product)) &&
(product > palindrome)) {
palindrome = product;
}
}
}

System.out.println(palindrome);
}

public static boolean isPalindrome(String data) {
char[] array = data.toCharArray();
int reverseIndex = array.length - 1;
int index = 0;

while (index < reverseIndex) {
if (array[index] != array[reverseIndex]) {
return false;
}

++index;
--reverseIndex;
}

return true;
}
}

Friday, May 15, 2009

Project Euler Problem 3

Problem 3

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

What is the largest prime factor of the number 600851475143 ?

The solution is to find a factor and check if its a prime number. Since we need largest prime factor, run the for loop in reverse direction. Also a prime factor of a number cannot be greater than the square root of the number.


public class ProblemThree {
public static void main(String[] args) {
long range = (long) Math.sqrt(600851475143L);

for (long i = range; i > 1; --i) {
if ((0 == (number % i)) && isPrimeNumber(i)) {
System.out.println(i);

break;
}
}
}

public static boolean isPrimeNumber(long number) {
long halfRange = number / 2;

for (long i = 2; i <= halfRange; ++i) {
if (0 == (number % i)) {
return false;
}
}

return true;
}
}

Wednesday, May 13, 2009

Project Euler Problem 2

Today I got some time to look at Euler Problem 2.

Problem 2

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, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

The solution is to run a loop to find the next term in Fibonacci series. Check the evenness of the term and add to the sum.


public class ProblemTwo {
public static void main(String[] args) {
int firstTerm = 0;
int secondTerm = 1;
int newTerm = 0;
int sum = 0;

while (secondTerm <= 4000000) {
newTerm = firstTerm + secondTerm;

if (0 == (newTerm % 2)) {
sum += newTerm;
}

firstTerm = secondTerm;
secondTerm = newTerm;
}

System.out.print(sum);
}
}

Monday, May 11, 2009

Project Euler Problem 1

Recently I came across Project Euler web site. This is what the site has to say

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems. The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context."

I quickly registered to check what kind of mathematical problems are asked. I couldn't resist myself to start solving the problems. I chose Java to solve the problems as this is the only language I know ;)

Problem 1

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.

The solution is very simple and the code is self explanatory.

public class ProblemOne {
public static void main(String[] args) {
int result = 0;

for (int i = 1; i < 1000; ++i) {
if ((0 == (i % 3)) || (0 == (i % 5))) {
result += i;
}
}

System.out.println(result);
}
}