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 @@ + + + + +EulerProblem053 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Combinatoric selections (Euler Problem 53)

Back to overview.

+
+
+
+
+
+
+
+

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?

+
+
+
+
+
+
In [3]:
+
+
+
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)
+
+
+
+
+
+
+
+
In [7]:
+
+
+
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
+
+
+
+
+
+
+
+
In [9]:
+
+
+
print(s)
+assert(s == 4075)
+
+
+
+
+
+
+
+
+
+
4075
+
+
+
+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem054.html b/ipython/html/EulerProblem054.html new file mode 100644 index 0000000..30839ed --- /dev/null +++ b/ipython/html/EulerProblem054.html @@ -0,0 +1,12081 @@ + + + + +EulerProblem054 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Problem Name (Euler Problem 54)

Back to overview.

+
+
+
+
+
+
+
+

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 Fives2C 3S 8S 8D TD Pair of EightsPlayer 2
25D 8C 9S JS AC Highest card Ace2C 5C 7D 8S QH Highest card QueenPlayer 1
32D 9C AS AH AC Three Aces3D 6D 7D TD QD Flush with DiamondsPlayer 2
44D 6S 9H QH QC Pair of Queens Highest card Nine3D 6D 7H QD QS Pair of Queens Highest card SevenPlayer 1
52H 2D 4C 4D 4S Full House With Three Fours3C 3D 3S 9S 9D Full House with Three ThreesPlayer 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.

+
+
+
+
+
+
In [21]:
+
+
+
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)
+
+
+
+
+
+
+
+
In [37]:
+
+
+
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.

+
+
+
+
+
+
In [45]:
+
+
+
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]
+    
+
+
+
+
+
+
+
+
In [55]:
+
+
+
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 
+
+
+
+
+
+
+
+
In [67]:
+
+
+
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      
+
+
+
+
+
+
+
+
In [70]:
+
+
+
print(s)
+assert(s == 376)
+
+
+
+
+
+
+
+
+
+
376
+
+
+
+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem055.html b/ipython/html/EulerProblem055.html new file mode 100644 index 0000000..7661dcf --- /dev/null +++ b/ipython/html/EulerProblem055.html @@ -0,0 +1,11809 @@ + + + + +EulerProblem055 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Lychrel numbers (Euler Problem 55)

Back to overview.

+
+
+
+
+
+
+
+

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.

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem056.html b/ipython/html/EulerProblem056.html new file mode 100644 index 0000000..3d3f400 --- /dev/null +++ b/ipython/html/EulerProblem056.html @@ -0,0 +1,11800 @@ + + + + +EulerProblem056 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Powerful digit sum (Euler Problem 56)

Back to overview.

+
+
+
+
+
+
+
+

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?

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem057.html b/ipython/html/EulerProblem057.html new file mode 100644 index 0000000..362829d --- /dev/null +++ b/ipython/html/EulerProblem057.html @@ -0,0 +1,11808 @@ + + + + +EulerProblem057 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Square root convergents (Euler Problem 57)

Back to overview.

+
+
+
+
+
+
+
+

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?

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem058.html b/ipython/html/EulerProblem058.html new file mode 100644 index 0000000..c25e6e6 --- /dev/null +++ b/ipython/html/EulerProblem058.html @@ -0,0 +1,11809 @@ + + + + +EulerProblem058 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Spiral primes (Euler Problem 58)

Back to overview.

+
+
+
+
+
+
+
+

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%?

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem059.html b/ipython/html/EulerProblem059.html new file mode 100644 index 0000000..f8f6334 --- /dev/null +++ b/ipython/html/EulerProblem059.html @@ -0,0 +1,11804 @@ + + + + +EulerProblem059 + + + + + + + + + + + + + + +
+
+
+
+
+
+

XOR decryption (Euler Problem 59)

Back to overview.

+
+
+
+
+
+
+
+

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.

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/EulerProblem060.html b/ipython/html/EulerProblem060.html new file mode 100644 index 0000000..d3c2bac --- /dev/null +++ b/ipython/html/EulerProblem060.html @@ -0,0 +1,11801 @@ + + + + +EulerProblem060 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Prime pair sets (Euler Problem 60)

Back to overview.

+
+
+
+
+
+
+
+

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.

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/index.html b/ipython/html/index.html index 652d2c3..501b585 100644 --- a/ipython/html/index.html +++ b/ipython/html/index.html @@ -839,6 +839,86 @@ + + + Problem 053 + + + + + + + + + + Problem 054 + + + + + + + + + + Problem 055 + + + + + + + + + + Problem 056 + + + + + + + + + + Problem 057 + + + + + + + + + + Problem 058 + + + + + + + + + + Problem 059 + + + + + + + + + + Problem 060 + + + + + + + Problem 067