<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Meraj Ahmed]]></title><description><![CDATA[Meraj Ahmed is blogger, write articles about code, mathematics, algorithm and interesting stuff. Follow Meraj Ahmed.  ]]></description><link>https://code.merajahmed.in</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 14:08:49 GMT</lastBuildDate><atom:link href="https://code.merajahmed.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Sum of Divisor (Algo + Code) Part - 1]]></title><description><![CDATA[What is the divisor of a number N?
Any number k that divides N completely and leaves 0 as the remainder is a divisor of N
Example:
Divisor of 10 = 1, 2, 5, 10
Divisor of 74 = 1, 2, 37, 74
Sum of the divisor

Let  \( \sigma{(n)}  \) denotes the sum of...]]></description><link>https://code.merajahmed.in/sum-of-divisor-algo-code-part-1</link><guid isPermaLink="true">https://code.merajahmed.in/sum-of-divisor-algo-code-part-1</guid><category><![CDATA[Mathematics]]></category><category><![CDATA[C++]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Meraj Ahmed]]></dc:creator><pubDate>Fri, 14 May 2021 12:29:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1620996473691/rkTTq0z0a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="what-is-the-divisor-of-a-number-n">What is the divisor of a number N?</h3>
<p>Any number k that divides N completely and leaves 0 as the remainder is a divisor of N</p>
<pre><code><span class="hljs-attr">Example:</span>
<span class="hljs-string">Divisor</span> <span class="hljs-string">of</span> <span class="hljs-number">10</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-number">2</span><span class="hljs-string">,</span> <span class="hljs-number">5</span><span class="hljs-string">,</span> <span class="hljs-number">10</span>
<span class="hljs-string">Divisor</span> <span class="hljs-string">of</span> <span class="hljs-number">74</span> <span class="hljs-string">=</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-number">2</span><span class="hljs-string">,</span> <span class="hljs-number">37</span><span class="hljs-string">,</span> <span class="hljs-number">74</span>
</code></pre><h3 id="sum-of-the-divisor">Sum of the divisor</h3>
<blockquote>
<p>Let  \( \sigma{(n)}  \) denotes the sum of the divisor of N.</p>
</blockquote>
<p> \( \sigma{(10)}  \) = 1 + 2 + 5 + 10 = 18</p>
<p> \( \sigma{(74)}  \) = 1 + 2 + 37 + 74 = 114</p>
<h3 id="how-to-find-the-sigman">How to find the \( \sigma{(n)}  \)</h3>
<p>Naive solution:<br /></p>
<ul>
<li>Loop i through 1 to n<br /></li>
<li>check if i divides n,<br /></li>
<li>if yes, then sum such i's<br /></li>
</ul>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sumOfDivisor</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span></span>{
    <span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++){
        <span class="hljs-keyword">if</span>(n % i == <span class="hljs-number">0</span>) sum = sum + i;
    }
    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p><strong>The time complexity of the above code is O(n).</strong></p>
<p><strong>Can we optimize this code</strong>: <em>The answer is yes.</em></p>
<p>If n has a factor k, then another factor will be \( \frac{n}{k} \). <br />
It is easy to see that if one factor is less than \(  \sqrt[]{n}  \), then another factor will be greater than \(  \sqrt[]{n}  \) (<strong>Think, why !!</strong>)</p>
<p>Also, take extra care if N is a perfect square,  \(  \sqrt[]{n}  \) is also its factor, don't double count it</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sumOfDivisor</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span></span>{
    <span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> k = <span class="hljs-built_in">sqrt</span>(n);
    <span class="hljs-keyword">if</span>(k * k == n) sum += k;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; k; i++){
        <span class="hljs-keyword">if</span>(n % i == <span class="hljs-number">0</span>) sum = sum + i + (n / i);
    }
    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p><strong>The time complexity of the above code is O(\(  \sqrt[]{n}  \)).</strong></p>
<p><strong>Can we optimize the above code further:</strong> <em>The answer is yes.</em></p>
<p>The first observation is that if N is an odd number then its factor can't be any even number.
So, we can increment by 2.</p>
<p><em>Part - 2 is coming soon</em></p>
<p><em>Like and Follow  <a target="_blank" href="https://twitter.com/Aladin_Chirag">Me</a>  on Twitter</em></p>
]]></content:encoded></item><item><title><![CDATA[There are infinite Prime Numbers]]></title><description><![CDATA[Consider the below statement


There is an Infinite number of Prime Numbers.

I know you guys will think that it is obvious. 
But if you think deep, you will notice that it is not straightforward because as the number increases, the density of the pr...]]></description><link>https://code.merajahmed.in/there-are-infinite-prime-numbers</link><guid isPermaLink="true">https://code.merajahmed.in/there-are-infinite-prime-numbers</guid><category><![CDATA[Mathematics]]></category><category><![CDATA[Math]]></category><category><![CDATA[coding]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Meraj Ahmed]]></dc:creator><pubDate>Thu, 13 May 2021 18:14:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1620998448658/7U3ncDDB-.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Consider the below statement
</strong></p>
<blockquote>
<p>There is an Infinite number of Prime Numbers.</p>
</blockquote>
<p>I know you guys will think that it is obvious. 
But if you think deep, you will notice that it is not straightforward because as the number increases, the density of the prime number decreases. If the number is large enough, then we may not get any prime number after that. Moreover, for large numbers, the chances that it has a factor are more because more numbers are candidates for its factor.</p>
<h3 id="how-to-prove-the-infiniteness-of-prime-numbers">How to prove the infiniteness of prime numbers?</h3>
<p>There are many methods, but the one I love most is <strong>Euclid's method</strong>. It is based on the <strong>Proof By Contradiction</strong> method.</p>
<p>It follows as following.</p>
<ul>
<li>Assume there is a finite number of prime numbers. Then We can list down all the prime numbers as 
<code>{p1, p2, p3 ... pn}</code>, where pn is the largest prime number.  Note that the size of the list is finite.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1620927838814/E6SXYPcZb.png" alt="carbon (2).png" /></p>
<ul>
<li>Now consider a new no. N as
<code>N = p1 * p2 * p3 ... * pn + 1</code> </li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1620928019109/1ReANqvJx.png" alt="carbon (3).png" /></p>
<ul>
<li>We can see that N is greater than <code>pn</code> (largest prime number in the above list). </li>
<li><p><strong>For N, either of the two cases is true</strong></p>
<ol>
<li>It is a prime number</li>
<li>It is a composite number</li>
</ol>
</li>
</ul>
<ul>
<li><strong>If N is a prime number</strong>:  We got a new prime that was not included in the list.</li>
<li><strong>If N is a composite number</strong>: We can express it as the product of primes. But we can see N is not divisible by either of p1 or p2 .... or pn. So, if N is a composite number, then there must exist a prime factor of N which is not already in the list.</li>
</ul>
<p>In both cases, we can get new prime numbers that are not on the list.</p>
<p>So our assumption that prime numbers are finite is wrong.</p>
<p><strong>So there are an infinite number of primes
</strong></p>
<p><em>Thanks for Reading. Follow  <a target="_blank" href="https://hashnode.com/@meraj">me</a>  for amazing content. Thanks
</em></p>
]]></content:encoded></item></channel></rss>