Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 975e4b3

Browse files
committed
Merge branch 'master' of github.com:lucashc/programmingCheatSheet
2 parents 7c780d9 + ed6e2a4 commit 975e4b3

File tree

5 files changed

+98
-8
lines changed

5 files changed

+98
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Extended Euclidean algorithm
2+
def xgcd(a, b):
3+
"""return (g, x, y) such that a*x + b*y = g = gcd(a, b)"""
4+
x0, x1, y0, y1 = 0, 1, 1, 0
5+
while a != 0:
6+
q, b, a = b // a, a, b % a
7+
y0, y1 = y1, y0 - q * y1
8+
x0, x1 = x1, x0 - q * x1
9+
return b, x0, y0
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Chinese Remainder Theorem
2+
## \note only works for coprime numbers
3+
from functools import reduce
4+
def chinese_remainder(n, a):
5+
sum=0
6+
prod=reduce(lambda a, b: a*b, n)
7+
for n_i, a_i in zip(n,a):
8+
p=prod/n_i
9+
sum += a_i* mul_inv(p, n_i)*p
10+
return sum % prod
11+
12+
13+
def mul_inv(a, b):
14+
b0= b
15+
x0, x1= 0,1
16+
if b== 1: return 1
17+
while a>1 :
18+
q=a// b
19+
a, b= b, a%b
20+
x0, x1=x1 -q *x0, x0
21+
if x1<0 : x1+= b0
22+
return x1
23+
24+

algorithms/gcd.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

algorithms/maxflow.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Max Flow (Ford-Fulkerson)
2+
## \note Using a matrix $g$ with source $s$ and sink $t$
3+
from queue import Queue
4+
import math
5+
def max_flow(s, t, g):
6+
m = 0
7+
path = bfs(s, t, g)
8+
while path[t] != -1:
9+
flow = math.inf
10+
current = t
11+
while current != s:
12+
flow = min(flow, g[path[current]][current])
13+
current = path[current]
14+
current = t
15+
while current != s:
16+
g[path[current]][current] -= flow
17+
g[current][path[current]] += flow
18+
current = path[current]
19+
m += flow
20+
path = bfs(s, t, g)
21+
return m
22+
23+
def bfs(s, t, g):
24+
visited = [False for i in range(len(g))]
25+
q = Queue()
26+
q.put(s)
27+
visited[s] = True
28+
path = [-1 for i in range(len(g))]
29+
while not q.empty():
30+
n = q.get()
31+
if n == t: break
32+
for i in range(len(g)):
33+
if not visited[i] and g[n][i] > 0:
34+
q.put(i)
35+
path[i] = n
36+
visited[i] = True
37+
return path

main.tex

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ \section{Modules}
5757
\subsection{Python}
5858
\begin{itemize}
5959
\item Priority queue \arrow \ms{queue.PriorityQueue}
60+
\item Python base switching \arrow \ms{int(x, base=b)}
61+
\item Deque \arrow \ms{collections.deque}
6062
\end{itemize}
6163
\subsection{C++}
6264
\begin{itemize}
@@ -121,7 +123,7 @@ \subsection*{Links of rechts ombuigen}\vspace{-0.5em}
121123
\draw[->, >=latex] (0mm, 0mm) node[left] {A} -- (15mm, 10mm) node[above left] {B};
122124
\draw[->, >=latex] (15mm, 10mm) -- (40mm, 20mm) node[above] {C};
123125
\end{tikzpicture}
124-
\end{minipage}%
126+
\end{minipage}
125127
\begin{minipage}{0.25\linewidth}
126128
\begin{align*}
127129
\vec{AB} &= \begin{bmatrix} p \\ q \end{bmatrix} \\
@@ -179,6 +181,31 @@ \subsection*{Point to line distance}
179181
\begin{equation*}
180182
d(ax+by+c=0, (x_0, y_0)) = \frac{|ax_0+by_0+c|}{\sqrt{a^2+b^2}}
181183
\end{equation*}
184+
\subsection*{Number theory}
185+
$d(n)$ is het aantal positieve delers van een positief geheel getal $n$, met 1 en $n$ inbegrepen.
186+
\begin{align*}
187+
d(1) + d(2) + \ldots + d(n) &= \lfloor \frac{n}{1} \rfloor + \lfloor \frac{n}{2} \rfloor + \ldots + \lfloor \frac{n}{n} \rfloor
188+
\end{align*}
189+
190+
Aantal factoren van een priemgetal $a$ in $n!$ is:
191+
\begin{align*}
192+
\lfloor \frac{n}{a} \rfloor + \rfloor \frac{n}{a^2} \rfloor + \ldots
193+
\end{align*}
194+
Kleine stelling van Fermat, $p$ priem en $a>0$:
195+
\begin{align*}
196+
a^p \equiv a \mod p
197+
\end{align*}
198+
Als $a$ en $p$ copriem zijn:
199+
\begin{align*}
200+
a^{p-1} \equiv 1 \mod p
201+
\end{align*}
202+
\subsection*{Series}
203+
Geometric series with $r$:
204+
\begin{align*}
205+
a + ar^2 + \ldots + ar^{n-1} &= \sum^{n-1}_{k=0} ar^{k} = a\left(\frac{1-r^n}{1-r}\right) \\
206+
\lim_{n\rightarrow \infty} S &= \frac{a}{1-r} \quad \forall |r| < 1
207+
\end{align*}
208+
182209
\section{Algorithms}
183210

184211
%PLACEHOLDER%

0 commit comments

Comments
 (0)