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

.

.

# 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

.

.

# 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

.

.

# 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
--
--
| otherwise = gf ([p,1]:acc) ps
where
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

.

.

# MacBook Pro A1502

Euler problem 12.1.2

.

(defun good-reverse (lst)
(labels ((rev (lst acc)
(if (null lst)
acc
(rev
(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
acc-list)))
((eql (mod n p) 0)
(factor-iter (floor n p)
p-list
(cons p acc-list)))
((NULL ps)
(let* ((op
*primes*)
(num-extend
(range (1+
(ceiling (sqrt n)))
:min (+ p 2)
:step 2))
(primes-extend
(remove-if-not
#'primep
num-extend)))
(if (NULL primes-extend)
(cons n acc-list)
(progn
(setf *primes*
(append op primes-extend))
(factor-iter n
primes-extend
acc-list)))))
('t
(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

.

.

# 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$.

~~~

If

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

then

\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

.

.

# Euler problem 12.1.1

(defmacro sq (x)
(* ,x ,x))

(defmacro last-item (lst)
(car (last ,lst)))

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

(defun prime-sieve-a-list (input-lst)
(labels ((sieve-iter (go-lst acc-list)
(if (not go-lst)
acc-list
(if (> (sq (car go-lst))
(last-item go-lst))

(append (good-reverse acc-list)
go-lst)

(sieve-iter
(remove-if #'(lambda (x)
(=
(mod x
(car go-lst))
0))
(cdr go-lst))
(cons (car go-lst)
acc-list))))))

(sieve-iter input-lst '())))

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

(defmacro prime-sieve (n)
(prime-sieve-a-list (cons 2
(range (1+ ,n)
:min 3
:step 2))))

(time (length (prime-sieve 20000000)))

;; Evaluation took:
;; 19.907 seconds of real time


What is the value of the first triangle number to have over five hundred divisors?

(defun factor-iter (n p-list acc-list)
(if (NULL p-list)
acc-list
(let* ((p (car p-list))
(ps (cdr p-list)))
(cond ((> (* p p) n)
(good-reverse (cons n
acc-list)))
((eql (mod n p) 0)
(factor-iter (floor n p)
p-list
(cons p acc-list)))
('t
(factor-iter n ps acc-list))))))

(defparameter *pm* 2000000)

(defparameter *psi* (prime-sieve *pm*))

(defun factor (n)
(if (> n (expt *pm* 2))

(let ((m (floor (sqrt n))))
(factor-iter n (prime-sieve m) '()))

(factor-iter n *psi* '())))

(defun group-factors (lst)
(labels ((gf (acc lst)
(if (NULL lst)
(good-reverse acc)
(let* ((p (car lst))
(ps (cdr lst))
(lp1 (list p 1)))
(if (NULL acc)
(gf (list lp1) ps)
(if (eql p (caar acc))
(gf (cons
(list p
(+ 1
(cdr acc))
ps)
(gf (cons lp1 acc) ps)))))))
(gf '() lst)))

(defmacro sum (lst)
(reduce #'+ ,lst))

(defmacro product (lst)
(reduce #'* ,lst))

(defun nDiv (n)
(product (mapcar #'(lambda (x) (1+ (cadr x)))
(group-factors (factor n)))))

(defun fm (m n)
(labels ((tri-div (n)
(if (evenp n)
(* (nDiv (/ n 2)) (nDiv (1+ n)))
(* (nDiv n) (nDiv (/ (1+ n) 2))))))

(if (> (tri-div n) m)
(/  (* n (1+ n)) 2)
(fm m (+ 1 n)))))

;

(time (fm 500 1))

;; Evaluation took:
;; 0.007 seconds of real time

;; 76576500


— Me@2023-03-25 07:51:18 PM

.

.

# MangoHud

A Vulkan and OpenGL overlay for monitoring FPS, temperatures, CPU/GPU load and more.

.

.

vram
ram
swap
gpu_temp
cpu_temp
gpu_power
cpu_power


.

.

— Me@2023-03-07 09:14:51 AM

.

.

# Ex 2.1-2 Particle in a Box

Quantum Methods with Mathematica

.

Reproduce Figure 2.1-1 …

~~~

phi[n_, x_] := Sin[n Pi x]

p[n_] := Plot[phi[n,x],{x,0,1}]

GraphicsColumn[{p[1], p[2], p[3]}]


— Me@2023-03-03 01:30:46 PM

.

.

# Euler problem 10.2

Find the sum of all the primes below two million.

removeIf :: (a -> Bool) -> [a] -> [a]
removeIf p = filter (not . p)

sieveIter :: Integral a => [a] -> [a] -> [a]
sieveIter [] (x:xs) = x:xs
sieveIter (x:xs) acc
| x^2 > last (x:xs) = reverse acc++(x:xs)
| otherwise = sieveIter xss (x:acc)
where
xss = removeIf (\n -> n mod x == 0) xs

primeList :: Integral a => [a] -> [a]
primeList xs = sieveIter xs []

pL :: [Integer]
pL = primeList [2..2000000]

f :: Integer
f = sum (takeWhile (< 2000000) pL)


— colorized by palette fm

— Me@2023-02-25 12:35:57 PM

.

.

# Zsh, 2

To install the Z shell (Zsh):

1. Read my blog post “Zsh” to follow the instructions point 1 and point 2.

2. In the Bash terminal, run

zsh


once, in order to get the config file generated.

— Me@2023-01-19 12:41:56 PM

— Me@2023-02-24 05:19:38 PM

.

.

# Euler problem 10.1

Find the sum of all the primes below two million.

(defmacro sq (x)
(* ,x ,x))

(defmacro last-item (lst)
(car (last ,lst)))

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

(defun prime-sieve-a-list (input-lst)
(labels ((sieve-iter (go-lst acc-list)
(if (not go-lst)
acc-list
(if (> (sq (car go-lst))
(last-item go-lst))

(append (good-reverse acc-list)
go-lst)

(sieve-iter
(remove-if #'(lambda (x)
(=
(mod x (car go-lst))
0))
(cdr go-lst))
(cons (car go-lst)
acc-list))))))

(sieve-iter input-lst '())))

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

(defmacro prime-sieve (n)
(prime-sieve-a-list (cons 2 (range (1+ ,n)
:min 3
:step 2))))

(defun sum (lst)
(reduce #'+ lst))

(sum (prime-sieve 2000000))

(time (sum (prime-sieve 2000000)))


— Me@2023-02-16 11:34:49 PM

.

.

# G40-70

If you are using SSD, you should turn off the swap file:

1. In command line, run

swapoff --all


2. Backup the file

/etc/fstab


3. Then, in that file, comment out any lines that contain the word “swap”.

— Me@2023-02-14 05:00:26 PM

.

.

# KDE Chinese, 2

After installing Chinese Input in KDE, I found that in Emacs, I did not have the font regular script (楷書).

To install and activate the font, in the Bash terminal, run this command:

sudo apt-get install fonts-arphic-ukai


— Me@2023.01.22 05:42:57 PM

.

.

# FF7R

Crucial MX500 1TB 3D NAND SATA SSD

.

Final Fantasy 7 Remake Steam version testing result:

1. With GTX 1050 Ti as the GPU, the bottleneck is the GPU, not the CPU, even though my CPU itself was weak.

2. However, once the GPU got upgraded, the CPU became the bottleneck.

The following settings can make the game smoother:

3. Set the Texture to Low. However, I do not recommend that.

4. Set the Shadow to Low.

5. Set the number of background people to be zero.

6. Set the resolution to 720p.

.

The following are the less obvious steps to release some CPU pressure:

7. Turn Steam into offline mode.

8. Turn off as many as other programs as possible.

9. In the controller setting, change the controller from the default “XInput” to “XBox 360“. However, I do not recommend that because that would disable the game’s rumble function.

.

After these settings, the game should be able to load Cloud’s headache memory cinematic scenes.

— Me@2023-01-11 12:39:59 AM

.

10. If not, start to repeat pressing the pause button every one second when a scene starts to load.

— Me@2023.01.23 05:17:38 PM

.

.

# htmlize

is an Emacs app for exporting syntax-highlighted buffers to html files. This post is about how to install it.

1. In the Bash terminal, get htmlize by the following command:

sudo apt-get install elpa-htmlize


2.0 Change the value of the variable htmlize-output-type to inline-css by the following steps:

2.1 In Emacs, run the function describe-variable by the hotkey:

Ctrl-h v


2.2 When you are asked to describe variable, type

htmlize-output-type


2.3 Click “customize“.

2.4 Click “Value Menu“.

2.5 Select “inline-css“.

2.6 Click “Apply and Save“.

.

3.1 Then whenever you want to turn the Emacs buffer into html, run the command

M-x htmlize-buffer


The buffer will show the generated html code.

3.2 Save the buffer as an html file. The html code itself will get syntax-highlighted.

.

4.1 In the output html file, delete everything before

<body style="color: #333333; background-color: #FFFFFF;">


4.2 Then replace these 2 lines

<body style="color: #333333; background-color: #FFFFFF;">
<pre>


with

<pre style="color: #333333;background-color: #FFFFFF">


4.3 Delete everything after

</pre>


— Me@2023-01-15 03:39:29 PM

.

.

# KDE Chinese

This post is about how to enable Traditional Chinese keyboard in KDE Plasma desktop in Ubuntu 22.10.

1. In the Bash terminal, get the Cangjie input method (倉頡輸入法) by the following command:

sudo apt-get install ibus-cangjie


2. Then, get also the the following packages

sudo apt-get install \
ibus-data \
ibus-gtk \
ibus-gtk3 \
ibus-gtk4 \
ibus-table-cangjie3 \
ibus-table-cangjie5 \
ibus-table-cantonese \
ibus-table-quick-classic \
python3-ibus-1.0


4. In KDE, open the application IBus Preferences. Add the language Chinese.

5. Select Cangjie3.

6. Change the language switch hotkey from Super-Space to Ctrl-Space.

7. Then in any text editor, press Ctrl-Space to switch input language.

— Me@2023-01-01 11:52:09 AM

.

.

# Zsh

To install Z shell (Zsh):

1. In the Bash terminal, get zsh by the following command:

sudo apt-get install zsh


2. Then, apt-get also the following 3 packages:

sudo apt-get install zsh-syntax-highlighting

sudo apt-get install zsh-autosuggestions

sudo apt-get install zsh-theme-powerlevel9k


.

Follow the instructions point 0 and point 1 to install the Nix package manager.

.

4. Then, use the Nix package manager to install the Powerlevel10k theme.

nix-env -iA nixpkgs.zsh-powerlevel10k


5. Also, install the Meslo Nerd Font.

nix-env -iA nixpkgs.meslo-lgs-nf


The font would be located at

~/.nix-profile/share/fonts/truetype/


6. Use, for example, the KDE Plasma Font Management program to install the font.

.

7. Open the text file

~/.zshrc


Add the following 3 lines onto it:

source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh

source ~/.nix-profile/share/zsh-powerlevel10k/powerlevel10k.zsh-theme

8. Run the command

chsh --shell /usr/bin/zsh


9. Reboot the computer.

.

— Me@2022-12-21 01:29:17 PM

— Me@2023-01-05 10:43:10 PM

.

.

# cmu-infix

is

a library for writing infix mathematical notation in Common Lisp.

To install it:

1. In the Bash terminal, get Quicklisp by the following command:

sudo apt-get install cl-quicklisp


The file quicklisp.lisp will be created at

/usr/share/common-lisp/source/quicklisp/quicklisp.lisp


.

2. Run the command

sbcl --load /usr/share/common-lisp/source/quicklisp/quicklisp.lisp

(quicklisp-quickstart:install)



.

4. To enable the library, in the Common Lisp REPL, run the code:

(ql:quickload :cmu-infix)

5. And then run:

(named-readtables:in-readtable cmu-infix:syntax)

.

6. To test the library, run:

(defmacro add (x y)
(let ((a ,x)
(b ,y))
#I(a+b)))