Quicklisp, 2

Euler problem 13


To run the following code, you have to install the Quicklisp library manager by following the steps in my blog post titled cmu-infix.

(ql:quickload "str")

(defun file-get-contents (filename)
  (with-open-file (stream filename)
    (let ((contents (make-string
                     (file-length stream))))
      (read-sequence contents stream)

(defun file-get-lines (filename)
  (with-open-file (stream filename)
    (loop :for line = (read-line stream nil)
          :while line
          :collect line)))

(file-get-lines #P"n.txt")

(defun string-to-list (the-string)
  (loop :for char :across the-string
        :collect char))

(defun char-to-integer-list (char-list)
  (mapcar #'digit-char-p char-list))

(let ((the-list (file-get-contents #P"n.txt")))
      (butlast (str:split #\newline the-list)))))
   0 10))

— Me@2023-05-26 12:16:50 PM



2023.05.26 Friday (c) All rights reserved by ACHK

Ex 1.29 A particle of mass m slides off a horizontal cylinder, 1.1

Structure and Interpretation of Classical Mechanics


A particle of mass m slides off a horizontal cylinder of radius R in a uniform gravitational field with acceleration g. If the particle starts close to the top of the cylinder with zero initial speed, with what angular velocity does it leave the cylinder?


Along the tangential direction,

\displaystyle{m \frac{dv}{dt} = m g \sin \theta - f_a - f}

Assuming there is only air friction,

\displaystyle{m \frac{dv}{dt} = m g \sin \theta - f_a}


If the air resistance \displaystyle{f_a} equals \displaystyle{\frac{\beta m v^2}{2 R}},

\displaystyle{    m \frac{dv}{dt} = m g \sin \theta - \frac{\beta m v^2}{2 R}    }


Along the normal direction,

\displaystyle{\begin{aligned}      F_{\text{net}} &= F_C \\    m g \cos \theta - F_R &= \frac{m v^2}{R} \\     \end{aligned}},

where \displaystyle{F_R} is the normal reaction force.


\displaystyle{\begin{aligned}      m R \frac{d \dot \theta}{dt} &= m g \sin \theta - \frac{\beta}{2} \left( m g \cos \theta - F_R \right)  \\     R \ddot \theta &=  g \sin \theta - \frac{\beta}{2} \left( g \cos \theta - F_R \right)  \\    \end{aligned}}

This equation is not useful yet, because \displaystyle{F_R(\theta(t))} is still not known. So we keep using the original equation:

\displaystyle{\begin{aligned}      m \frac{dv}{dt} &= m g \sin \theta - \frac{\beta m v^2}{2 R} \\     R \frac{d^2 \theta}{dt^2} &= g \sin \theta - \frac{\beta R \dot \theta^2}{2} \\     \end{aligned}}


\displaystyle{\begin{aligned}      u &= \dot \theta^2 \\    \end{aligned}}

— Me@2023-05-23 11:02:25 AM



2023.05.25 Thursday (c) All rights reserved by ACHK

Not a number

Infinity is not a number.

Infinity is a process of ever increasing.

An ever-increasing process cannot be assigned a number.


At a particular time, the process has a particular number such as density.

But the whole process itself cannot be assigned a number.

— Me@2016-04-24 07:10:28 AM



2023.05.22 Monday (c) All rights reserved by ACHK

VirtualBox Start

In KDE, when a Virtualbox virtual machine is turned on, sometimes, both the guest OS and the host OS would capture the Super key at the same time. In order to avoid that, use the following steps:

1. Right-click the VirtualBox title bar

2. Choose:

More Actions
\to Special Window Settings
\to Ignore Global Shortcuts (in Appearance and Fixes)
\to Force \to Yes

— Me@2023-05-17 07:52:55 PM



2023.05.19 Friday (c) All rights reserved by ACHK

Random variable

Random variable X represents a single-valued result of a random event. Its value is unknown to us, not because of our ignorance, but because of its non-existence. The value exists only after the happening of that random event.

Symbol x represents a particular value of X. It is an existing value that can be substituted to X. We use symbol x instead of a number because we have not yet known what that particular number is.

— Me@2016-04-08 05:24:45 PM


X ~ random variable

It is a variable due to the fact that the “identical” random process can result differently at different times.


x ~ a value of X

Since it is a particular value of X, it is not a variable. However, it may seem to be a variable because it may still be unknown to us.


Symbol P(X) is meaningless because inside, it must be a statement (representing an event). Symbol X is a random variable, not a statement.

Instead, “X=x” is a statement. So expression P(X=x) is meaningful, such as

P(X=x) = {\begin{cases}{\frac {1}{2}},&x=0,\\{\frac {1}{2}},&x=1,\\0,&x\notin \{0,1\} \end{cases}}


From another point of view, X is a noun phrase, such as “my monthly salary”, not a number. Symbol x is a number, although maybe not known yet. That’s why whatever the formula, it contains no X‘s, but x‘s. For example,

\cdots = {\begin{cases}{\frac {1}{2}},&x=0,\\{\frac {1}{2}},&x=1,\\0,&x\notin \{0,1\} \end{cases}}

— Me@2016-05-04 06:32:24 PM



2023.05.16 Tuesday (c) All rights reserved by ACHK


"A real person has two reasons for doing anything... 
a good reason and the real reason."


One today worths two tomorrows.

Do one thing well. Do exactly what a customer wants, 
nothing more, nothing less. 

You can't solve all the problems. You can't satisfy 
all the people.

Constructive laziness is one of the cardinal virtues 
of a master programmer.

"such a final theory must be concerned not with fields, 
not even with spacetime, but rather with information 
exchange among physical processes."



A good scientist is a person with original ideas. 
A good engineer is a person who makes a design 
that works with as few original ideas as possible. 
There are no prima donnas in engineering. 
-- Freeman Dyson

"There are two kinds of researchers: 
those that have implemented something and 
those that have not. The latter will tell you that 
there are 142 ways of doing things and that 
there isn't consensus on which is best.  
The former will simply tell you that 141 of them 
don't work."




2023.05.14 Sunday ACHK


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










— Me@2023-03-10 11:00:16 AM



2023.05.11 Thursday (c) All rights reserved by ACHK

Euler problem 12.2

primes :: [Integer]
primes = 2 : filter (null . tail . primeFactors) [3, 5 ..]

primeFactors :: Integer -> [Integer]
primeFactors n = factor n primes
    factor n (p : ps)
      | p * p > n = [n]
      | n `mod` p == 0 = p : factor (n `div` p) (p : ps)
      | otherwise = factor n ps

groupFactors :: [Integer] -> [[Integer]]
groupFactors = gf []
    gf acc lst
      | null lst = reverse acc
      | null acc = gf [[p,1]] ps
      | p == head (head acc) =
        gf ([p, head (tail (head acc)) + 1]:tail acc) ps
      | otherwise = gf ([p,1]:acc) ps
        p = head lst
        ps = tail lst
nDiv :: Integer -> Integer
nDiv n = product (map ((1+) . head . tail)
                  (groupFactors (primeFactors n)))

fm :: Integer -> Integer -> Integer
fm m n
  | triDiv n > m = n*(n+1) `div` 2
  | otherwise = fm m (n+1)
    triDiv n
      | even n = nDiv (n `div` 2)*nDiv (n+1)
      | otherwise = nDiv n*nDiv ((n+1) `div` 2)

λ> :set +s
λ> fm 500 1
(0.20 secs, 199,313,728 bytes)

— Me@2023-05-04 09:51:19 AM



2023.05.04 Thursday (c) All rights reserved by ACHK

Ex 2.2 Stereographic Projection

Functional Differential Geometry


The points on the plane can also be specified with polar coordinates \displaystyle{(\rho, \theta)} and the points on the sphere are specified both by Riemann coordinates and the traditional colatitude and longitude \displaystyle{(\phi, \lambda)}.

   (chart S2-spherical)
   (point S2-Riemann)
   (chart R2-rect)
   (point R2-polar))
  (up 'rho 'theta)))


1. The code

(up 'rho 'theta)

represents the polar coordinates of a point.

2. The function

(point R2-polar)

generates an abstract point from a point in R2-polar coordinates.

3. The function

(chart R2-rect)

gives the rect coordinates given an abstract point on the plane R2.

   (chart R2-rect)
   (point R2-polar))
  (up 'rho 'theta)))


The procedure (point S2-Riemann) gives the point on the sphere given rectangular coordinates on the plane.

In other words, the function

(point S2-Riemann)

generates an abstract point-on-the-sphere (S2) from a point-on-the-plane (R2) in rect coordinates. In other words,






Perform an analogous computation to get the polar coordinates of the point on the plane corresponding to a point on the sphere given by its colatitude and longitude.

   (chart R2-polar)
   (point R2-rect)
   (chart S2-Riemann)
   (point S2-spherical))
  (up 'phi 'lambda)))

— Me@2023-04-22 10:42:50 PM



2023.04.25 Tuesday (c) All rights reserved by ACHK

Posted in FDG

Split-brain, 2.1

y-c-o-m-b 8 months ago | next [–]

I have schizotypal personality disorder. For me it feels like a collective, kind of like the borg. I say “me” or “I” when communicating externally, but all thoughts are actually communicated as “us”. All entities speak with the same voice (what most people think of as their inside voice), but each one speaks differently, with a different pace/tone, and has its own personality, thoughts, and desires. There are a handful of dominant ones that sit in the captain’s chair so to speak, and they all have a say in how we behave externally.

For example one of us is a pacifist, another one loves to socialize and party, one is cautious and anxious, the other very confident, and there’s one that has a severe thirst for violence and blood-lust (this one we work hard to keep in check). I’ve literally been in fights where immediately after knocking my opponent down, I ask if they’re ok and then help them back up and let them go. Then there’s hundreds of transient entities that are usually clones of personalities observed elsewhere (movie characters, celebrities, or other influential people). These transient types will actually adopt the mannerisms, voice, and even accent of the personalities and display them outwardly! Makes for very weird interactions with family and friends lol.

I’ve yet to see anyone on reddit or elsewhere with this description of the disorder. The closest thing is DID or maybe even borderline personality disorder (which is on the same schizo spectrum), but there’s no disassociation with what I have, we’re all fully aware of what the other is thinking.

— Split Brain Psychology

— Hacker News



2023.04.20 Thursday ACHK

權力來源 1.4

這段改編自 2010 年 4 月 24 日的對話。


~ environmental default

— Me@2021-11-16 07:59:12 PM


~ potential

~ 未來事件s對現在的影響

— Me@2023-03-12 09:14:50 AM


potential energy

~ energy of potential motion

~ energy of future motion

— Me@2023-04-16 12:08:08 PM



~ 勢力




— Me@2023-04-17 12:56:11 AM



2023.04.17 Monday (c) All rights reserved by ACHK

MacBook Pro A1502

Euler problem 12.1.2


(defun good-reverse (lst)
  (labels ((rev (lst acc)
             (if (null lst)
                  (cdr lst)
                  (cons (car lst) acc)))))
    (rev lst nil)))

(defparameter *primes* '(2 3 5))

(defun primeFactors (n)
  (factor-iter n *primes* '()))

(defun range (max &key (min 0) (step 1))
  (loop :for n :from min :below max :by step
        collect n))

(defun primep (x)
  (NULL (cdr (primeFactors x))))

(defun factor-iter (n p-list acc-list)
  (let* ((p (car p-list))
         (ps (cdr p-list)))
    (cond ((> (* p p) n)
           (good-reverse (cons n
          ((eql (mod n p) 0)
           (factor-iter (floor n p)
                        (cons p acc-list)))
          ((NULL ps)
           (let* ((op
                    (range (1+
                            (ceiling (sqrt n)))
                           :min (+ p 2)
                           :step 2))
             (if (NULL primes-extend)
                 (cons n acc-list)                
                   (setf *primes*
                         (append op primes-extend))
                   (factor-iter n
           (factor-iter n ps acc-list)))))

(defmacro prime-filter (n)
  `(remove-if-not #'primep
                  (cons 2
                        (range (1+ ,n)
                               :min 3
                               :step 2))))

(time (length (prime-filter 20000000)))

;; Evaluation took:
;; 13.056 seconds of real time
;; 1270607

— Me@2023-03-28 11:51:24 AM



2023.04.11 Tuesday (c) All rights reserved by ACHK

Ex 1.28 The energy function

Structure and Interpretation of Classical Mechanics


An analogous result holds when the f_\alpha‘s depend explicitly on time.

c. Show, using Euler’s theorem, that the energy function is \mathcal{E} = A + B.



\displaystyle{ \begin{aligned}      f(t x_1, t x_2, ...) &= t^n f( x_1, x_2 , ...),  \\     \end{aligned}}


\displaystyle{ \begin{aligned}      \sum_{i} x_i \frac{\partial f}{\partial x_i} &= n f(\mathbf{x}) \\    \end{aligned}}

— Euler’s Homogeneous Function Theorem


\displaystyle{\begin{aligned}          L + D_t F &= A - B \\ \\    D_t F &=  - \frac{1}{2} \sum_\alpha m_\alpha \left \{ g_\alpha(t,q) + [\partial_0 f_\alpha (t,q)]^2 + 2 \partial_0 f_\alpha (t,q) \partial_1 f_\alpha (t,q) v \right \} \\ \\    A &= \frac{1}{2} \sum_\alpha m_\alpha [\partial_1 f_\alpha (t,q) ]v^2  \\     - B &= - V(t, q) - \frac{1}{2} \sum_\alpha m_\alpha g_\alpha(t,q) \\            \end{aligned}}

This answer is not totally correct, since the generalized velocity, v, should be a vector.

— Me@2022-11-01 08:58:52 AM


Eq. (1.133):

\displaystyle{\mathcal{P}_i = (\partial_2 L)_i}

Eq. (1.144):

\displaystyle{\mathcal{E} = \mathcal{P} \dot Q - L},

where \displaystyle{\mathcal{P}} is the momentum state function.


\displaystyle{\begin{aligned}     \mathcal{E} &= \mathcal{P} \dot Q - A + B \\   &= \left( \partial_2 L \right) \dot Q - A + B \\   &= \left( \partial_2 (A - B) \right) \dot Q - A + B \\  \end{aligned}}

Since B has no velocity dependence,

\displaystyle{\begin{aligned}     \mathcal{E} &= \left( \partial_2 A \right) \dot Q - A + B \\    &=    \begin{bmatrix} (\partial_2 A)_1 & (\partial_2 A)_2 & ... \end{bmatrix}     \begin{bmatrix} \dot Q_1 \\ \dot Q_2 \\ \vdots \end{bmatrix}   - A + B \\   \\  \end{aligned}}

Since A is a homogeneous function of the generalized velocities of degree 2, by Euler’s Homogeneous Function Theorem,

\displaystyle{ \begin{aligned}        \sum_{i} v_i \frac{\partial A}{\partial v_i} &= 2 A(v) \\      \end{aligned}}

— Me@2023-04-06 12:49:49 PM



2023.04.07 Friday (c) All rights reserved by ACHK


Thinking in terms of infinity




— Me@2016-05-24 12:12:33 PM


無限思維 = 無思維

— Me@2023-04-02 03:38:07 PM


Infinity is not a number. So it cannot be used for comparing.

As long as you use “infinity”, you stop weighing the pros and cons.

In other words, you stop reasoning.

— Me@2023-04-02 01:51:06 PM


“Optimization without constraints” is not optimization.

— Me@2023-04-02 03:41:19 PM



2023.04.02 Sunday (c) All rights reserved by ACHK

History 5

bambax on June 17, 2018

Not just music history, but history in general.

History told forwards is superstition: trying to show that what came after was inevitable, when in fact it was one future among an infinite number of different possible futures.

History told backwards makes sense: look for the seeds of post events, in the past.

However the human mind loves nothing more than causation; we see cause and effect everywhere even where it’s not, we like “stories”, we don’t understand, don’t believe in, and outright reject chance.

— We Should Teach Music History Backwards

— Hacker News



2023.04.01 Saturday ACHK