diff --git a/ipython/html/EulerProblem053.html b/ipython/html/EulerProblem053.html new file mode 100644 index 0000000..1cf8e08 --- /dev/null +++ b/ipython/html/EulerProblem053.html @@ -0,0 +1,11865 @@ + + + +
+https://projecteuler.net/problem=53
+There are exactly ten ways of selecting three from five, 12345:
+$123, 124, 125, 134, 135, 145, 234, 235, 245, 345$
+In combinatorics, we use the notation, 5C3 = 10.
+In general,
+$nCr = \frac{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: 23C10 = 1144066.
+How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?
+import math
+
+def combinations_naiv(n, r):
+ return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
+
+assert(combinations_naiv(5, 3) == 10)
+assert(combinations_naiv(23, 10) == 1144066)
+assert(combinations_naiv(20, 20) == 1)
+count = 0
+
+for n in range (1, 101):
+ for r in range(1, n + 1):
+ if combinations_naiv(n, r) > 10**6:
+ count += 1
+
+s = count
+print(s)
+assert(s == 4075)
+
+In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
+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?
+Let's start with some helper functions.
+def suit_to_value(suit):
+ return {
+ "2": 2,
+ "3": 3,
+ "4": 4,
+ "5": 5,
+ "6": 6,
+ "7": 7,
+ "8": 8,
+ "9": 9,
+ "T": 10,
+ "J": 11,
+ "Q": 12,
+ "K": 13,
+ "A": 14,
+ }[suit]
+
+assert(suit_to_value('K') == 13)
+
+
+def is_flush(colors):
+ first_color = colors[0]
+ for color in colors:
+ if color != first_color:
+ return False
+ return True
+
+
+assert(is_flush(["H", "H", "H", "H", "H"]))
+assert(is_flush(["H", "H", "D", "H", "H"]) == False)
+
+def is_straight(suits):
+ suits = sorted(suits)
+ first_suit = suits[0]
+ for suit in suits[1:]:
+ if first_suit + 1 != suit:
+ return False
+ first_suit = suit
+ return True
+
+assert(is_straight([6,3,4,5,7]))
+assert(is_straight([6,3,4,5,8]) == False)
+def get_numbered_groups(ns):
+ """ Takes [0, 3, 0, 3, 1] and returns
+ [(2, 3), (2, 0), (1, 1)]
+ """
+ rs = []
+ current_group = []
+ for n in sorted(ns, reverse=True):
+ if not current_group or n in current_group:
+ current_group.append(n)
+ else:
+ rs.append(current_group)
+ current_group = [n]
+ rs.append(current_group)
+ rs = sorted([(len(r), r[0]) for r in rs], reverse=True)
+ return rs
+
+assert(get_numbered_groups([0, 3, 0, 3, 1]) == [(2, 3), (2, 0), (1, 1)])
+Let's put that stuff together.
+def rank(hand):
+ """ A hand must be provided as a list of two letter strings.
+ The first letter is the suit and the second the suit of a card.
+
+ The function returns a tuple. The first value represents the hand as an integer
+ where 0 means high card and 9 means Straight Flush. The second value is a list of integers
+ ranking the value of the respective rank. For example, a Royal Flush would be (9, [14, 13, 12, 11, 10]),
+ while 22aqj would be (1, [2, 2, 14, 12, 11]). By doing this we can simply compare to hands
+ by first comparing the rank itself and then the list of integers.
+
+ We get something like ["5H", "6S", "7S", "5C", "KD"].
+ """
+
+ suits, colors = zip(*(map(lambda s: (s[0], s[1]), hand)))
+ suits = sorted(map(suit_to_value, suits))
+
+ flush = is_flush(colors)
+ straight = is_straight(suits)
+ numbered_suits = get_numbered_groups(suits)
+ if flush and straight:
+ return [8, numbered_suits]
+ if flush:
+ return [5, numbered_suits]
+ if straight:
+ return [4, numbered_suits]
+ if numbered_suits[0][0] == 4:
+ return [7, numbered_suits]
+ if numbered_suits[0][0] == 3 and numbered_suits[1][0] == 2:
+ return [6, numbered_suits]
+ if numbered_suits[0][0] == 3:
+ return [3, numbered_suits]
+ if numbered_suits[0][0] == 2 and numbered_suits[1][0] == 2:
+ return [2, numbered_suits]
+ if numbered_suits[0][0] == 2:
+ return [1, numbered_suits]
+ return [0, numbered_suits]
+
+assert(rank(["5H", "5C", "6S", "7S", "KD"]) == [1, [(2, 5), (1, 13), (1, 7), (1, 6)]]) # Pair of Fives
+assert(rank(["5D", "8C", "9S", "JS", "AC"]) == [0, [(1, 14), (1, 11), (1, 9), (1, 8), (1, 5)]]) # Highest card Ace
+assert(rank(["2D", "9C", "AS", "AH", "AC"]) == [3, [(3, 14), (1, 9), (1, 2)]]) # Three Aces
+assert(rank(["4D", "6S", "9H", "QH", "QC"]) == [1, [(2, 12), (1, 9), (1, 6), (1, 4)]]) # Pair of Queens Highest card Nine
+assert(rank(["2H", "2D", "4C", "4D", "4S"]) == [6, [(3, 4), (2, 2)]]) # Full House With Three Fours
+assert(rank(["2C", "3S", "8S", "8D", "TD"]) == [1, [(2, 8), (1, 10), (1, 3), (1, 2)]]) # Pair of Eights
+assert(rank(["2C", "5C", "7D", "8S", "QH"]) == [0, [(1, 12), (1, 8), (1, 7), (1, 5), (1, 2)]]) # Highest card Queen
+assert(rank(["3D", "6D", "7D", "TD", "QD"]) == [5, [(1, 12), (1, 10), (1, 7), (1, 6), (1, 3)]]) # Flush with Diamonds
+assert(rank(["3D", "6D", "7H", "QD", "QS"]) == [1, [(2, 12), (1, 7), (1, 6), (1, 3)]]) # Pair of Queens Highest card Seven
+assert(rank(["3C", "3D", "3S", "9S", "9D"]) == [6, [(3, 3), (2, 9)]]) # Full House with Three Threes
+def read_hands():
+ """ Reads a list of tuples where each field
+ in the tuple represents a hand.
+ """
+ hands = []
+ with open("EulerProblem054.txt", "r") as f:
+ for line in f.readlines():
+ cards = line.strip().split(" ")
+ hands.append((cards[:5], cards[5:]))
+ return hands
+
+p1_wins = 0
+
+for p1_hand, p2_hand in read_hands():
+ if rank(p1_hand) > rank(p2_hand):
+ p1_wins += 1
+ msg = "P1 hand {} wins over P2 hand {}."
+ #print(msg.format(p1_hand, p2_hand))
+ else:
+ msg = "P1 hand {} loses versus P2 hand {}."
+ #print(msg.format(p1_hand, p2_hand))
+
+s = p1_wins
+print(s)
+assert(s == 376)
+
+https://projecteuler.net/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.
+
+A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
+Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?
+
+https://projecteuler.net/problem=57
+It is possible to show that the square root of two can be expressed as an infinite continued fraction.
+√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
+By expanding this for the first four iterations, we get:
+1 + 1/2 = 3/2 = 1.5
+1 + 1/(2 + 1/2) = 7/5 = 1.4
+1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
+1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...
+The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
+In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?
+
+https://projecteuler.net/problem=58
+Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.
+37 36 35 34 33 32 31 +38 17 16 15 14 13 30 +39 18 5 4 3 12 29 +40 19 6 1 2 11 28 +41 20 7 8 9 10 27 +42 21 22 23 24 25 26 +43 44 45 46 47 48 49
+It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.
+If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?
+
+https://projecteuler.net/problem=59
+Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.
+A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
+For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.
+Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.
+Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher.txt (right click and 'Save Link/Target As...'), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.
+
+https://projecteuler.net/problem=60
+The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
+Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
+
+