原题链接 http://projecteuler.net/problem=54

Poker hands

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:


























































HandPlayer 1Player 2Winner
15H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
25D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
32D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
44D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
52H 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?

扑克手牌

在扑克牌游戏中,一手牌由5张牌构成,有不同的等级,从最小到最大,规则如下:

大牌:牌中最大的

对子:两张相同的牌

两对:有两对对子

三条:三张相同的牌

顺子:所有的牌是连续的

同花:所有的牌是同一花色

葫芦:三条和一对

四条:四张相同的牌

同花顺:所有的牌连续且是同花

同花大顺:AKQJ10组成的同花顺

牌值的顺序如下:

2,3,4,5,6,7,8,9,10,J,Q,K,A

如果两个玩家有相同等级的牌,则值更大的那个赢,例如一对8赢一对5(如下例1),但是如果等级相同,例如两个玩家都有一对Q,那么将比较手牌中的最大值(如下例4),如果最大的依然相同,则比较次大的,重复如上步骤。

考虑如下5种两个玩家的对局情况:

玩家1

玩家2

赢家

1

5H 5C 6S 7S KD
一对5

2C 3S 8S 8D TD
一对8

玩家2

2

5D 8C 9S JS AC
最大牌A

2C 5C 7D 8S QH
最大牌Q

玩家1

3

2D 9C AS AH AC
3张A

3D 6D 7D TD QD
方块同花

玩家2

4

4D 6S 9H QH QC
一对Q
最大9

3D 6D 7H QD QS
一对Q
最大7

玩家1

5

2H 2D 4C 4D 4S
葫芦
三个四

3C 3D 3S 9S 9D
葫芦
三个三

玩家1




文件 poker.txt中包含一千次两个玩家的对局情况,每一行有10张牌(空格分开),前5张牌是玩家1的牌,后5张牌是玩家2的牌,你可以假设手牌都是有效的(没有无效字符和重复的牌),每个玩家的手牌都没有特殊顺序,并且每一对局一定有一个赢。

求玩家1共赢了多少次。

解答:

这题好麻烦啊,写的代码真是乱。就是按照扑克牌的规则比较大小,真是很麻烦,一度令我有放弃欧拉工程的冲动,还好坚持下来了。

联系作者

原题链接 http://projecteuler.net/problem=53

#

Combinatoric selections

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, C(5,3) = 10.

In general,

C(n,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: C(23,10) = 1144066.

How many, not necessarily distinct, values of C(n,r), for 1 <= n <= 100, are greater than one-million?

组合选择

从五个,12345中选出三个一共有10中方法

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

在组合中我们使用记号C(5,3) = 10.

一般来说

C(n,r)=n!/(r!(n-r)!),r <= n, n! = n (n - 1) 3 2 * 1, 且0! = 1

直到n = 23,才出现值超过1000000: C(23,10) = 1144066.

求一共有多少个值,没有必要是唯一的,使得C(n,r), 1 <=n<= 100,超过1000000?

解答:

遍历吧。

联系作者

原题链接http://projecteuler.net/problem=52

Permuted multiples

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.
倍数排列
可以看到数125874和它的两倍251748,包含相同的数字,只是顺序不同。
找出最小的正整数x,使得x,2x,3x,4x,5x,和6x包含相同的数字。

解答:
暴力吧,有一点要注意的是,第一个数字一定是1。

联系作者

原题链接http://projecteuler.net/problem=51

Prime digit replacements

By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, 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.

素数数字替换

通过替换两位数*3的第一位数字,我们得到9个数字中的6个是素数:13,23,43,53,73和83.

通过用同一数字替换数56**3的第3、4位,这个5位数是第一个数,使得替换之后的10个数中,有7个数是素数。这就形成一个家族,它们是:56003,56113,56333,56443,56773和56993。56003则是这个素数族的第一个成员,是具有这种性质的最小素数。

找出最小的素数,通过用相同的数字替换这个素数中的一部分数字(没有必要想邻),可以得到一个素数,这个素数是8素数族的一部分。

解答:

一时还想不出好的办法,先空着。

联系作者

原题链接http://projecteuler.net/problem=50

Consecutive prime sum

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?

连续素数的和
素数41,可以写成6个连续素数的和:
41 = 2 + 3 + 5 + 7 + 11 + 13
这是100以下最长的连续素数的和等于一个素数
1000以下最长的连续素数的和,包含21个数,等于953
求1 000 000​以下能写成最长连续素数的和的素数


解法:
不懂得数学方法,只好暴力了,先求出1000 000以下素数,之后双重循环。太暴力了,竟然用了18分钟,看来还是得想办法改进。果然还是有方法的,想办法生成一个三角形,最底层是1000 000以下素数,上一层是连续两个素数之和,再上一层是连续三个素数之和,。。最后一层就是所有素数之和,从上往下找,第一个小于1000000的素数就是答案。实际过程中,可以不用从最顶上开始找,可以从第一个大于1000000那一行开始往下找,然后由这一行的数生成下一行,继续找。

联系作者

原题链接http://projecteuler.net/problem=49

Prime permutations

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?

素数排列
算术序列1487,4817,8147是按照3330递增的,它有两个平常的性质:(1)这三个数都是素数 (2)这三个4位数中的每一个都是另一个的排列

不存在1位,2位,3位具有这种性质的序列,但是还有另一个4位的具有这种性质的递增序列。

求将这个序列中的三个数链接起来得到的12位数

解答:
没什么好说的,先生成一个素数判别表,之后就简单了。

联系作者

原题链接http://projecteuler.net/problem=48

Self powers

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.
​自幂

序列 11 + 22 + 33 + … + 1010 = 10405071317
求序列11 + 22 + 33 + … + 10001000​的最后十个数字​

解答:
无非是大数运算。在Python里很简单。暴力之,只是不知道有什么数学规律没,我想不到。

联系作者

原题链接http://projecteuler.net/problem=47

Distinct primes factors

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 prime factors. What is the first of these numbers?

不同的素数因子
第一个连续两个数具有两个不同的素数因子的是:
14 = 2 7
15 = 3
5

第一个连续三个数具有三个不同的素数因子的是:
644 = 2² 7 23
645 = 3 5 43
646 = 2 17 19.

请找到第一个连续四个数具有四个不同的素数因子。这些数中的第一个是什么?

解答:
无非就是求素数因子,使用第3题中的方法,得到素数因子,剩下的就是暴力了。

联系作者

原题链接http://projecteuler.net/problem=46

Goldbach’s other conjecture

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 + 212
15 = 7 + 2
22
21 = 3 + 232
25 = 7 + 2
32
27 = 19 + 222
33 = 31 + 2
12

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?

哥德巴赫的另一个猜想
这个猜想是克里斯蒂安.哥德巴赫提出的,它是这样的:任意一个合数,如果是奇数的话,则可以写成一个素数与一个平方数的两倍的和
9 = 7 + 212
15 = 7 + 2
22
21 = 3 + 232
25 = 7 + 2
32
27 = 19 + 222
33 = 31 + 2
12
结果这个猜想是错的。
求最小的不能写成一个素数与一个平方数的两倍的和的奇合数​

解答:
还是暴力吧。

联系作者

原题链接http://projecteuler.net/problem=45

Triangular, pentagonal, and hexagonal

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle

T(n)=n(n+1)/2

1, 3, 6, 10, 15, …

Pentagonal

P(n)=n(3_n-_1)/2

1, 5, 12, 22, 35, …

Hexagonal

H(n)=n(2_n-_1)

1, 6, 15, 28, 45, …



It can be verified that T(285) = P(165) = H(143) = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

三角形的,五边形的和六边形的
三角形的、五边形的和六边形的数可以由以下公式生成:
三角形的 T(n)=n(n+1)/2 1,3,6,10,15,…
五边形的 P(n)=n(3n-1)/2 1,5,12,22,35,…
六边形的 H(n)=n(2n-1) 1,6,15,28,45,…
可以验证T(285)=P(165)=H(143)=40755.
求下一个既是五边形数,又是六边形数的三角形数。

解法:
没想到什么好的方法,只好暴力了。

联系作者