From e569d3b9cdd574875dfd1c461cc2af8bc748136b Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Tue, 4 Oct 2016 14:39:35 +0530 Subject: [PATCH 1/8] added newline added new line to correct formatting of main page. --- src/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.md b/src/index.md index 1d1a11af7..2c8bbea57 100644 --- a/src/index.md +++ b/src/index.md @@ -40,6 +40,7 @@ especially popular in field of competitive programming.* - [Kirchhoff theorem](./graph/kirchhoff-theorem.html) - [Topological sorting](./graph/topological-sort.html) - [Searching for bridges in O(N+M)](./graph/bridge-searching.html) + --- [Information for contributors](./contrib.html) and [Test-Your-Page form](./test.php) From fde62e015da933741f701316d852661211b1885a Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 26 Oct 2016 04:43:19 +0530 Subject: [PATCH 2/8] Added Euclid's algorithm Intial version. --- src/algebra/euclid-algorithm.md | 113 ++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/algebra/euclid-algorithm.md diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md new file mode 100644 index 000000000..55194a9c7 --- /dev/null +++ b/src/algebra/euclid-algorithm.md @@ -0,0 +1,113 @@ + + +# The Euclidean algorithm for finding GCD (greatest common divisor) + +Given two non-negative integers $a$ and $b$, we require the greater common divisor i.e. the largest number which is a divisor of $a$ and $b$ simultaneously. It's common denoted by $gcd$. + +![Euclid’s formula](http://e-maxx.ru/tex2png/cache/e49f97b41b2449d76d33517b580fef68.png) + +(here the symbol "$\mid$" denotes divisibility, i.e. “$k\mid a$” means “$k$ divides $a$”) + +When one of the numbers is zero, while the other is non zero, the greatest common divisor, by definition, is the second number. However, when both these numbers are zero, it is undefined(it can be an infinitely large number), but we can take it to be equal to zero. So we have a simple rule: if one of the numbers is zero, the other number is the gcd. + +The Euclidean algorithm, discussed below, solves the problem of finding the greatest common divisor of two numbers $a$ and $b$ in $O(\log min(a, b))$. + +The algorithm was first described in Euclid's “**Elements**” (circa 300 BC). However, it is possible that the algorithm has earlier origins. + +##Algorithm + +The algorithm is extremely simple and described by this formula: + +![image](http://e-maxx.ru/tex2png/cache/b8e1571eb429ef8ef1c06bc060dec3e7.png) + +##Implementation + +```cpp +int gcd (int a, int b) { + if (b == 0) + return a; + else + return gcd (b, a % b); +} +``` + +Using the ternary operator in C++, we can write a shorter implementation: + +```cpp +int gcd (int a, int b) { + return b ? gcd (b, a % b) : a; +} +``` + +Finally, we present a non-recursive form of implementation of the algorithm: + +```cpp +int gcd (int a, int b) { + while (b) { + a %= b; + swap (a, b); + } + return a; +} +``` + +##The Proof of correctness + +First, observe that at each successive iteration of the Euclidean algorithm, it's second argument strictly decreases, therefore, since it is always non-negative, the algorithm must terminate. + +For the proof of correctness, we need to show $gcd(a, b) = gcd(b, a mod b)$ for all $a \geq 0$, $b > 0$. + +We will show that the quantity on the left side of the equation, can be written as the value on the left. Obviously, this would mean that the left and right sides are the same and prove Euclid's algorithm. + +Let $d = gcd(a, b)$. Then, by definition $d\mid a$ and $d\mid b$. + +New writing the remainder on dividing $a$ by $b$: + +![image](http://e-maxx.ru/tex2png/cache/ad604a58d5faf878aeaa5492d8f28d12.png) + +But then, it follows: + +![image](http://e-maxx.ru/tex2png/cache/85feaf22c5f69e1d19617467cdab7a04.png) + +So, remembering the statement $d\mid b$, we obtain the system: + +![image](http://e-maxx.ru/tex2png/cache/9d785ea6dd075edc4a1849cf44affb22.png) + +We now use the following fact: if for any three numbers $p$, $q$, and $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get: + +![image](http://e-maxx.ru/tex2png/cache/9318355a27d52014ebd91c204579fb3c.png) + +Or, by substituting $d$ by it's definition($gcd(a, b)$) we get: + +![image](http://e-maxx.ru/tex2png/cache/256b72e6f755e9181250d20bf15cc4b7.png) + +So, we have shown that the left side divides the right. The second half of the proof is similar. + +##Time complexity + +The running time of the algorithm is estimated by Lame's theorem, which establishes a surprising connection between the euclidean algorithm and the Fibonacci sequence: + +If $a > b \geq 1$ and $b < F_n$ for some $n$, the euclidean algorithm performs no more than $n-2$ recursive calls. + +Moreover, one can show that the upper bound of this theorem is optimal. When $a = F_n$ and $b = F_n-1$, $gcd(a, b)$ performs $n-2$ recursive calls. In other words, consecutive fibonacci numbers are the worst case input for Euclid's algorithm. + +Given that Fibonacci numbers grow exponentially(as a constant with respect to $n$), we find that the Euclidean algorithm is performed in $O(\log min(a, b))$ + +LCM(Least common multiple) + +Calculating least common multiple (least common multiplier, lcm) is reduced to calculating $gcd$ with the following simple statement: + +![image](http://e-maxx.ru/tex2png/cache/1407548584803d912291dc28ee8f40ec.png) + +Thus, the calculation of the LCM can also be done using the euclidean algorithm with the same time complexity + +```cpp +int lcm (int a, int b) { + return a / gcd (a, b) * b; +} +``` +(here it is advantageous to first divide $gcd$ and then multiply by $b$, as it helps avoid overflow in some cases) + +##Literature + +- Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005] \ No newline at end of file From 647ab4a4a01c607987801478255fa84900868d5c Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 26 Oct 2016 04:44:44 +0530 Subject: [PATCH 3/8] Added note --- src/algebra/euclid-algorithm.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 55194a9c7..b7a1f713a 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -110,4 +110,6 @@ int lcm (int a, int b) { ##Literature -- Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005] \ No newline at end of file +- Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005] + +####Note: Translated by [NibNalin](http://codeforces.com/profile/NibNalin) \ No newline at end of file From 4b23bfe8cf5a4cbbfb333c05cbccd7aba903e081 Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 26 Oct 2016 04:50:48 +0530 Subject: [PATCH 4/8] Minor edits Updated formatting, grammar etc. --- src/algebra/euclid-algorithm.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index b7a1f713a..198902b63 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -14,13 +14,13 @@ The Euclidean algorithm, discussed below, solves the problem of finding the grea The algorithm was first described in Euclid's “**Elements**” (circa 300 BC). However, it is possible that the algorithm has earlier origins. -##Algorithm +## Algorithm The algorithm is extremely simple and described by this formula: ![image](http://e-maxx.ru/tex2png/cache/b8e1571eb429ef8ef1c06bc060dec3e7.png) -##Implementation +## Implementation ```cpp int gcd (int a, int b) { @@ -51,11 +51,11 @@ int gcd (int a, int b) { } ``` -##The Proof of correctness +## The Proof of correctness First, observe that at each successive iteration of the Euclidean algorithm, it's second argument strictly decreases, therefore, since it is always non-negative, the algorithm must terminate. -For the proof of correctness, we need to show $gcd(a, b) = gcd(b, a mod b)$ for all $a \geq 0$, $b > 0$. +For the proof of correctness, we need to show $gcd(a, b) = gcd(b, a\mod b)$ for all $a \geq 0$, $b > 0$. We will show that the quantity on the left side of the equation, can be written as the value on the left. Obviously, this would mean that the left and right sides are the same and prove Euclid's algorithm. @@ -83,17 +83,17 @@ Or, by substituting $d$ by it's definition($gcd(a, b)$) we get: So, we have shown that the left side divides the right. The second half of the proof is similar. -##Time complexity +## Time complexity The running time of the algorithm is estimated by Lame's theorem, which establishes a surprising connection between the euclidean algorithm and the Fibonacci sequence: If $a > b \geq 1$ and $b < F_n$ for some $n$, the euclidean algorithm performs no more than $n-2$ recursive calls. -Moreover, one can show that the upper bound of this theorem is optimal. When $a = F_n$ and $b = F_n-1$, $gcd(a, b)$ performs $n-2$ recursive calls. In other words, consecutive fibonacci numbers are the worst case input for Euclid's algorithm. +Moreover, one can show that the upper bound of this theorem is optimal. When $a = F_n$ and $b = F_{n-1}$, $gcd(a, b)$ performs $n-2$ recursive calls. In other words, consecutive fibonacci numbers are the worst case input for Euclid's algorithm. Given that Fibonacci numbers grow exponentially(as a constant with respect to $n$), we find that the Euclidean algorithm is performed in $O(\log min(a, b))$ -LCM(Least common multiple) +## LCM(Least common multiple) Calculating least common multiple (least common multiplier, lcm) is reduced to calculating $gcd$ with the following simple statement: From ccb9cc81aa9df6255263e111eddad3e5cee32bb3 Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 26 Oct 2016 04:53:28 +0530 Subject: [PATCH 5/8] Added images Copied images from emaxx.ru --- img/euclid1.png | Bin 0 -> 1364 bytes img/euclid2.png | Bin 0 -> 2048 bytes img/euclid3.png | Bin 0 -> 787 bytes img/euclid4.png | Bin 0 -> 643 bytes img/euclid5.png | Bin 0 -> 974 bytes img/euclid6.png | Bin 0 -> 850 bytes img/euclid7.png | Bin 0 -> 1002 bytes img/euclid8.png | Bin 0 -> 1030 bytes 8 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 img/euclid1.png create mode 100644 img/euclid2.png create mode 100644 img/euclid3.png create mode 100644 img/euclid4.png create mode 100644 img/euclid5.png create mode 100644 img/euclid6.png create mode 100644 img/euclid7.png create mode 100644 img/euclid8.png diff --git a/img/euclid1.png b/img/euclid1.png new file mode 100644 index 0000000000000000000000000000000000000000..4902f1959f2a6acfccb0388d63bec2945921925c GIT binary patch literal 1364 zcmV-a1*`grP)FMbJ z007+F+>(-#b#-;VzP^NnggQDpSXfxi&COWs)(&@_@}LL-vNIBec-Tz1OJeTM{^q1i zHZkW$9|NcavpSyt!=sh;Qx;_!7`5~#Br1S{ zq=CkT*HUT@cwO1WOiAcP5;PdP805$-xFs$SYqh<@U6?zoq#R~zgeA}O4v0sfH>!i7 z#a_L!J-NI!4*K>HD(Bcm#t_^rgl0P)5%5f(!G|77NF~DMAw~LjSS-Y}_{QYcc9J6V z>W9yccGhfD9nNN!Mm9?e89-H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?zg+zrw490T|?B2u~Lq1E&3A7f-t@{NxTHGCkhCH%TS^6p6pX6DohQk?Aqsn`D05 z{V@LUYDcC=c;Uvv`ta(%ZDTwob~bzJ)sbc`wr6fkzkc*FUOB{0fM)+Lc(9%)X6u=1 zdDHzc9_Vhg;zp*;Bjww%W3zONU-IzxjS3ISUTOkeY-sm>%=7g3)I39t&)ursU_^oTdf5%h^?E?9)9<}iw(qWr(eEEq6JV0=#GSX6q0ALRKw*~`fku(BzFlFpciroLb7dXErtq}EE;YWEsi%Cu=1H@%t;RL%dGW(r# zYh3L$%!})xT44*Otni~e-^5z9lL6u!MEhaeJc5=q2N6ygQa-8`bXXL|5A*zp?`0ai zj(B3rCGWThk7|Vz{3y={6V6>Pb03dug}vYXD9^X5v?{hGnER<}fdp$YqH=;VV0wJkXn*H5*ATb^BleJcDg&p+ag)7AoE9{g9B9HEQ?d`r7;{X=7^ zG|n|~(1kZMGZ&iNL&NC^FWuh?e-tBjf**F6TqjPWO;vcvxAcdwP8?+pf)D;-)8zTh wYd7uP4vDTTv6Clh%w5#~a_T*!{twF%e>^%Eyxd$z=>Px#07*qoM6N<$f?;*mIRF3v literal 0 HcmV?d00001 diff --git a/img/euclid6.png b/img/euclid6.png new file mode 100644 index 0000000000000000000000000000000000000000..5a7e0d720d8c82ba644b57757eaa7d42a1bb36eb GIT binary patch literal 850 zcmV-Y1FigtP)FMbJ z006MCu!Mw!l9G}vEG$@9SREZ5&CSi++}w3_b-up7Nl8f#4h}jxI-#MVW@cu@&FD4& z00P=cL_t(Y4W(Awf~z15jDSi&k^leNlM6zu_H4JyLmNXFCzFsC;JyyL``&qSe7EyA zf40Y+5B;&YZoV2JkVFwNz}!g=I&`q=X^x!3^0I)J{hEV+(Vhg#kD8*<(& zm1VTVH{4noUa;~e4h#t`zEzI)G6p7rDJ4xNCy$cs-1=e-Azcc~h=XQyDMrstbfh4D ztf?Lv(7{^#z}h)9Zw``aIa^jN8I~p12uMt#L@fz1Ne@p77^fO>HtYoPWDXFI$p^&s z@EJUJLM{~SMeYH1!(xr%PANIt(nh(Zl4P>S(#AsRCXsul3z2sP44h-3$hPi?6K^Jw zVxqULqS{P#7+~$J!P=-^gn2UeNmE?`P zT#5(8D>+#BupBX9^o zkGo#qS43B*EkK(06xD=ESkf&9Oy7NCF?sfI;jk4?B8GD9_sPpWLzR$&BgkK+qg{ft z^uUVHx*@9De4^uQcy%y+l(S-nF`L?!d<}ARVPt7@*%-BFmei~kZam+cJg6VmAlD|@ zInZvg$CZt<`rm?o=Wx%$*`9M-aIV%DfyQet|GHpY_!04?rT-Kk4qtx6+-C}KWax7E%CY@5C8xG07*qoM6N<$g1h;c!vFvP literal 0 HcmV?d00001 diff --git a/img/euclid7.png b/img/euclid7.png new file mode 100644 index 0000000000000000000000000000000000000000..7e16d4826e277b8c9a040ac4a7f29ac82c3e019d GIT binary patch literal 1002 zcmVFMbJ z007+F+>(-#b#-;VzP^NnggQDpSXfxi&COaN1%|AEsPa_J#l*W4?ao@M*~0cAn7I zKh-{iKh6Bo*Jm&l&`HtCJ(HzR?`jQ+1dJT6T&1}3WuKg-I*~BxL`EoQCVm* zrNCNEHylq1jcz^0?zt9qJ`Ou8;EK`gCuQxd!WvCC8c$i4TYGL;OPbCvHDuUX0auKI zJSu8u6(*X%x*04v0Oy=+!0Z;_DMvX)II6w&c59vFavmx+>DSnzrA-ND1Iya}Y z90#I{XC7N+=K2LAP2gvmN-Kg>qSYiH* z6wYa}0QY%wb$fmr)WtMGjLEYC})Aa z*7Hdq&f1-`;*GNru*R^wd!*Kx%g*pi$>gawO|UNsk6ImB3+zlT{gd;M&14AYugEa* z?>!p{9nJDl=WGP5 zF^srJYMsq;)>q2eOQuBEFw$h#loH=@mXaCJo)k9P3O>1L3X5?vHellNtw9~I(^ea! zuT14^1Y9u%t0FPmnARCy+t@CVl}s7iX_^o3{qE_lKrQ^PNx{?QxXOOmi0{gg&~@14 zy0r0pi7_ZYMhLH*4T1$zu;z-yYEztE7T=rA1u2?{BKh=Wy)2W*;#{pM|H@hT@&VaA zTof$uKN+XI9ELI$Bx`BW@KbUadD6gtFJ}+HJTqf;B*j%lNV1~XRAuJwMpL=i030V zmrW_g+tw6h|G$p92x&<7|J$`^<{7xdmgqlyS54vbdgb4}Z(=O%k~D+=3;ZVDNHc5t Y2SA}6*jO9G`2YX_07*qoM6N<$g7sbB3;+NC literal 0 HcmV?d00001 diff --git a/img/euclid8.png b/img/euclid8.png new file mode 100644 index 0000000000000000000000000000000000000000..fad67553ce5178686ddbde251b1f9b97e0e944aa GIT binary patch literal 1030 zcmV+h1o``kP)FMbJ z007O+&9Jbrb#-;2p`m7GW=Tm&l9H0#+}tcIEWWBlB`6%br%i_V#I-bUk2m zulGK#$K>X5nsh#m$u%7cwQG;^ z9d3AArZ4g1c3Yu<$)li(af{8iZGWsTS>A9|ojfY!q#jE{s+)&-TERBJ(pY2`29E=} zKfKCt{}ET8qYO+0rdY}ebl*KKlsQVS@`U}8a^>pehI^daln1}H32SrgL?mRhmxV5f2rYleBx zUP&!|RMS#g$%%d(Z&r?59J*n;4Pu(XQYGe6wNp_d-&uXj@u(JZ$$xAYjuD8eHo{Os zOfZ1KEz->}=vlX(J3PSj>YaGDX5>EbD521^p=Sn-w1J~>Hhw`ERe z4F9qo#SSf3ch1C)*WiB>F_|ugi3yhADvpj|lquvi_OHMR>~?r8BGk-?X*5!SC4cqI z2e#qKG;%r*PW38JjcVnKx7#l&{X3PGCsWDkG&mKhI4g-Os<2nL7N2LeIys#Orxaf$ zkwmfDXWZG*sf*R>e!^OCJ+N8{DJydN2{>iY2F%o^ zG{R4qxgCIGFH^~B6P#oRK2~|5X>; Date: Wed, 26 Oct 2016 04:55:04 +0530 Subject: [PATCH 6/8] Updated links Updated image links to root --- src/algebra/euclid-algorithm.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 198902b63..6ed136842 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -4,7 +4,7 @@ Given two non-negative integers $a$ and $b$, we require the greater common divisor i.e. the largest number which is a divisor of $a$ and $b$ simultaneously. It's common denoted by $gcd$. -![Euclid’s formula](http://e-maxx.ru/tex2png/cache/e49f97b41b2449d76d33517b580fef68.png) +![Euclid’s formula](&imgroot&/euclid1.png) (here the symbol "$\mid$" denotes divisibility, i.e. “$k\mid a$” means “$k$ divides $a$”) @@ -18,7 +18,7 @@ The algorithm was first described in Euclid's “**Elements**” (circa 300 BC). The algorithm is extremely simple and described by this formula: -![image](http://e-maxx.ru/tex2png/cache/b8e1571eb429ef8ef1c06bc060dec3e7.png) +![image](&imgroot&/euclid2.png) ## Implementation @@ -63,23 +63,23 @@ Let $d = gcd(a, b)$. Then, by definition $d\mid a$ and $d\mid b$. New writing the remainder on dividing $a$ by $b$: -![image](http://e-maxx.ru/tex2png/cache/ad604a58d5faf878aeaa5492d8f28d12.png) +![image](&imgroot&/euclid3.png) But then, it follows: -![image](http://e-maxx.ru/tex2png/cache/85feaf22c5f69e1d19617467cdab7a04.png) +![image](&imgroot&/euclid4.png) So, remembering the statement $d\mid b$, we obtain the system: -![image](http://e-maxx.ru/tex2png/cache/9d785ea6dd075edc4a1849cf44affb22.png) +![image](&imgroot&/euclid5.png) We now use the following fact: if for any three numbers $p$, $q$, and $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get: -![image](http://e-maxx.ru/tex2png/cache/9318355a27d52014ebd91c204579fb3c.png) +![image](&imgroot&/euclid6.png) Or, by substituting $d$ by it's definition($gcd(a, b)$) we get: -![image](http://e-maxx.ru/tex2png/cache/256b72e6f755e9181250d20bf15cc4b7.png) +![image](&imgroot&/euclid7.png) So, we have shown that the left side divides the right. The second half of the proof is similar. @@ -97,7 +97,7 @@ Given that Fibonacci numbers grow exponentially(as a constant with respect to $n Calculating least common multiple (least common multiplier, lcm) is reduced to calculating $gcd$ with the following simple statement: -![image](http://e-maxx.ru/tex2png/cache/1407548584803d912291dc28ee8f40ec.png) +![image](&imgroot&/euclid8.png) Thus, the calculation of the LCM can also be done using the euclidean algorithm with the same time complexity From 379ca99533d484a7a39506f5ce9c46d58c646b12 Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 26 Oct 2016 05:10:39 +0530 Subject: [PATCH 7/8] Added problem and link to home Added basic problem and updated link in index. --- src/algebra/euclid-algorithm.md | 4 ++++ src/index.md | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 6ed136842..a0ca5ab74 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -112,4 +112,8 @@ int lcm (int a, int b) { - Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005] +## Problems + +- [GCD and LCM[easy]](https://www.codechef.com/problems/FLOW016) + ####Note: Translated by [NibNalin](http://codeforces.com/profile/NibNalin) \ No newline at end of file diff --git a/src/index.md b/src/index.md index 2e4fc8195..31c55b78a 100644 --- a/src/index.md +++ b/src/index.md @@ -9,6 +9,7 @@ especially popular in field of competitive programming.* ### Algebra - [Euler's Totient Function](./algebra/phi-function.html) +- [Euclidean algorithm for finding the GCD(greatest common divisor)](./algebra/euclid-algorithm.html) - [Sieve of Eratosthenes](./algebra/sieve-of-eratosthenes.html) - [Binary Exponentiation](./algebra/binary-exp.html) - [Balanced Ternary](./algebra/balanced-ternary.html) @@ -53,8 +54,6 @@ especially popular in field of competitive programming.* - [Kirchhoff theorem](./graph/kirchhoff-theorem.html) - [Topological sorting](./graph/topological-sort.html) - [Searching for bridges in O(N+M)](./graph/bridge-searching.html) -<<<<<<< HEAD -======= - [Finding a negative cycle in the graph](./graph/finding-negative-cycle-in-graph.html) - [Floyd-Warshall - all shortest paths](./graph/all-pair-shortest-path-floyd-warshall.html) - [Dijkstra - shortests paths from given vertex](./graph/dijkstra.html) @@ -68,9 +67,7 @@ especially popular in field of competitive programming.* ### Sequences - [RMQ task (Range Minimum Query - the smallest element in an interval)](./sequences/rmq.html) - [K-th order statistic in O(N)](./sequences/k-th.html) ->>>>>>> e-maxx-eng/master --- [Information for contributors](./contrib.html) and [Test-Your-Page form](./test.php) - From e0413265295640ac863887ec5d630c359d91b0ea Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Wed, 11 Jan 2017 11:52:58 +0530 Subject: [PATCH 8/8] Fixed issues --- img/euclid1.png | Bin 1364 -> 0 bytes img/euclid2.png | Bin 2048 -> 0 bytes img/euclid3.png | Bin 787 -> 0 bytes img/euclid4.png | Bin 643 -> 0 bytes img/euclid5.png | Bin 974 -> 0 bytes img/euclid6.png | Bin 850 -> 0 bytes img/euclid7.png | Bin 1002 -> 0 bytes img/euclid8.png | Bin 1030 -> 0 bytes src/algebra/euclid-algorithm.md | 54 +++++++++++++++++++------------- 9 files changed, 33 insertions(+), 21 deletions(-) delete mode 100644 img/euclid1.png delete mode 100644 img/euclid2.png delete mode 100644 img/euclid3.png delete mode 100644 img/euclid4.png delete mode 100644 img/euclid5.png delete mode 100644 img/euclid6.png delete mode 100644 img/euclid7.png delete mode 100644 img/euclid8.png diff --git a/img/euclid1.png b/img/euclid1.png deleted file mode 100644 index 4902f1959f2a6acfccb0388d63bec2945921925c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1364 zcmV-a1*`grP)FMbJ z007+F+>(-#b#-;VzP^NnggQDpSXfxi&COWs)(&@_@}LL-vNIBec-Tz1OJeTM{^q1i zHZkW$9|NcavpSyt!=sh;Qx;_!7`5~#Br1S{ zq=CkT*HUT@cwO1WOiAcP5;PdP805$-xFs$SYqh<@U6?zoq#R~zgeA}O4v0sfH>!i7 z#a_L!J-NI!4*K>HD(Bcm#t_^rgl0P)5%5f(!G|77NF~DMAw~LjSS-Y}_{QYcc9J6V z>W9yccGhfD9nNN!Mm9?e89-HUG`PH#riaSdx;u1a2i(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1E&3A7f-t@{NxTHGCkhCH%TS^6p6pX6DohQk?Aqsn`D05 z{V@LUYDcC=c;Uvv`ta(%ZDTwob~bzJ)sbc`wr6fkzkc*FUOB{0fM)+Lc(9%)X6u=1 zdDHzc9_Vhg;zp*;Bjww%W3zONU-IzxjS3ISUTOkeY-sm>%=7g3)I39t&)ursU_^oTdf5%h^?E?9)9<}iw(qWr(eEEq6JV0=#GSX6q0ALRKw*~`fku(BzFlFpciroLb7dXErtq}EE;YWEsi%Cu=1H@%t;RL%dGW(r# zYh3L$%!})xT44*Otni~e-^5z9lL6u!MEhaeJc5=q2N6ygQa-8`bXXL|5A*zp?`0ai zj(B3rCGWThk7|Vz{3y={6V6>Pb03dug}vYXD9^X5v?{hGnER<}fdp$YqH=;VV0wJkXn*H5*ATb^BleJcDg&p+ag)7AoE9{g9B9HEQ?d`r7;{X=7^ zG|n|~(1kZMGZ&iNL&NC^FWuh?e-tBjf**F6TqjPWO;vcvxAcdwP8?+pf)D;-)8zTh wYd7uP4vDTTv6Clh%w5#~a_T*!{twF%e>^%Eyxd$z=>Px#07*qoM6N<$f?;*mIRF3v diff --git a/img/euclid6.png b/img/euclid6.png deleted file mode 100644 index 5a7e0d720d8c82ba644b57757eaa7d42a1bb36eb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 850 zcmV-Y1FigtP)FMbJ z006MCu!Mw!l9G}vEG$@9SREZ5&CSi++}w3_b-up7Nl8f#4h}jxI-#MVW@cu@&FD4& z00P=cL_t(Y4W(Awf~z15jDSi&k^leNlM6zu_H4JyLmNXFCzFsC;JyyL``&qSe7EyA zf40Y+5B;&YZoV2JkVFwNz}!g=I&`q=X^x!3^0I)J{hEV+(Vhg#kD8*<(& zm1VTVH{4noUa;~e4h#t`zEzI)G6p7rDJ4xNCy$cs-1=e-Azcc~h=XQyDMrstbfh4D ztf?Lv(7{^#z}h)9Zw``aIa^jN8I~p12uMt#L@fz1Ne@p77^fO>HtYoPWDXFI$p^&s z@EJUJLM{~SMeYH1!(xr%PANIt(nh(Zl4P>S(#AsRCXsul3z2sP44h-3$hPi?6K^Jw zVxqULqS{P#7+~$J!P=-^gn2UeNmE?`P zT#5(8D>+#BupBX9^o zkGo#qS43B*EkK(06xD=ESkf&9Oy7NCF?sfI;jk4?B8GD9_sPpWLzR$&BgkK+qg{ft z^uUVHx*@9De4^uQcy%y+l(S-nF`L?!d<}ARVPt7@*%-BFmei~kZam+cJg6VmAlD|@ zInZvg$CZt<`rm?o=Wx%$*`9M-aIV%DfyQet|GHpY_!04?rT-Kk4qtx6+-C}KWax7E%CY@5C8xG07*qoM6N<$g1h;c!vFvP diff --git a/img/euclid7.png b/img/euclid7.png deleted file mode 100644 index 7e16d4826e277b8c9a040ac4a7f29ac82c3e019d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1002 zcmVFMbJ z007+F+>(-#b#-;VzP^NnggQDpSXfxi&COaN1%|AEsPa_J#l*W4?ao@M*~0cAn7I zKh-{iKh6Bo*Jm&l&`HtCJ(HzR?`jQ+1dJT6T&1}3WuKg-I*~BxL`EoQCVm* zrNCNEHylq1jcz^0?zt9qJ`Ou8;EK`gCuQxd!WvCC8c$i4TYGL;OPbCvHDuUX0auKI zJSu8u6(*X%x*04v0Oy=+!0Z;_DMvX)II6w&c59vFavmx+>DSnzrA-ND1Iya}Y z90#I{XC7N+=K2LAP2gvmN-Kg>qSYiH* z6wYa}0QY%wb$fmr)WtMGjLEYC})Aa z*7Hdq&f1-`;*GNru*R^wd!*Kx%g*pi$>gawO|UNsk6ImB3+zlT{gd;M&14AYugEa* z?>!p{9nJDl=WGP5 zF^srJYMsq;)>q2eOQuBEFw$h#loH=@mXaCJo)k9P3O>1L3X5?vHellNtw9~I(^ea! zuT14^1Y9u%t0FPmnARCy+t@CVl}s7iX_^o3{qE_lKrQ^PNx{?QxXOOmi0{gg&~@14 zy0r0pi7_ZYMhLH*4T1$zu;z-yYEztE7T=rA1u2?{BKh=Wy)2W*;#{pM|H@hT@&VaA zTof$uKN+XI9ELI$Bx`BW@KbUadD6gtFJ}+HJTqf;B*j%lNV1~XRAuJwMpL=i030V zmrW_g+tw6h|G$p92x&<7|J$`^<{7xdmgqlyS54vbdgb4}Z(=O%k~D+=3;ZVDNHc5t Y2SA}6*jO9G`2YX_07*qoM6N<$g7sbB3;+NC diff --git a/img/euclid8.png b/img/euclid8.png deleted file mode 100644 index fad67553ce5178686ddbde251b1f9b97e0e944aa..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1030 zcmV+h1o``kP)FMbJ z007O+&9Jbrb#-;2p`m7GW=Tm&l9H0#+}tcIEWWBlB`6%br%i_V#I-bUk2m zulGK#$K>X5nsh#m$u%7cwQG;^ z9d3AArZ4g1c3Yu<$)li(af{8iZGWsTS>A9|ojfY!q#jE{s+)&-TERBJ(pY2`29E=} zKfKCt{}ET8qYO+0rdY}ebl*KKlsQVS@`U}8a^>pehI^daln1}H32SrgL?mRhmxV5f2rYleBx zUP&!|RMS#g$%%d(Z&r?59J*n;4Pu(XQYGe6wNp_d-&uXj@u(JZ$$xAYjuD8eHo{Os zOfZ1KEz->}=vlX(J3PSj>YaGDX5>EbD521^p=Sn-w1J~>Hhw`ERe z4F9qo#SSf3ch1C)*WiB>F_|ugi3yhADvpj|lquvi_OHMR>~?r8BGk-?X*5!SC4cqI z2e#qKG;%r*PW38JjcVnKx7#l&{X3PGCsWDkG&mKhI4g-Os<2nL7N2LeIys#Orxaf$ zkwmfDXWZG*sf*R>e!^OCJ+N8{DJydN2{>iY2F%o^ zG{R4qxgCIGFH^~B6P#oRK2~|5X>; 0$. +For the proof of correctness, we need to show $gcd(a, b) = gcd(b, a \mod b)$ for all $a \geq 0$, $b > 0$. -We will show that the quantity on the left side of the equation, can be written as the value on the left. Obviously, this would mean that the left and right sides are the same and prove Euclid's algorithm. +We will show that the quantity on the left side of the equation, divides the quantity on it's right. Obviously, this would mean that the left and right sides are equal and prove Euclid's algorithm. Let $d = gcd(a, b)$. Then, by definition $d\mid a$ and $d\mid b$. New writing the remainder on dividing $a$ by $b$: -![image](&imgroot&/euclid3.png) +$$ +a \mod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor\qquad +$$ But then, it follows: -![image](&imgroot&/euclid4.png) +$$ +d \mid (a \mod b) +$$ -So, remembering the statement $d\mid b$, we obtain the system: +So, remembering the statement $d \mid b$, we obtain the system: -![image](&imgroot&/euclid5.png) +$$ +\cases{d \mid b,\cr d \mid (a \mod b)\cr} +$$ -We now use the following fact: if for any three numbers $p$, $q$, and $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get: +We now use the fact that if for any three numbers $p$, $q$, and $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get: -![image](&imgroot&/euclid6.png) +$$ +d \mid gcd(b, a \mod b) +$$ Or, by substituting $d$ by it's definition($gcd(a, b)$) we get: -![image](&imgroot&/euclid7.png) +$$ +gcd(a, b) \mid gcd(b, a \mod b) +$$ So, we have shown that the left side divides the right. The second half of the proof is similar. @@ -95,9 +107,11 @@ Given that Fibonacci numbers grow exponentially(as a constant with respect to $n ## LCM(Least common multiple) -Calculating least common multiple (least common multiplier, lcm) is reduced to calculating $gcd$ with the following simple statement: +Calculating least common multiple (least common multiplier or LCM) is reduced to calculating $gcd$ with the following simple statement: -![image](&imgroot&/euclid8.png) +$$ +lcm(a, b) = \dfrac{a \cdot b}{gcd(a, b)} +$$ Thus, the calculation of the LCM can also be done using the euclidean algorithm with the same time complexity @@ -108,12 +122,10 @@ int lcm (int a, int b) { ``` (here it is advantageous to first divide $gcd$ and then multiply by $b$, as it helps avoid overflow in some cases) -##Literature +## Literature - Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005] -## Problems +## Practice Problems - [GCD and LCM[easy]](https://www.codechef.com/problems/FLOW016) - -####Note: Translated by [NibNalin](http://codeforces.com/profile/NibNalin) \ No newline at end of file