Euler problem 27.1

(defparameter *primes-up-to-1000*
  (let ((sieve (make-array 1001
                           :element-type 'bit
                           :initial-element 0)))
    (loop for i from 2 to 1000
          when (zerop (sbit sieve i))
            do (loop for j from (* i i) to 1000 by i
                     do (setf (sbit sieve j) 1)))
    (loop for i from 2 to 1000
          when (zerop (sbit sieve i))
            collect i)))

(defun primep (n)
  (cond ((<= n 1) nil)
        ((<= n 1000) (member n *primes-up-to-1000*))
        (t (loop for p in *primes-up-to-1000*
                 while (<= (* p p) n)
                 never (zerop (mod n p))))))

(defun prime-sequence-length (a b)
  (loop for n from 0
        while (primep (+ (* n n) (* a n) b))
        count t))

(defun find-best-quadratic-coefficients
    (&optional (max-a 1000) (max-b 1000))
  (loop with best-length = 0
        with best-a = 0
        with best-b = 0
        for a from (- max-a) to max-a
        do (loop for b in *primes-up-to-1000*
                 when (> b max-b) do (loop-finish)
                   do (let ((len (prime-sequence-length
                                  a
                                  b)))
                        (when (> len best-length)
                          (setf best-length len
                                best-a a
                                best-b b))))
        finally (return (list best-length
                              best-a best-b))))

(defun euler-27 ()
  (destructuring-bind (len a b)
      (find-best-quadratic-coefficients)
    (format t
            "Sequence length: ~d, a: ~d, b: ~d~%"
            len a b)
    (* a b)))

(defun main ()
  (time (format t "Answer: ~d~%" (euler-27))))

; SLIME 2.28
CL-USER> (main)
Sequence length: 71, a: -61, b: 971
Answer: -59231
Evaluation took:
  0.103 seconds of real time
  0.103747 seconds of total run time (0.103637 user, 0.000110 system)
  100.97% CPU
  258,919,169 processor cycles
  0 bytes consed
  
NIL
CL-USER> 

— Me@2025-02-23 03:48:23 PM

.

.

2025.02.23 Sunday (c) All rights reserved by ACHK

Ex 1.32 Path functions and state functions, 2.4

Structure and Interpretation of Classical Mechanics

.

2. On the other hand, the path function \bar f[q] and the path function \bar \Gamma (\bar f) \circ \Gamma [q] are not necessarily the same. Explain.

4. Write programs to illustrate the behavior.

~~~

(pp Gamma)

(define ((f-bar q) t)
  (f ((Gamma q) t)))
 
(define ((Gamma-bar h-bar) local)
  ((h-bar (osculating-path local)) (time local)))

(define (q t)
  (sin t))

(define (f-bar q)
  (D q))

(define p
  (literal-function 'q))

(show-expression
 ((f-bar p) 't))

Dq(t)

(show-expression
 ((osculating-path ((Gamma p) 't_0)) 't))

q(t_0) + (t - t_0)Dq(t_0)

(show-expression
 ((Gamma-bar f-bar) ((Gamma p) 't)))

Dq(t)

(define ((GammaBar-fBar-o-Gamma f-bar) q)
  (compose (Gamma-bar f-bar) (Gamma q)))

(show-expression
 (((GammaBar-fBar-o-Gamma f-bar) p) 't))

Two paths that have the same local description up to the n-th derivative are said to osculate with order-n contact. For example, a path and the truncated power series representation of the path up to order n have order-n contact; if fewer than n derivatives are needed by a local-tuple function, the path and the truncated power series representation are equivalent.

(define (g-bar q)
  ((expt D 3) q))

(show-expression
 ((Gamma p) 't))

\left(  \begin{array}{c}  t \\  q(t) \\  Dq(t)  \end{array}  \right)

(show-expression
 ((g-bar p) 't))

((D^3 q)(t))

(show-expression
 ((osculating-path ((Gamma p) 't_0)) 't))

q(t_0) + (t - t_0)Dq(t_0)

(show-expression
 (((GammaBar-fBar-o-Gamma g-bar) p) 't))

0

— Me@2024-10-16 10:34:35 AM

.

.

2025.02.16 Sunday (c) All rights reserved by ACHK

Lisp as a Y combinator

Lisp = Process(Lisp), 1.3

.

YCombinator = a fixed point of
Lisp = Process(Lisp)

So

Lisp = Y(Process)

— Me@2025-01-09 01:09:57 PM

.

Actually, it is

Lisp_n = Process(Lisp_(n-1))

— Me@2024-12-30 10:43:36 AM

.

.

2025.02.13 Thursday (c) All rights reserved by ACHK

Where are you? 3

Utopia | 何有之鄉, 2.5

.

選擇戀愛對象的原則,並不是找「理想伴侶」,而是找厭惡得來,仍然可以遷就到的人。反過來說,必須排除那些,「不可能遷就到」的可能對象。可以遷就到的,就是缺點;想遷就也遷就不到的,就為之人格問題。

不可能遷就到,而為了保命,必須立刻超光速逃離,或者𣊬間轉移的例子有:

……

六、 浪費:

買東西之目的在於:用途、娛樂 或 收藏。

但是有些人,三樣也不是,只享受購買當刻的快感。

七、 吝嗇:

賺錢之目的在於:現在使用 或 將來使用。

但是有些人,永久不使用。

.

題外話:

饋贈他人算不算「使用」呢?

不太算。盡量少做。

小恩養貴人 大恩養仇人
幫難不幫懶 救急不救窮

試想想,不是他自己賺來的錢,他還會珍惜嗎?

— Me@2025-02-10 12:08:47 PM

.

.

2025.02.10 Monday (c) All rights reserved by ACHK

Euler problem 26.2.2

import Data.List (elemIndex, maximumBy) 
import Data.Ord (comparing)
import Data.Maybe (fromJust)

recurringCycle :: Int -> Int
recurringCycle d = remainders 1 []
  where
    remainders r rs
      | r == 0          = 0
      | s `elem` rs     = 1 + fromJust (elemIndex s rs)
      | otherwise       = remainders ((r * 10) `mod` d) (s : rs)
      where s = r `mod` d

p26 :: Int
p26 = fst $ maximumBy (comparing snd) [(n, recurringCycle n) | n <- [1,3..999], n `mod` 5 /= 0]

λ> :set +s
λ> p26
983
(0.11 secs, 29,770,480 bytes)
λ> 

— Me@2025-02-06 08:29:54 AM

.

.

2025.02.06 Thursday (c) All rights reserved by ACHK

Quick Calculation 13.5

A First Course in String Theory

.

Show that [ \bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] = \frac{q}{2} \bar{\alpha}_{- \frac{q}{2}} and explain why \bar{N}^\perp is properly called a number operator.

~~~

\bar N^\perp = \sum_{p=1}^{\infty} \bar \alpha^i_{-p} \bar \alpha^i_{~p} + \sum_{k \in \mathbb{Z}^+_\text{odd}} \bar \alpha_{- \frac{k}{2}} \bar \alpha_{\frac{k}{2}}

Eq. (13.109):

\left[ \bar \alpha_{\frac{m}{2}}, \bar \alpha_{\frac{n}{2}} \right] = \frac{m}{2} \delta_{m+n, 0}

.

[AB,C]=A[B,C]+[A,C]B

\begin{aligned}  &[ \bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] \\  &= \sum_{p=1}^{\infty} [ \bar \alpha^i_{-p} \bar \alpha^i_{~p}, \bar{\alpha}_{- \frac{q}{2}} ] + \sum_{k \in \mathbb{Z}^+_\text{odd}} [ \bar \alpha_{- \frac{k}{2}} \bar \alpha_{\frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ]  \end{aligned}

.

If q=0,

[\bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] = 0

.

If q>0,

\begin{aligned}  &[ \bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] \\  &= \sum_{k \in \mathbb{Z}^+_\text{odd}}\left(   \bar \alpha_{- \frac{k}{2}} [  \bar \alpha_{\frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ]   + [ \bar \alpha_{- \frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ] \bar \alpha_{\frac{k}{2}} \right)  \\  &= \sum_{k \in \mathbb{Z}^+_\text{odd}}\left(   \bar \alpha_{- \frac{k}{2}} [  \bar \alpha_{\frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ] \right)  + 0  \\  &=    \bar \alpha_{- \frac{q}{2}} [  \bar \alpha_{\frac{q}{2}}, \bar{\alpha}_{- \frac{q}{2}} ]   \\  &=    \bar \alpha_{- \frac{q}{2}} \frac{q}{2}  \\  \end{aligned}

.

If q<0,

\begin{aligned}  &[ \bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] \\  &= \sum_{k \in \mathbb{Z}^+_\text{odd}}\left(   \bar \alpha_{- \frac{k}{2}} [  \bar \alpha_{\frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ]   + [ \bar \alpha_{- \frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ] \bar \alpha_{\frac{k}{2}} \right)  \\  &= 0 + \sum_{k \in \mathbb{Z}^+_\text{odd}}  + [ \bar \alpha_{- \frac{k}{2}}, \bar{\alpha}_{- \frac{q}{2}} ] \bar \alpha_{\frac{k}{2}}   \\  &=    \frac{q}{2} \bar \alpha_{\frac{-q}{2}}  \\  \end{aligned}

.

\bar{N}^\perp should not be called a number operator. Instead, [ \bar{N}^\perp, \cdot ] should be.

It's through the commutation relation [ \bar{N}^\perp, \bar{\alpha}_{- \frac{q}{2}} ] = \frac{q}{2} \bar{\alpha}_{- \frac{q}{2}} that we see \bar{N}^\perp functioning as a number operator, not by \bar{N}^\perp acting directly on \bar{\alpha}_{- \frac{q}{2}} operators.

— Me@2025-02-04 11:56:57 AM

.

.

2025.02.04 Tuesday (c) All rights reserved by ACHK

Logical arrow of time, 4.2

There is only one macroscopic reality in the past. For the “prediction” of the past, or the reconstruction of the past, aka postdiction, the uncertainty is a subjective ignorance of the past macroscopic data. An observer who has more data of the past will have less ignorance than other observers.

But for the prediction of the future, the uncertainty is an objective ignorance of the future microscopic state of the physical system. The ignorance of the future is objective in two senses:

In one sense, future data, by definition, does not exist for any present observers.

In another sense, the ignorance also includes the uncertainty of macroscopically indistinguishable microscopic states.

In other words, entropy is part of the objective ignorance. As in diffusion, you can see that this uncertainty about “which microstate the system is in” becomes bigger and bigger.

— Me@2013-08-10 05:26 PM

— Me@2022-04-04 06:59 PM

.

.

2025.02.01 Saturday (c) All rights reserved by ACHK