Euler problem 27.2.2

p_27 = -(2 * a - 1) * (a ^ 2 - a + 41)
  where
    n = 1000
    m = head $ filter (\x -> x ^ 2 - x + 41 > n) [1 ..]
    a = m - 1

Euler Problem 27 asks us to find a quadratic equation of the form (n^2 + a \cdot n + b) that generates the most consecutive prime numbers starting from n = 0, with the constraints |a| < 1000 and |b| \leq 1000. The final answer is the product a \cdot b for the winning pair. The “official” Haskell solution looks cryptic, but there’s a clever trick behind it. Let’s break it down step-by-step in a way that’s easier to follow.

A Handy Math Trick: Shifting the Quadratic

Imagine we tweak our quadratic (n^2 + a \cdot n + b) by replacing n with n - x. When we expand it out, we get:

(n - x)^2 + a(n - x) + b = n^2 + (a - 2x)n + (x^2 - ax + b)

This is still a quadratic in n, but now the coefficients are:

  • a_{\text{new}} = a - 2x
  • b_{\text{new}} = x^2 - ax + b

By choosing the right x, we can transform any quadratic into a simpler form, like n^2 + d or n^2 + n + d. This is our starting point.

Which Form Works Best?

  • Form 1: n^2 + d
    This one’s a dud. Plug in n = 0, 1, 2, 3, and you’ll see every other number is even (e.g., d, d+1, d+4, d+9). With so many evens, it can’t produce a long run of primes—primes are mostly odd (except 2).
  • Form 2: n^2 + n + d
    This is more promising. A mathematician named Rabinowitz proved that for this form to generate the longest sequence of primes, 4d - 1 must be a special number called a Heegner number. There are only six possibilities, and after testing, n^2 + n + 41 comes out on top. It spits out 40 primes from n = 0 to n = 39. The runner-up, n^2 + n + 17, doesn’t do as well.

So, n^2 + n + 41 is our golden ticket. But there’s more to it.

The Symmetry Bonus

Here’s a cool trick with n^2 + n + 41:

(-1 - n)^2 + (-1 - n) + 41 = n^2 + n + 41

This means if n = 2 gives a prime (like 47), then n = -3 (i.e., -1 - 2) does too (also 47). The sequence works both ways—positive and negative n. Starting at n = 0, we get 40 primes up to n = 39, but we can also count backward with negative n. Our goal is to shift this quadratic so we capture more of these primes from n = 0 onward, while keeping a and b within the problem’s limits.

Fitting It Into the Rules

Let’s start with n^2 + n + 41 (where a = 1, b = 41) and shift it using our trick:

  • New quadratic: n^2 + (1 - 2x)n + (x^2 - x + 41)
  • New a = 1 - 2x
  • New b = x^2 - x + 41

The problem says |a| < 1000 and |b| \leq 1000, so:

  • |1 - 2x| < 1000
  • |x^2 - x + 41| \leq 1000

The second condition is trickier because x^2 grows fast. We need the biggest x where x^2 - x + 41 \leq 1000.

Crunching the Numbers

Let’s test some x values:

  • x = 31: 31^2 - 31 + 41 = 961 - 31 + 41 = 971 (under 1000)
  • x = 32: 32^2 - 32 + 41 = 1024 - 32 + 41 = 1033 (over 1000)

So, x = 31 is the largest value that works. Now calculate:

  • a = 1 - 2 \cdot 31 = 1 - 62 = -61
  • b = 971 (from above)

Check the constraints:

  • |a| = |-61| = 61 < 1000
  • |b| = 971 \leq 1000

The Haskell Code Explained

That’s where this funky code comes in:

n = 1000
m = head $ filter (\x -> x ^ 2 - x + 41 > n) [1 ..]
a = m - 1

It finds m = 32 (first x where x^2 - x + 41 > 1000). Then x = m - 1 = 31.

The final line:

p_27 = -(2 * a - 1) * (a ^ 2 - a + 41)

This is a bit off. It uses a = 31, but we need a = 1 - 2x = -61. The correct product should be:

  • a = -61
  • b = 971
  • a \cdot b = -61 \cdot 971 = -59231

The code’s formula seems to be a typo or a leftover from a different derivation. It should just compute (1 - 2x) \cdot (x^2 - x + 41).

Why It Works

With a = -61, b = 971, the quadratic n^2 - 61n + 971 generates 71 primes from n = 0 to n = 70. This shift captures more of the n^2 + n + 41 sequence’s primes in the positive direction, thanks to the negative a. The product -59231 is the answer.

Wrapping Up

This solution is sneaky—it starts with a prime-generating champ, shifts it just right, and fits the problem’s rules. It’s not the brute-force way (testing every a and b), but a math shortcut that lands on the perfect answer. Pretty cool, right?

— Me@2025-03-24 12:25:20 PM

.

.

2025.03.30 Sunday (c) All rights reserved by ACHK

4 Basis Fields, 2

Functional Differential Geometry

.

(define e0
  (+ (* (literal-manifold-function 'e0x R2-rect)
        d/dx)
     (* (literal-manifold-function 'e0y R2-rect)
        d/dy)))

This code is misleading because:

1. The term d/dx is undefined:

1 ]=> (pp d/dx)

;Unbound variable: d/dx

2. It is defined only implicitly, making it difficult to follow:

(define-coordinates (up x y) R2-rect)

(define-coordinates (up r theta) R2-polar)

(define R2-rect-chi-inverse
  (point R2-rect))

(define R2-rect-point
  (R2-rect-chi-inverse (up 'x_0 'y_0)))

Once implicitly defined, we can print its definition:


1 ]=> (pp d/dx)
(lambda (f)
  (compose ((apply partial i) (compose f (coordinate-system '->point)))
           (coordinate-system '->coords)))

;No return value.

1 ]=> 

However, we still do not know where d/dx is defined.

— Me@2025-03-19 02:35:03 PM

.

.

2025.03.20 Thursday (c) All rights reserved by ACHK

Posted in FDG

LISP, Y Combinator, and the Fixed Point of Startup Innovation

According to Harold Abelson, one of the authors of “Structure and Interpretation of Computer Programs” (SICP), LISP can be seen as the fixed point of the process of defining a programming language, where the language itself can be used to define itself. This concept is analogous to the Y combinator in lambda calculus, which finds the fixed point of a function.

Drawing from this analogy, if LISP is considered the fixed point of the process of defining a programming language:

    • Paul Graham’s Y Combinator (the company) could be seen as the fixed point of the process of startup creation and growth. Here’s how:
      • Startup Ecosystem: Just as LISP recursively defines its own structure and operations, Y Combinator as a company recursively supports, nurtures, and grows startups. Each startup that Y Combinator backs can be seen as an iteration or instance of this process, much like how LISP functions or programs are instances of the language’s self-definition.
      • Self-Replication: Similar to how the Y combinator in lambda calculus allows functions to call themselves without explicit recursion, Y Combinator as a business model “replicates” itself through each cohort of startups it invests in. Each startup, in turn, might use the methodologies, culture, and network provided by Y Combinator to further innovate or even spawn more startups.
      • Cultural and Intellectual Capital: Y Combinator also serves as a hub for knowledge transfer, where the “fixed point” isn’t just the company but the collective wisdom, culture, and network that grows with each new company. This knowledge and network then feed back into the system, enhancing future iterations.

Therefore, according to the conceptual framework laid out by the authors of SICP, Paul Graham’s Y Combinator could be seen as the fixed point of the entrepreneurial process, where the methodology, culture, and success of one startup generation informs and shapes the next, creating a self-sustaining cycle of innovation and growth. This interpretation is not explicitly stated by Abelson or Sussman but is a logical extension of their ideas applied to business and innovation ecosystems.

— Me@2024-12-30 07:37:41 PM

.

.

2025.03.14 Friday (c) All rights reserved by ACHK

馬和小孩

這段改編自 2021 年 12 月 17 日的對話。

.

人的內置系統,即是先天而言,在愛情上是從一而終的。或者說,任何高等生物,都應該是那樣。

我有這個信念的原因是,看過一些例子:

一:

在澳洲大火中,一隻馬衝入火場,救牠的「太太」和兒子。

二:

一位父親戲弄四歲兒子,假扮和外來女子通電話。兒子相當氣憤,要告訴媽媽。

即使父親宣稱會買玩具給他,他也不肯隱瞞。

— Me@2025-03-04 07:26:55 PM

.

.

2025.03.05 Wednesday (c) All rights reserved by ACHK