Commit 5a659fe3 authored by Nayuki's avatar Nayuki

P71, P145: Tweaked bullet points in Python solution comments for consistency..

P128: Tweaked bullet points in Haskell solution comments for consistency.
parent 3635e913
...@@ -47,34 +47,34 @@ import qualified EulerLib ...@@ -47,34 +47,34 @@ import qualified EulerLib
- C - C
- -
- Basic observations: - Basic observations:
- - Except for the degenerate ring 0, each ring k has 6k cells. - * Except for the degenerate ring 0, each ring k has 6k cells.
- The kth ring has exactly 6 corner cells and 6(k - 1) edge cells. - The kth ring has exactly 6 corner cells and 6(k - 1) edge cells.
- - In the code we will skip the PD (prime difference) calculation for - * In the code we will skip the PD (prime difference) calculation for
- rings 0 and 1 because the existence of ring 0 breaks many patterns. - rings 0 and 1 because the existence of ring 0 breaks many patterns.
- - Doing the PD calculation for rings 0 and 1 by hand (n = 1 to 7 - * Doing the PD calculation for rings 0 and 1 by hand (n = 1 to 7
- inclusive), we find that PD(n) = 3 for and only for n = 1, 2. - inclusive), we find that PD(n) = 3 for and only for n = 1, 2.
- -
- Now let's analyze the characteristics of all cells in rings 2 or above. - Now let's analyze the characteristics of all cells in rings 2 or above.
- It's hard to justify these assertions rigorously, but they are true from - It's hard to justify these assertions rigorously, but they are true from
- looking at the spiral diagram. - looking at the spiral diagram.
- -
- - Corner cells along the upward vertical direction and the edge cells - * Corner cells along the upward vertical direction and the edge cells
- immediately to the right of this vertical column are the most interesting, - immediately to the right of this vertical column are the most interesting,
- so we will save these cases for last. - so we will save these cases for last.
- -
- - Claim: Except for cells immediately right of the upward corner column, - * Claim: Except for cells immediately right of the upward corner column,
- no edge cell satisfies PD(n) = 3. Proof: Take an arbitrary edge cell n - no edge cell satisfies PD(n) = 3. Proof: Take an arbitrary edge cell n
- not immediately to the right of the upward corner column... - not immediately to the right of the upward corner column...
- - The two neighbors in the same ring have a difference of 1 compared to n, - * The two neighbors in the same ring have a difference of 1 compared to n,
- which is not a prime number. - which is not a prime number.
- - The two neighbors in the previous (inward) ring are consecutive numbers, - * The two neighbors in the previous (inward) ring are consecutive numbers,
- so exactly one of them has an even absolute difference with n. Because - so exactly one of them has an even absolute difference with n. Because
- n is in ring 2 or above, the difference with any neighboring number in the - n is in ring 2 or above, the difference with any neighboring number in the
- previous ring is at least 6. Thus an even number greater than 2 is not prime. - previous ring is at least 6. Thus an even number greater than 2 is not prime.
- - Similarly, the two neighbors in the next (outward) ring are consecutive numbers. - * Similarly, the two neighbors in the next (outward) ring are consecutive numbers.
- One of them has an even difference with n, and this number is also at least 6, - One of them has an even difference with n, and this number is also at least 6,
- so one neighbor is definitely not prime. - so one neighbor is definitely not prime.
- - Therefore with at least 4 neighbors that do not have a prime difference, PD(n) <= 2. - * Therefore with at least 4 neighbors that do not have a prime difference, PD(n) <= 2.
- Example of an edge cell n = 11 in ring 2, which is straight left of the origin: - Example of an edge cell n = 11 in ring 2, which is straight left of the origin:
- 10 - 10
- 24 03 - 24 03
...@@ -82,19 +82,19 @@ import qualified EulerLib ...@@ -82,19 +82,19 @@ import qualified EulerLib
- 25 04 - 25 04
- 12 - 12
- -
- - Claim: No corner cell in the other 5 directions satisfies PD(n) = 3. - * Claim: No corner cell in the other 5 directions satisfies PD(n) = 3.
- Proof: Take an arbitrary corner cell n in the non-upward direction... - Proof: Take an arbitrary corner cell n in the non-upward direction...
- - Two of its neighbors (in the same ring) have a difference of 1, - * Two of its neighbors (in the same ring) have a difference of 1,
- which is not prime. - which is not prime.
- - One neighbor is in the previous ring (inward) while three neighbors - * One neighbor is in the previous ring (inward) while three neighbors
- are in the next ring (outward). - are in the next ring (outward).
- - Let the inner ring neighbor be k and the outer ring's middle neighbor - * Let the inner ring neighbor be k and the outer ring's middle neighbor
- be m. The three outer ring neighbors are {m - 1, m, m + 1}. - be m. The three outer ring neighbors are {m - 1, m, m + 1}.
- - Then n - k + 6 = m - n. Also, {m - 1, m + 1} have the same parity, - * Then n - k + 6 = m - n. Also, {m - 1, m + 1} have the same parity,
- and {k, m} have the same other parity. - and {k, m} have the same other parity.
- - Either both {|k - n|, |m - n|} are even or both {|m - 1 - n|, |m + 1 - n|} are even. - * Either both {|k - n|, |m - n|} are even or both {|m - 1 - n|, |m + 1 - n|} are even.
- In any case, all these differences are at least 6, so the even numbers are not prime. - In any case, all these differences are at least 6, so the even numbers are not prime.
- - Therefore with at least 4 neighbors that do not have a prime difference, PD(n) <= 2. - * Therefore with at least 4 neighbors that do not have a prime difference, PD(n) <= 2.
- Example of a corner cell n = 14 in ring 2, which is straight below the origin: - Example of a corner cell n = 14 in ring 2, which is straight below the origin:
- 05 - 05
- 13 15 - 13 15
...@@ -102,16 +102,16 @@ import qualified EulerLib ...@@ -102,16 +102,16 @@ import qualified EulerLib
- 28 30 - 28 30
- 29 - 29
- -
- - Now let's consider an arbitrary upward corner cell n in ring k, with k >= 2. - * Now let's consider an arbitrary upward corner cell n in ring k, with k >= 2.
- We shall give variables to all its neighbors like this: - We shall give variables to all its neighbors like this:
- d - d
- e f - e f
- n - n
- b c - b c
- a - a
- - a is in the previous ring, {b, c} are in the same ring as n, - * a is in the previous ring, {b, c} are in the same ring as n,
- and {d, e, f} are in the next ring. - and {d, e, f} are in the next ring.
- - Equations derived from the structure of the hexagonal spiral: - * Equations derived from the structure of the hexagonal spiral:
- n = 3k(k - 1) + 2. - n = 3k(k - 1) + 2.
- a = n - 6(k - 1). - a = n - 6(k - 1).
- b = n + 1. - b = n + 1.
...@@ -119,14 +119,14 @@ import qualified EulerLib ...@@ -119,14 +119,14 @@ import qualified EulerLib
- d = n + 6k. - d = n + 6k.
- e = n + 6k + 1 = d + 1. - e = n + 6k + 1 = d + 1.
- f = n + 6k + 6(k + 1) - 1 = n + 12k + 5. - f = n + 6k + 6(k + 1) - 1 = n + 12k + 5.
- - Hence we get these absolute differences with n: - * Hence we get these absolute differences with n:
- |a - n| = 6(k - 1). (Not prime because it's a multiple of 6) - |a - n| = 6(k - 1). (Not prime because it's a multiple of 6)
- |b - n| = 1. (Not prime) - |b - n| = 1. (Not prime)
- |c - n| = 6k - 1. (Possibly prime) - |c - n| = 6k - 1. (Possibly prime)
- |d - n| = 6k. (Not prime because it's a multiple of 6) - |d - n| = 6k. (Not prime because it's a multiple of 6)
- |e - n| = 6k + 1. (Possibly prime) - |e - n| = 6k + 1. (Possibly prime)
- |f - n| = 12k + 5. (Possibly prime) - |f - n| = 12k + 5. (Possibly prime)
- - Therefore for each k >= 2, we need to count how many numbers - * Therefore for each k >= 2, we need to count how many numbers
- in the set {6k - 1, 6k + 1, 12k + 5} are prime. - in the set {6k - 1, 6k + 1, 12k + 5} are prime.
- Example of a corner cell n = 8 in ring 2, which is straight above the origin: - Example of a corner cell n = 8 in ring 2, which is straight above the origin:
- 20 - 20
...@@ -135,7 +135,7 @@ import qualified EulerLib ...@@ -135,7 +135,7 @@ import qualified EulerLib
- 09 19 - 09 19
- 02 - 02
- -
- - Finally let's consider an arbitrary edge cell immediately to the right of the - * Finally let's consider an arbitrary edge cell immediately to the right of the
- upward vertical column. Suppose the cell's value is n and it is in ring k, - upward vertical column. Suppose the cell's value is n and it is in ring k,
- with k >= 2. Give variables to all its neighbors like this: - with k >= 2. Give variables to all its neighbors like this:
- f - f
...@@ -143,9 +143,9 @@ import qualified EulerLib ...@@ -143,9 +143,9 @@ import qualified EulerLib
- n - n
- a d - a d
- b - b
- - {a, b} are in the previous ring, {c, d} are in the current ring, and {e, f} are in - * {a, b} are in the previous ring, {c, d} are in the current ring, and {e, f} are in
- the next ring. The ascending ordering of all these numbers is (a, b, c, d, n, e, f). - the next ring. The ascending ordering of all these numbers is (a, b, c, d, n, e, f).
- - Equations derived from the structure of the hexagonal spiral: - * Equations derived from the structure of the hexagonal spiral:
- n = 3k(k + 1) + 1. - n = 3k(k + 1) + 1.
- a = n - 6k - 6(k - 1) + 1 = n - 12k + 7. - a = n - 6k - 6(k - 1) + 1 = n - 12k + 7.
- b = n - 6k. - b = n - 6k.
...@@ -153,14 +153,14 @@ import qualified EulerLib ...@@ -153,14 +153,14 @@ import qualified EulerLib
- d = n - 1. - d = n - 1.
- e = n + 6(k + 1) - 1 = n + 6k + 5. - e = n + 6(k + 1) - 1 = n + 6k + 5.
- f = n + 6(k + 1). - f = n + 6(k + 1).
- - Hence we get these absolute differences with n: - * Hence we get these absolute differences with n:
- |a - n| = 12k - 7. (Possibly prime) - |a - n| = 12k - 7. (Possibly prime)
- |b - n| = 6k. (Not prime because it's a multiple of 6) - |b - n| = 6k. (Not prime because it's a multiple of 6)
- |c - n| = 6k - 1. (Possibly prime) - |c - n| = 6k - 1. (Possibly prime)
- |d - n| = 1. (Not prime) - |d - n| = 1. (Not prime)
- |e - n| = 6k + 5. (Possibly prime) - |e - n| = 6k + 5. (Possibly prime)
- |f - n| = 6(k + 1). (Not prime because it's a multiple of 6) - |f - n| = 6(k + 1). (Not prime because it's a multiple of 6)
- - Therefore for each k >= 2, we need to count how many numbers - * Therefore for each k >= 2, we need to count how many numbers
- in the set {6k - 1, 6k + 5, 12k - 7} are prime. - in the set {6k - 1, 6k + 5, 12k - 7} are prime.
- Example of an edge cell n = 19 in ring 2: - Example of an edge cell n = 19 in ring 2:
- 37 - 37
......
...@@ -14,11 +14,11 @@ if sys.version_info.major == 2: ...@@ -14,11 +14,11 @@ if sys.version_info.major == 2:
# We consider each (integer) denominator d from 1 to 1000000 by brute force. # We consider each (integer) denominator d from 1 to 1000000 by brute force.
# For a given d, what is the largest integer n such that n/d < 3/7? # For a given d, what is the largest integer n such that n/d < 3/7?
# #
# * If d is a multiple of 7, then the integer n' = (d / 7) * 3 satisfies n'/d = 3/7. # - If d is a multiple of 7, then the integer n' = (d / 7) * 3 satisfies n'/d = 3/7.
# Hence we choose n = n' - 1 = (d / 7) * 3 - 1, so that n/d < 3/7. # Hence we choose n = n' - 1 = (d / 7) * 3 - 1, so that n/d < 3/7.
# Since (d / 7) * 3 is already an integer, it is equal to floor(d * 3 / 7), # Since (d / 7) * 3 is already an integer, it is equal to floor(d * 3 / 7),
# which will unifie with the next case. Thus n = floor(d * 3 / 7) - 1. # which will unifie with the next case. Thus n = floor(d * 3 / 7) - 1.
# * Otherwise d is not a multiple of 7, so choosing n = floor(d * 3 / 7) # - Otherwise d is not a multiple of 7, so choosing n = floor(d * 3 / 7)
# will automatically satisfy n/d < 3/7, and be the largest possible n # will automatically satisfy n/d < 3/7, and be the largest possible n
# due to the definition of the floor function. # due to the definition of the floor function.
# #
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
# reversed number does not have leading zeros. # reversed number does not have leading zeros.
# #
# Consider different cases for the number of digits n (from 1 to 9, but the arguments apply generally): # Consider different cases for the number of digits n (from 1 to 9, but the arguments apply generally):
# * n = 1: # - n = 1:
# Clearly there are no solutions because the last digit is always even. # Clearly there are no solutions because the last digit is always even.
# #
# * n = 0 mod 2: # - n = 0 mod 2:
# We begin by proving that when a number is "reversible", the process of adding # We begin by proving that when a number is "reversible", the process of adding
# the number to the reverse of itself will involve no carries in the arithmetic. # the number to the reverse of itself will involve no carries in the arithmetic.
# Normally a rigorous proof would require the use of mathematical induction, # Normally a rigorous proof would require the use of mathematical induction,
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
# (9,0). # (9,0).
# Therefore by combinatorics, there are 20 * 30^(n/2 - 1) reversible n-digit numbers when n is even. # Therefore by combinatorics, there are 20 * 30^(n/2 - 1) reversible n-digit numbers when n is even.
# #
# * n = 1 mod 2: # - n = 1 mod 2:
# Let's illustrate what happens with a 7-digit number abcdefg: # Let's illustrate what happens with a 7-digit number abcdefg:
# 0101010 # 0101010
# abcdefg # abcdefg
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
# This is why we get the alternating pattern of carries in the adding process. # This is why we get the alternating pattern of carries in the adding process.
# #
# The rest of the work is to enumerate the possibilities for each type of digit(s) in the number: # The rest of the work is to enumerate the possibilities for each type of digit(s) in the number:
# * Pairs of digits which take no carry and must generate a carry (20 choices): # - Pairs of digits which take no carry and must generate a carry (20 choices):
# (9,8), (9,6), (9,4), (9,2), # (9,8), (9,6), (9,4), (9,2),
# (8,9), (8,7), (8,5), (8,3), # (8,9), (8,7), (8,5), (8,3),
# (7,8), (7,6), (7,4), # (7,8), (7,6), (7,4),
...@@ -103,7 +103,7 @@ ...@@ -103,7 +103,7 @@
# (3,8), # (3,8),
# (2,9). # (2,9).
# Note that the first and last digits fall into this category, and there are no 0s at all. # Note that the first and last digits fall into this category, and there are no 0s at all.
# * Non-middle pairs of digits which take a carry and generate no carry (25 choices): # - Non-middle pairs of digits which take a carry and generate no carry (25 choices):
# (0,0), (0,2), (0,4), (0,6), (0,8), # (0,0), (0,2), (0,4), (0,6), (0,8),
# (1,1), (1,3), (1,5), (1,7), # (1,1), (1,3), (1,5), (1,7),
# (2,0), (2,2), (2,4), (2,6), # (2,0), (2,2), (2,4), (2,6),
...@@ -113,7 +113,7 @@ ...@@ -113,7 +113,7 @@
# (6,0), (6,2), # (6,0), (6,2),
# (7,1), # (7,1),
# (8,0). # (8,0).
# * Middle single digit, which takes a carry and generates no carry (5 choices): 0, 1, 2, 3, 4. # - Middle single digit, which takes a carry and generates no carry (5 choices): 0, 1, 2, 3, 4.
# All in all, there are 5 * 20^((n + 1)/4) * 25^((n - 3)/4) = 100 * 500^((n - 3)/4) # All in all, there are 5 * 20^((n + 1)/4) * 25^((n - 3)/4) = 100 * 500^((n - 3)/4)
# reversible n-digit numbers when n = 3 mod 4. # reversible n-digit numbers when n = 3 mod 4.
def compute(): def compute():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment