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)
      contents)))

(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")))
  (subseq
   (prin1-to-string
    (reduce
     #'+
     (mapcar
      #'parse-integer
      (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.

So

\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}}

Let

\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

NotesOrganization

"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."

… 

04[.]08[.]2003

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."

ResearchNotesOrganization

.

.

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
  where
    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 []
  where
    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
      where
        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)
  where
    triDiv n
      | even n = nDiv (n `div` 2)*nDiv (n+1)
      | otherwise = nDiv n*nDiv ((n+1) `div` 2)

λ> :set +s
λ> fm 500 1
76576500
(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