Page 1 of 612345»...Last »

Project Euler(55)

Posted on Sunday, June 28th, 2009 at 2:15 am by Universe Queen

Problem 55:

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.

Not all numbers produce palindromes so quickly. For example,

349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337

That is, 349 took three iterations to arrive at a palindrome.

Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).

Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.

How many Lychrel numbers are there below ten-thousand?

NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.

Answer: 249

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.math.*;
 
public class Euler55 
{
	public static boolean isPalindrome( String number )
	{
		for( int i=0; i<number.length()/2; i++ )
		{
			if( number.charAt(i) != number.charAt(number.length()-i-1) )
			{
				return false;
			}
		}
 
		return true;
	}
 
	public static String reverse( String str )
	{
		char[] result = new char[str.length()];
 
		for( int i=0; i<result.length; i++ )
		{
			result[i] = str.charAt( str.length()-i-1 );
		}
 
		return new String(result);
	}
 
	public static boolean isLychrel( int number )
	{
		int counter = 1;
		BigInteger n1 = BigInteger.valueOf( number );
		BigInteger n2;
 
		while( counter < 50 )
		{
			n2 = new BigInteger( reverse(n1.toString()) );
			n1 = n1.add( n2 );
 
			if( isPalindrome( n1.toString() ) )
			{
				return false;
			}
 
			counter++;
		}
 
		return true;
	}
 
	public static void main(String[] args)
	{
		int result = 0;
 
		for( int i=0; i<10000; i++ )
		{
			if( isLychrel(i) )
			{
				result++;
			}
		}
 
		System.out.println( result );
	}
}

Project Euler(54)

Posted on Sunday, June 28th, 2009 at 1:19 am by Universe Queen

Problem 54:

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

* High Card: Highest value card.
* One Pair: Two cards of the same value.
* Two Pairs: Two different pairs.
* Three of a Kind: Three cards of the same value.
* Straight: All cards are consecutive values.
* Flush: All cards of the same suit.
* Full House: Three of a kind and a pair.
* Four of a Kind: Four cards of the same value.
* Straight Flush: All cards are consecutive values of same suit.
* Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:
Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
2 5D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
3 2D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
4 4D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
5 2H 2D 4C 4D 4S
Full House
With Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1’s cards and the last five are Player 2’s cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player’s hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

Answer: 376

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.*;
 
public class Euler54 
{
	public static BigInteger winValue( String[] p )
	{
		BigInteger value = BigInteger.valueOf(0);
		int[] number = new int[p.length];
 
		for( int i=0; i<number.length; i++ )
		{
			if( p[i].charAt(0) >= '2' && p[i].charAt(0) <= '9' )
			{
				number[i] = p[i].charAt(0) - '0';
			}else if( p[i].charAt(0) == 'A' )
			{
				number[i] = 14;
			}else if( p[i].charAt(0) == 'T' )
			{
				number[i] = 10;
			}
			else if( p[i].charAt(0) == 'J' )
			{
				number[i] = 11;
			}
			else if( p[i].charAt(0) == 'Q' )
			{
				number[i] = 12;
			}
			else if( p[i].charAt(0) == 'K' )
			{
				number[i] = 13;
			}
		}
 
		for( int i=0; i<number.length-1; i++ )
		{
			for( int j=0; j<number.length-i-1; j++ )
			{
				if( number[j] < number[j+1] )
				{
					int tmp = number[j];
					number[j] = number[j+1];
					number[j+1] = tmp;
				}
			}
		}
 
		if( isSameSuit(p) && isConsecutive(number) && number[0] == 10 )
		{
			value = value.add( BigInteger.valueOf(1000000000000000l).multiply( BigInteger.valueOf(number[0]) ) );
		}
 
		if( isSameSuit(p) && isConsecutive(number) )
		{
			value = value.add( BigInteger.valueOf(100000000000000l).multiply( BigInteger.valueOf(number[0]) ) );
		}
 
		if( isSame4(number) > 0 )
		{
			value = value.add( BigInteger.valueOf(10000000000000l).multiply( BigInteger.valueOf(isSame4(number)) ) );
		}
 
		if( isSame3(number) > 0 && isSame2(number) > 0 )
		{
			value = value.add( BigInteger.valueOf(1000000000000l).multiply( BigInteger.valueOf(isSame3(number)) ) );
			value = value.add( BigInteger.valueOf(100000000000l).multiply( BigInteger.valueOf(isSame2(number)) ) );
		}
 
		if( isSameSuit(p) )
		{
			value = value.add( BigInteger.valueOf(10000000000l).multiply( BigInteger.valueOf( number[0] ) ) );
		}
 
		if( isConsecutive(number) )
		{
			value = value.add( BigInteger.valueOf(1000000000).multiply( BigInteger.valueOf( number[0] ) ) );
		}
 
		if( isSame3(number) > 0 )
		{
			value = value.add( BigInteger.valueOf( 100000000*isSame3(number) ) );
		}
 
		if( is2DifPairs(number) > 0 )
		{
			value = value.add( BigInteger.valueOf( 1000000*is2DifPairs(number) ) );
		}
 
		if( isSame2(number) > 0 )
		{
			value = value.add( BigInteger.valueOf( 100000*isSame2(number) ) );
		}
 
		value = value.add( BigInteger.valueOf( number[0]*10000 + number[1]*1000 + number[2]*100 + + number[3]*10 + number[4] ) );
 
		return value;
	}
 
	public static int is2DifPairs( int[] number )
	{
		if( isSame4(number) > 0 )
		{
			return -1;
		}
 
		if( isSame3(number) > 0 )
		{
			return -1;
		}
 
		int[] p = new int[2];
		int counter = 0;
		int result;
 
		for( int i=0; i<number.length-1; i++ )
		{
			for( int j=i+1; j<number.length; j++ )
			{
				if( number[i] == number[j] )
				{
					p[counter] = number[i];
					counter++;
				}
			}
		}
 
		if( counter < 2 )
		{
			return -1;
		}
 
		if( p[0] > p[1] )
		{
			result = 10*p[0] + p[1];
		}
		else
		{
			result = 10*p[1] + p[0];
		}
 
		return result;
	}
 
	public static int isSame3( int[] number )
	{
		if( isSame4(number) > 0 )
		{
			return -1;
		}
 
		int counter = 0;
 
		for( int i=0; i<3; i++ )
		{
			counter = 0;
 
			for( int j=i+1; j<number.length; j++ )
			{
				if( number[i] == number[j] )
				{
					counter++;
 
					if( counter >= 2 )
					{
						return number[i];
					}
				}
			}
		}
 
		return -1;
	}
 
	public static int isSame2( int[] number )
	{
		if( isSame4(number) > 0 )
		{
			return -1;
		}
 
		if( isSame3(number) > 0 )
		{
			return -1;
		}
 
		int counter = 0;
 
		for( int i=0; i<4; i++ )
		{
			counter = 0;
 
			for( int j=i+1; j<number.length; j++ )
			{
				if( number[i] == number[j] )
				{
					counter++;
 
					if( counter >= 1 )
					{
						return number[i];
					}
				}

Project Euler(53)

Posted on Saturday, June 27th, 2009 at 7:31 pm by Universe Queen

Problem 53:

There are exactly ten ways of selecting three from five, 12345:

123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

In combinatorics, we use the notation, ^(5)C_(3) = 10.

In general,
^(n)C_(r) =
n!

r!(n−r)!
,where r ≤ n, n! = n×(n−1)×…×3×2×1, and 0! = 1.

It is not until n = 23, that a value exceeds one-million: ^(23)C_(10) = 1144066.

How many, not necessarily distinct, values of ^(n)C_(r), for 1 ≤ n ≤ 100, are greater than one-million?

Answer: 4075

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
45
46
47
48
49
50
51
52
import java.math.*;
 
public class Euler53 
{
	public static BigInteger multiply( int from, int to )
	{
		BigInteger result = BigInteger.valueOf(from);
 
		if( from == to )
		{
			return BigInteger.valueOf(1);
		}
 
		for( int i=from+1; i<=to; i++ )
		{
			result = result.multiply( BigInteger.valueOf(i) );
		}
 
		return result;
	}
 
	public static BigInteger c( int n, int r )
	{
		if( n == r )
		{
			return BigInteger.valueOf(1);
		}
 
		BigInteger l1 = multiply( r+1, n );
		BigInteger l2 = multiply( 1, n-r );
 
		return l1.divide( l2 );
	}
 
	public static void main(String[] args)
	{
		long result = 0;
 
		for( int n=1; n<=100; n++ )
		{
			for( int r=1; r<=n; r++ )
			{
				if( c( n, r ).compareTo( BigInteger.valueOf(1000000) ) > 0 )
				{ 
					result++;
				}
			}
		}
 
		System.out.println( result );
	}
}

Project Euler(52)

Posted on Friday, June 26th, 2009 at 10:07 pm by Universe Queen

Problem 52:

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

Answer: 142857

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
45
46
public class Euler52 
{
	public static String rearrangeNumber( long number )
	{
		char[] n = String.valueOf( number ).toCharArray();
 
		for( int i=0; i<n.length-1; i++ )
		{
			for(  int j=0; j<n.length-i-1; j++ )
			{
				if( n[j] > n[j+1] )
				{
					char tmp = n[j];
					n[j] = n[j+1];
					n[j+1] = tmp;
				}
			}
		}
 
		return new String( n );
	}
 
 
	public static void main(String[] args)
	{
		long result = 1;
 
		while( true )
		{
			String n = rearrangeNumber(result);
 
			if( n.compareTo(rearrangeNumber(result*2)) == 0 && 
				n.compareTo(rearrangeNumber(result*3)) == 0 &&
				n.compareTo(rearrangeNumber(result*4)) == 0 &&
				n.compareTo(rearrangeNumber(result*5)) == 0 &&
				n.compareTo(rearrangeNumber(result*6)) == 0)
			{
				break;
			}
 
			result++;
		}
 
		System.out.println( result );
	}
}

Project Euler(51)

Posted on Wednesday, May 27th, 2009 at 5:16 am by Universe Queen

Problem 51:

By replacing the 1^(st) digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.

By replacing the 3^(rd) and 4^(th) digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

Answer: 121313

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public class Euler51 
{
	public static boolean isPrime( int 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 int factory( int n )
	{
		int result = 1;
 
		for( int i=1; i<=n; i++ )
		{
			result *= i;
		}
 
		return result; 
	}
 
	public static int[][] getCombinations( int n, int[] array  )
	{	
		int[][] result;
 
		if( n == 1 )
		{
			result = new int[array.length][1];
 
			for( int i=0; i<array.length; i++ )
			{
				result[i][0] = array[i];
			}
		}
		else if( n == array.length )
		{
			result = new int[1][];
 
			result[0] = array.clone();
		}
		else	
		{
			int numberOfCombinations = factory(array.length) / ( factory(n) * factory(array.length-n) );
			result = new int[numberOfCombinations][n];
			int index = 0;
 
			for( int i=0; i<=array.length-n; i++ )
			{
				int[] a = new int[array.length-i-1];
 
				for( int j=0; j<a.length; j++ )
				{
					a[j] = array[i+1+j];
				}
 
				int[][] left = getCombinations( n-1, a );
 
				for( int j=0; j<left.length; j++ )
				{
					result[index][0] = array[i];
 
					for( int m=0; m<left[j].length; m++ )
					{
						result[index][m+1] = left[j][m];
					}
 
					index++;
				}
			}
		}
 
		return result;
	}
 
	public static void main(String[] args)
	{
		int result = 0;
		int counter = 0;
 
		while( true )
		{
			if( isPrime( result ) )
			{
				String number = String.valueOf( result );
 
				for( int numberOfSameDigiters=2; numberOfSameDigiters<=number.length(); numberOfSameDigiters++ )
				{
					for( int index=0; index<=number.length()-numberOfSameDigiters; index++ )
					{
						if( number.charAt( index ) - '0' > 2 )
						{
							continue;
						}
 
						int[] indexes;
						int c = 0;
 
						for( int i=index+1; i<number.length(); i++ )
						{
							if( number.charAt(i) == number.charAt(index) )
							{
								c++;
							}
						}
 
						indexes = new int[c];
						c = 0;
 
						for( int i=index+1; i<number.length(); i++ )
						{
							if( number.charAt(i) == number.charAt(index) )
							{
								indexes[c] = i;
								c++;
							}
						}
 
						int[][] p = getCombinations( numberOfSameDigiters-1, indexes );
 
						for( int m=0; m<p.length; m++ )
						{
							int v = number.charAt(index) - '0' + 1;
							char[] s = new String( number ).toCharArray();
 
							while( v <= 9 )
							{
								char z = (char)(v + '0');
								s[0] = z;
 
								for( int b=0; b<p[m].length; b++ )
								{
									s[p[m][b]] = z;
								}
 
								int y = Integer.valueOf( new String(s) );
 
								if( isPrime(y) )
								{
									counter++;
								}
 
								v++;
							}
 
							if( counter >= 7 )
							{
								System.out.println( result );
								return;
							}
							else
							{
								counter = 0;
							}
						}
 
					}
				}
			}
 
			result++;
		}		
	}
}

Project Euler(50)

Posted on Sunday, May 24th, 2009 at 2:49 am by Universe Queen

Problem 50:

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

Answer: 997651

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
 
 
public class Euler50 
{
	public static boolean isPrime( int 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)
	{
		int result = 0;
		int maxLength = 0;
		Set primers = new TreeSet();
 
		for( int i=2; i<1000000; i++ )
		{
			if( isPrime(i) )
			{
				primers.add( i );
			}
		}
 
		Integer[] p = (Integer[])primers.toArray(new Integer[primers.size()]);
 
		for( int i=0; i<p.length; i++ )
		{
			long tmp = 0;
 
			for( int k=i; k<p.length; k++ )
			{
				tmp += p[k];
			}
 
			for( int j=p.length-1; j>i; j-- )
			{
				if( tmp < 1000000 && isPrime((int)tmp) && (j-i) >= maxLength )
				{
					maxLength = j - i;
					result = (int)tmp;
					break;
				}
 
				tmp -= p[j];
			}
		}
 
		System.out.println( result );
	}
}

Project Euler(49)

Posted on Saturday, May 23rd, 2009 at 5:19 am by Universe Queen

Problem 49:

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

Answer: 296962999629

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.*;
 
public class Euler49 
{
	public static boolean isPrime( int 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 boolean isPermutation( int n1, int n2 )
	{
		boolean result = true;
		String s1 = String.valueOf( n1 );
		String s2 = String.valueOf( n2 );
 
		for( int i=0; i<s1.length(); i++ )
		{
			if( !s2.contains( String.valueOf( s1.charAt(i) ) ) )
			{
				result = false;
			}
		}
 
		for( int i=0; i<s2.length(); i++ )
		{
			if( !s1.contains( String.valueOf( s2.charAt(i) ) ) )
			{
				result = false;
			}
		}
 
		return result;
	}
 
	public static void main(String[] args)
	{
		String result = null;
		Set smallerPrimers = new TreeSet();
		boolean find = false;
 
		for( int i=1000; i<10000; i++ )
		{
			if( isPrime(i) )
			{
				Iterator it = smallerPrimers.iterator();
 
				while( it.hasNext() )
				{
					int pre = (Integer)(it.next());
 
					if( isPermutation( pre, i ) )
					{
						int next = i + ( i - pre );
 
						if( isPrime( next ) && isPermutation( i, next ) && next < 10000 )
						{
							result = String.valueOf( pre ) + String.valueOf( i ) + String.valueOf( next );
							find = true;
							break;
						}
					}
				}
 
				if( find && i != 4817 )
				{
					break;
				}
				else
				{
					find = false;
				}
 
				smallerPrimers.add( i );
			}
		}
 
		System.out.println( result );
	}
}

Project Euler(48)

Posted on Saturday, May 23rd, 2009 at 4:33 am by Universe Queen

Problem 48:

The series, 1^(1) + 2^(2) + 3^(3) + … + 10^(10) = 10405071317.

Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + … + 1000^(1000).

Answer: 9110846700

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
public class Euler48 
{
	public static long final10( int number )
	{
		long result = 1;
 
		for( int i=0; i<number; i++ )
		{
			result *= number;
 
			if( result > 9999999999l )
			{
				result = result % 10000000000l;
			}
		}
 
		return result;
	}
 
	public static void main(String[] args)
	{
		long result = 0;
 
		for( int i=1; i<=1000; i++ )
		{
			result += final10( i );
		}
 
		result = result % 10000000000l; 
 
		System.out.println( result );
	}
}

Project Euler(47)

Posted on Tuesday, March 31st, 2009 at 3:35 pm by Universe Queen

Problem 47:

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?

Answer: 134043

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class Euler47 
{
	public static boolean isPrime( int 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 int distinctPrimeFactors( int number )
	{
		int f = 0;
		int p = 2;
		boolean changed = true;
 
		while( !isPrime( number ) )
		{		
			if( number % p == 0 )
			{
				number /= p;
 
				if( changed )
				{
					f++;
				}
 
				changed = false;
			}
			else
			{
				p++;
				changed = true;
			}
		}
 
		f++;
 
		return f;
	}
 
	public static void main(String[] args)
	{
		int result = 647;
 
		while( true )
		{
			if( distinctPrimeFactors(result) == 4 && distinctPrimeFactors(result+1) == 4 && distinctPrimeFactors(result+2) == 4 && distinctPrimeFactors(result+3) == 4 )
			{
				break;
			}
 
			result++;
		}
 
		System.out.println( result );
	}
}

Project Euler(46)

Posted on Sunday, March 29th, 2009 at 4:43 pm by Universe Queen

Problem 46:

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

9 = 7 + 2×1^(2)
15 = 7 + 2×2^(2)
21 = 3 + 2×3^(2)
25 = 7 + 2×3^(2)
27 = 19 + 2×2^(2)
33 = 31 + 2×1^(2)

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

Answer: 5777

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Euler46 
{
	public static boolean isPrime( int 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)
	{
		int result = 7;
		boolean ok = false;
 
		while( !ok )
		{		
			result += 2;
 
			ok = true;
 
			if( isPrime( result ) )
			{
				ok = false;
				continue;
			}
 
			for( int p=2; p<result; p++ )
			{
				if( isPrime( p ) )
				{
					int r = result - p;
					int k = r / 2;
					double s = Math.round( Math.sqrt(k) );
					s = s * s - k;
 
					if( k*2 == r && s < 0.000001 && s > -0.000001 )
					{
						ok = false;
						break;
					}
				}
			}
		}
 
		System.out.println( result );
	}
}