Category Archives: Project Euler

Project Euler: Problem 5

By | 17. April 2013

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?

int times = 0;
for (int i=1; i<1000000000; i++) {
	for (int j=1; j<=20; j++) {
		if (i%j==0) {
			times++;
		}
	}
	if (times==20) {
		System.out.println(i);
		break;
	}
	times = 0;
}

Lösung: 232792560

Project Euler: Problem 4

By | 17. April 2013

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.

int number1;
int number2;
int highest = 0;
int temp;
for (number1=100; number1<999; number1++) {
	for (number2=100; number2<999; number2++) {
		temp = number1*number2;
		if (Integer.toString(temp).equals(new StringBuffer(Integer.toString(temp)).reverse().toString())) {
			if (temp>highest) {
			highest = temp;
			System.out.println(number1 + " * " + number2 + " = " + highest);
			}
		}
	}
}

Lösung: 906609

Project Euler: Problem 3

By | 17. April 2013

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

What is the largest prime factor of the number 600851475143 ?

final long value = 600851475143L;
long temp = value;
long prime = 2;
while (temp>1L) {
	if (temp%prime==0) {
		System.out.println(prime);
		temp = temp/prime;
	} else {
		prime++;
	}
}

Lösung: 6857

Project Euler: Problem 2

By | 17. April 2013

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.

int x = 0;
int y = 1;
int limit = 4000000;
int result = 0;
int temp;
while (y&lt;limit) {
	temp = x + y;
	x = y;
	y = temp;
	if (y%2==0) {
		result += y;
	}
}
System.out.println(result);

Lösung: 4613732

Project Euler: Problem 1

By | 17. April 2013

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.

int result = 0;
for (int i=0; i<1000; i++) {
	if (i%3==0 || i%5==0) {
		result += i;
	}
}
System.out.println(result);

Lösung: 233168