Skip to content

Commit

Permalink
P71, P145: Tweaked bullet points in Python solution comments for cons…
Browse files Browse the repository at this point in the history
…istency..

P128: Tweaked bullet points in Haskell solution comments for consistency.
  • Loading branch information
nayuki committed Mar 10, 2018
1 parent 3635e91 commit 5a659fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
52 changes: 26 additions & 26 deletions haskell/p128.hs
Expand Up @@ -47,86 +47,86 @@ import qualified EulerLib
- C
-
- 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.
- - 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.
- - 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.
-
- 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
- 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,
- 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
- 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.
- - 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
- 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.
- - 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,
- 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:
- 10
- 24 03
- 11
- 25 04
- 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...
- - 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.
- - 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).
- - 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}.
- - 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.
- - 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.
- - 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:
- 05
- 13 15
- 14
- 28 30
- 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:
- d
- e f
- n
- b c
- 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.
- - Equations derived from the structure of the hexagonal spiral:
- * Equations derived from the structure of the hexagonal spiral:
- n = 3k(k - 1) + 2.
- a = n - 6(k - 1).
- b = n + 1.
- c = n + 6k - 1 = d - 1.
- d = n + 6k.
- e = n + 6k + 1 = d + 1.
- 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)
- |b - n| = 1. (Not prime)
- |c - n| = 6k - 1. (Possibly prime)
- |d - n| = 6k. (Not prime because it's a multiple of 6)
- |e - n| = 6k + 1. (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.
- Example of a corner cell n = 8 in ring 2, which is straight above the origin:
- 20
Expand All @@ -135,32 +135,32 @@ import qualified EulerLib
- 09 19
- 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,
- with k >= 2. Give variables to all its neighbors like this:
- f
- c e
- n
- a d
- 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).
- - Equations derived from the structure of the hexagonal spiral:
- * Equations derived from the structure of the hexagonal spiral:
- n = 3k(k + 1) + 1.
- a = n - 6k - 6(k - 1) + 1 = n - 12k + 7.
- b = n - 6k.
- c = n - 6k + 1.
- d = n - 1.
- e = n + 6(k + 1) - 1 = n + 6k + 5.
- 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)
- |b - n| = 6k. (Not prime because it's a multiple of 6)
- |c - n| = 6k - 1. (Possibly prime)
- |d - n| = 1. (Not prime)
- |e - n| = 6k + 5. (Possibly prime)
- |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.
- Example of an edge cell n = 19 in ring 2:
- 37
Expand Down
4 changes: 2 additions & 2 deletions python/p071.py
Expand Up @@ -14,11 +14,11 @@
# 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?
#
# * 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.
# 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.
# * 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
# due to the definition of the floor function.
#
Expand Down
12 changes: 6 additions & 6 deletions python/p145.py
Expand Up @@ -15,10 +15,10 @@
# 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):
# * n = 1:
# - n = 1:
# 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
# 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,
Expand Down Expand Up @@ -70,7 +70,7 @@
# (9,0).
# 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:
# 0101010
# abcdefg
Expand All @@ -93,7 +93,7 @@
# 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:
# * 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),
# (8,9), (8,7), (8,5), (8,3),
# (7,8), (7,6), (7,4),
Expand All @@ -103,7 +103,7 @@
# (3,8),
# (2,9).
# 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),
# (1,1), (1,3), (1,5), (1,7),
# (2,0), (2,2), (2,4), (2,6),
Expand All @@ -113,7 +113,7 @@
# (6,0), (6,2),
# (7,1),
# (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)
# reversible n-digit numbers when n = 3 mod 4.
def compute():
Expand Down

0 comments on commit 5a659fe

Please sign in to comment.