Archive for June, 2009

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 );
	}
}