Posted on Monday, November 24th, 2008 at 11:06 pm by Universe Queen
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?
Answer: 232792560
Implementation in Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| public class Euler5
{
public static void main(String[] args)
{
int result = 1;
while( true )
{
boolean b = true;
for( int i=1; i<=20; i++ )
{
if( result%i != 0 )
{
b = false;
break;
}
}
if(b)
{
break;
}
result++;
}
System.out.println( result );
}
} |
Tags: Project Euler
Posted in Project Euler | Permanent Link to "Project Euler(5)" | 126 Comments »
Posted on Monday, November 24th, 2008 at 10:44 pm by Universe Queen
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.
Answer: 906609
Implementation in Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| public class Euler4
{
public static boolean isPalindrome( Integer number )
{
String s = number.toString();
for( int i=0; i<s.length()/2; i++ )
{
if( s.charAt(i) != s.charAt(s.length()-i-1) )
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
int result = 0;
for( int i=100; i<1000; i++ )
{
for( int j=100; j<1000; j++ )
{
if( isPalindrome(i*j) && (i*j>result) )
{
result = i*j;
}
}
}
System.out.println( result );
}
} |
Tags: Project Euler
Posted in Project Euler | Permanent Link to "Project Euler(4)" | 94 Comments »
Posted on Monday, November 24th, 2008 at 10:26 pm by Universe Queen
Problem 3:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Answer: 6857
Implementation in Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| public class Euler3
{
public static boolean isPrime( long number )
{
if( number < 2 )
{
return false;
}
else if ( number == 2 )
{
return true;
}
else
{
for( long i=2; i<=Math.sqrt(number); i++ )
{
if( number%i == 0 )
{
return false;
}
}
}
return true;
}
public static void main(String[] args)
{
long result = 0;
for( long i=2; i<Math.sqrt(600851475143l); i++ )
{
if( isPrime(i) )
{
if( 600851475143l%i == 0 )
{
result = i;
}
}
}
System.out.println( result );
}
} |
Tags: Project Euler
Posted in Project Euler | Permanent Link to "Project Euler(3)" | 97 Comments »
Posted on Monday, November 24th, 2008 at 9:58 pm by Universe Queen
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.
Answer: 4613732
Implementation in Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| public class Euler2
{
public static void main(String[] args)
{
int result = 0;
int p = 1;
int q = 2;
int tmp = 0;
while ( q <= 4000000 )
{
if( q%2 == 0 )
{
result += q;
}
tmp = p+q;
p = q;
q = tmp;
}
System.out.println( result );
}
} |
Tags: Project Euler
Posted in Project Euler | Permanent Link to "Project Euler(2)" | 126 Comments »
Posted on Monday, November 24th, 2008 at 9:47 pm by Universe Queen
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve.
The Website of Project Euler is: http://projecteuler.net
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.
Answer: 233168
Implementation in Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| public class Euler1
{
public static void main(String[] args)
{
int result = 0;
for( int i=1; i<=1000.0/3; i++ )
{
result += 3*i;
if( i>=1000.0/5 )
{
continue;
}
result += 5*i;
if( i>=1000.0/15 )
{
continue;
}
result -= 15*i;
}
System.out.println( result );
}
} |
Tags: Project Euler
Posted in Project Euler | Permanent Link to "Project Euler(1)" | 138 Comments »