Posts

Find Maximum Subarray

FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high) left-sum = - infty // It is INT_MIN in c in limits.h sum = 0 for i = mid downto low sum = sum + A[i] if sum > left-sum left-sum = sum max-left = i right-sum = - infty // It is INT_MIN in c in limits.h sum = 0 for j = mid + 1 to high sum = sum + A[j] if sum > right-sum right-sum = sum max-right = j return (max-left, max-right, left-sum + right-sum) FIND-MAXIMUM-SUBARRAY(A, low, high) if high == low return (low, high, A[low]) // base case: only one element else mid = (low + high)/2 (left-low, left-high, left-sum) = FIND-MAXIMUM-SUBARRAY(A, low, mid) (right-low, right-high, right-sum) = FIND-MAXIMUM-SUBARRAY(A, mid+1, high) (cross-low, cross-high, cross-sum) = FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high) if left-sum >= right-sum and left-sum >= cross-sum return (left-low, left-high, left-sum) elseif right-sum >= left-sum and righ...

Proofs of Logarithm Properties

$log_aM +  log_aN = log_aMN$  (1) proof:  assume: $log_aM = m, log_aN = n$  so:  $a^m = M, a^n = N \Rightarrow M \cdot N = a^m \cdot a^n = a^{m+n}$  $log_a{MN} = m + n = log_aM + log_aN$  $log_aM - log_aN = log_a\frac{M}{N}$ (2) proof: assume: $log_aM = m, log_aN = n$ so: $a^m = M, a^n = N \Rightarrow \frac{M}{N}=\frac{a^m}{a^n} = a^{m - n} \Rightarrow log_a{\frac{M}{N}} = m - n = log_aM - log_aN$ $log_aa^M = M$ (3) proof: assume: $a^M = B \Rightarrow log_aB = M$ so: $log_aa^M = M$ $a^{log_aM} = M$ (4) proof: assume: $log_aM = B$ so: $a^B = M$ $\because B = log_aM, a^B = M$ $\therefore a^B = a^{log_aM} = M$ $log_aM^N = Nlog_aM$ (5) proof: $log_aM^N = log_a(\overbrace{M \cdot M \cdots M}^{N})$ $\because log_aM +  log_aN = log_aMN$  property (1) $\therefore log_a(\overbrace{M \cdot M \cdots M}^{N}) = \overbrace{log_aM + log_aM + \cdots log_aM}^{N} = Nlog_aM$ $log_ab = \frac{log_cb}{log_ca}= \frac{lnb}{lna} = \frac{lgb}{lga}$ (6) ...

Formula for Arithmetic Series

The Simple Arithmetic Sequences Let's say we have the simplest of arithmetic sequences. $\{1, 2, 3, \cdot\cdot\cdot, n\}$ And what I want to think about is what is the sum of this sequence going to be? And the sum of a sequence, we already know we call a series as following: $S_n = 1 + 2 + 3 + \cdot\cdot\cdot + n$ $S_n = n + (n-1) + (n-2) + \cdot\cdot\cdot + 1$ Now I'm going to add these two equations. $2S_n = (n+1) + (n+1) + (n+1) + \cdot\cdot\cdot + (n+1)$ So how many of these $(n+1)$ do we have? Well we have n of them there were n of these terms in each of these equations. So, we can rewrite this thing as following: $2S_n = n(n+1)$ $$ S_n = \frac{n(n+1)}{2} = n \cdot \frac{n+1}{2} = n \cdot \frac{a_n+a_1}{2} $$ $a_n$ is the nth term in our sequence, $a_1$ is the first term in our sequence. General Arithmetic Sequences Let's write an arithmetic sequence in general terms. $\{a, a+d, a+2d,\cdot\cdot\cdot, a+(n-1)d\}$ d  could be a positive or a negative number, which we cal...

Argument Passing by Value or Reference

#include // To read from the standard input, we write std::cin. These names use the // scope operatro(::), which says that the compiler should look in the scop // of the left-hand operand for the name of the right-hand operand. Thus, // std::cin says that we want to use the name string from the namespace std. // Referring to library names with this notation can be cumbersome. // Fortunately, there are easier ways to use namespace members. The safest // way is a **using declaration.** // A using declaration lets us use a name from a namespace without // qualifying the name with a namespace_name::prefix. A using declaration // has the form // using namespace::name: // Once the using declaration has been made, we can access name directly: // #include // uisng std::cin; // int main() // { // int i; // cin >> i; // ok: cin is a synonym for std::cin // cout using std::string; // Passing arguments by value void reset_passed_by_value(int *ip) { *ip = 0; // ch...

Finite Geometric Series Formula

We know that: a = first term r = common ratio n = number of terms We'r going to use a notation $S_n$ to denote the sum of first n terms as following: $S_n$= sum of first n terms $S_n=a+ar+ar^2+\cdot\cdot\cdot+ar^{n-1}$ We want to come up with a nice clean formula for evaluating this and we're gonna use a little trick to do it. Let's just multiple negative r on both sides of equation as following: $-rS_n=-ar-ar^2-\cdot\cdot\cdot-ar^{n-1}-ar^n$ So: $S_n-rS_n=a-ar^n$ $S_n(1-r)=a(1-r^n)$ $S_n=\frac{a(1-r^n)}{1-r}$

Quadratic Formula

Quadratic Formula: The quadratic equation is as follows: $ax^2+bx+c=0$ The quadratic formula tells us that the solutions to this equation is  $x = \frac{-b\pm\sqrt{b^2-4ac}}{2a}$ So let's apply it to some problem. Let's start off with something that we could have factored just to verify that it's giving us the same answer. Example 1: $x^2+4x-21=0$ $a=1, b=4, c=-21$ $x = \frac{-4\pm\sqrt{4^2-4\cdot1\cdot(-21)}}{2\cdot1}$ $x=\frac{-4\pm\sqrt{16+84}}{2}$ $x=\frac{-4\pm\sqrt{100}}{2}$ $x=\frac{-4\pm10}{2}$ $x=-2\pm5$ So: $x=3$ or $x=-7$ Sothe quadratic formula seems to have given us an answer for this. You can verify just by substituting back in that these do work. $(x+7)\cdot(x-3)=0$ $x+7=0$ or $x-3=0$ $x=-7$ or $x=3$ Example 2:(no real solutions) $3x^2+6x+10=0$ $a=3, b=6, c=10$ $x=\frac{-6\pm\sqrt{6^2-4\cdot3\cdot10}}{2\cdot3}$ $x=\frac{-6\pm\sqrt{36-120}}{6}$ $x=\frac{-6\pm\sqrt{-84}}{6}$ It jus gives us a square root of a negative number. It means this will have no real sol...

FreeBSD安装fcitx中文输入法(csh/tcsh)

Install # pkg install zh-fcitx  # pkg install zh-fcitx-libpinyin # pkg install zh-fcitx-table-extra # pkg install zh-CJKUnifonts Configurations 1. 文件 ~/.cshrc 添加的內容如下: setenv XMODIFIERS @im=fcitx 2.文件 ~/.xinitrc 內容如下: exec fcitx -d & . /usr/local/etc/xdg/xfce4/xinitrc