<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>數學 Math | 奧數牧人巷</title><link>https://hk7math.netlify.app/tag/%E6%95%B8%E5%AD%B8-math/</link><atom:link href="https://hk7math.netlify.app/tag/%E6%95%B8%E5%AD%B8-math/index.xml" rel="self" type="application/rss+xml"/><description>數學 Math</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en</language><copyright>HK7Math © 2022</copyright><lastBuildDate>Fri, 14 Feb 2020 10:52:38 +0800</lastBuildDate><image><url>https://hk7math.netlify.app/media/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_2.png</url><title>數學 Math</title><link>https://hk7math.netlify.app/tag/%E6%95%B8%E5%AD%B8-math/</link></image><item><title>JS | 位元運算 Bitwise Operator</title><link>https://hk7math.netlify.app/post/js-bitwise-or/</link><pubDate>Fri, 14 Feb 2020 10:52:38 +0800</pubDate><guid>https://hk7math.netlify.app/post/js-bitwise-or/</guid><description>&lt;p>The following is my solution regarding a CodeSignal Problem &lt;a href="https://app.codesignal.com/challenge/BRW53NHcPd238GxxB" target="_blank" rel="noopener">addTwoDigits&lt;/a>&lt;/p>
&lt;p>1st Draft (61 Chars) makes use of reduce&lt;/p>
&lt;pre>&lt;code class="language-javascript">addTwoDigits=n=&amp;gt;
(&amp;quot;&amp;quot;+n).split(&amp;quot;&amp;quot;).reduce((x,y)=&amp;gt;
x+Number(y)
,0)
&lt;/code>&lt;/pre>
&lt;p>2nd Draft (49 Chars) deploys a recursive skill&lt;/p>
&lt;pre>&lt;code class="language-javascript">f = addTwoDigits = n =&amp;gt; n&amp;lt;10
? n
: n%10 + f(Math.floor(n/10))
&lt;/code>&lt;/pre>
&lt;p>It turns out I ignored the constraints that the number has only 2 digits&amp;hellip; but I learnt a new trick from the best solutionsss&lt;/p>
&lt;pre>&lt;code class="language-javascript">addTwoDigits = n =&amp;gt; n%10 + n/10|0
&lt;/code>&lt;/pre>
&lt;p>After some &lt;a href="https://stackoverflow.com/questions/654057/where-would-i-use-a-bitwise-operator-in-javascript" target="_blank" rel="noopener">researches&lt;/a>, there are quite some magical uses for the | bitwise operator, e.g. x|0 can function as:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>Math.floor(x) for postive x&lt;/p>
&lt;/li>
&lt;li>
&lt;p>parseInt(x) for string x&lt;/p>
&lt;/li>
&lt;li>
&lt;p>0 for many special x (NaN, Infinity, null, undefined, [], {} &amp;hellip;)&lt;/p>
&lt;/li>
&lt;/ol></description></item><item><title>JS | 組合算法 Combination Algorithm</title><link>https://hk7math.netlify.app/post/js-math-combination/</link><pubDate>Wed, 15 Jan 2020 00:48:47 +0800</pubDate><guid>https://hk7math.netlify.app/post/js-math-combination/</guid><description>&lt;p>This is originally a post in &lt;a href="https://dev.to/hk7math/javascript-big-combination-problem-1lco" target="_blank" rel="noopener">Dev.to&lt;/a>&lt;/p>
&lt;p>A recent &lt;a href="https://app.codesignal.com/challenge/a8GNsYr8FQxZmMhJj?solutionId=MZQa7pP2nzgzzoSev" target="_blank" rel="noopener">CodeSignal Challenge&lt;/a> was to calculate 1000C500 (mod 1e9+7) and I got defeated =(&lt;/p>
&lt;p>All my trials exceeded the time limit.. Here is the best JS solution by &lt;a href="https://app.codesignal.com/challenge/a8GNsYr8FQxZmMhJj?solutionId=MZQa7pP2nzgzzoSev" target="_blank" rel="noopener">psr&lt;/a>
, could anyone explain what happens in this line??? I learnt ES6 but got no idea about this syntax&amp;hellip;&lt;/p>
&lt;pre>&lt;code class="language-javascript">f[o = n + 1/k] = o in f
&lt;/code>&lt;/pre>
&lt;p>Full solution for reference, please tell me to delete this if I violated any rule&amp;hellip;&lt;/p>
&lt;pre>&lt;code class="language-javascript">f = countWays = (n, k) =&amp;gt; f[o = n + 1/k] = o in f
? f[o]
: k
? n &amp;amp;&amp;amp; (f(--n, k) + f(n, k - 1)) % (1e9 + 7)
: 1
&lt;/code>&lt;/pre>
&lt;hr>
&lt;p>Thanks Barbar&amp;rsquo;s comments in &lt;a href="https://stackoverflow.com/questions/59775224/seeking-help-on-explanation-for-a-big-combination-algorithm/59789925#59789925" target="_blank" rel="noopener">StackOverflow&lt;/a>, I understand the algorithm now.&lt;/p>
&lt;p>I have rewritten the algorithm as follows:&lt;/p>
&lt;pre>&lt;code class="language-javascript">f = nCk = (n, k) =&amp;gt; { //Declare both f and nCk as the same function
let o = n + 1/k //o will be the key of function object f
f[o] = o in f //Define f[o] based on a nested ternary expression
? f[o] //Avoid recalculation if f has key o already
: k==0 //nC0 is always 1
? 1
: n&amp;lt;k //nCk is improper and assigned 0 if n&amp;lt;k
? 0
: f(--n, k) //Do recursion nCk = (n-1)Ck + (n-1)C(k-1)
+ f(n, k - 1)
return f[o] //Done!
}
&lt;/code>&lt;/pre>
&lt;p>Here goes a walkthrough of 5C2&lt;/p>
&lt;pre>&lt;code>f(n,k) n k o f[o]
f(5,2) 5 2 5.5 f[5.5]=f(4,2)+f(4,1) //=10
f(4,2) 4 2 4.5 f[4.5]=f(3,2)+f(3,1) //=6
f(3,2) 3 2 3.5 f[3.5]=f(2,2)+f(2,1) //=3
f(2,2) 2 2 2.5 f[2.5]=f(1,2)+f(1,1) //=1
f(1,2) 1 2 1.5 f[1.5]=f(0,2)+f(0,1) //=0
f(0,2) 0 2 0.5 f[0.5]=0
f(0,1) 0 1 1 f[1]=0
f(1,1) 1 1 2 f[2]=f(0,1)+f(0,0) //=1
f(0,0) 0 0 Inf f[Inf]=1 //Inf aka Infinity
f(2,1) 2 1 3 f[3]=f(1,1)+f(1,0) //=2
f(1,0) 1 0 Inf f[Inf]=1
f(3,1) 3 1 4 f[4]=f(2,1)+f(2,0) //=3
f(n,0) n 0 Inf f[Inf]=1
f(4,1) 4 1 5 f[5]=f(3,1)+f(3,0) //=4
&lt;/code>&lt;/pre>
&lt;p>P.S. I got a few takeaways when investigating into this algorithm&lt;/p>
&lt;ol>
&lt;li>
&lt;p>Double declaration of function on the same line as a trick for recursion&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Immediate use of a key with its value just assigned&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Infinity can be used as a key of an object(!)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Syntax o in f checks if object f has the key o&lt;/p>
&lt;/li>
&lt;/ol></description></item></channel></rss>