<?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[The Introvert Coder | Quietly Building Loud Code!]]></title><description><![CDATA[A blog where we simplify complex coding concepts, break down Javascript quirks, and help you write clean, confident code (without all the noise).]]></description><link>https://blog.theintrovertcoder.in</link><image><url>https://cdn.hashnode.com/uploads/logos/5f5b77a3e0b75e58b2952121/7d4969bd-d923-45e8-8d72-55c2411f5b42.png</url><title>The Introvert Coder | Quietly Building Loud Code!</title><link>https://blog.theintrovertcoder.in</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 10:37:13 GMT</lastBuildDate><atom:link href="https://blog.theintrovertcoder.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[What is JavaScript and How It Powers Modern Websites]]></title><description><![CDATA[Picture this 👀 — you click a button. Nothing happens.
You hover over a menu. It doesn't open.
You submit a form. The page just sits there, staring at you.
That's the web without JavaScript — technica]]></description><link>https://blog.theintrovertcoder.in/what-is-javascript-and-how-it-powers-modern-websites</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/what-is-javascript-and-how-it-powers-modern-websites</guid><category><![CDATA[Web Development Tutorials]]></category><category><![CDATA[HTML & CSS Integration]]></category><category><![CDATA[javascript, programming]]></category><category><![CDATA[Beginner JavaScript]]></category><category><![CDATA[Front-end Development]]></category><category><![CDATA[Client-Side Scripting]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Mon, 07 Jul 2025 01:05:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/5f5b77a3e0b75e58b2952121/8c5f6b2e-4730-4e8b-be21-c70ed1bf8917.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Picture this 👀 — you click a button. Nothing happens.</p>
<p>You hover over a menu. It doesn't open.</p>
<p>You submit a form. The page just sits there, staring at you.</p>
<p>That's the web without JavaScript — technically functional, completely lifeless.</p>
<p>Every dropdown, animation, real-time update, and form validation you've ever interacted with online — That's JavaScript doing its job.</p>
<p>In this article, we're covering what JavaScript actually is, how it's different from the languages you might have heard of, where it runs, and why it's still the most relevant language in web development after nearly 30 years.</p>
<p>By the end, you'll have a clear mental model of what JS does and where it fits in the stack.</p>
<hr />
<h2>What we're covering 👇</h2>
<ul>
<li>What a programming language is — and what makes JS different</li>
<li>JavaScript vs ECMAScript — the naming confusion, cleared up</li>
<li>Where JS actually runs — browser, server, and beyond</li>
<li>JavaScript's role in the HTML / CSS / JS trio</li>
<li>What JS can and can't do</li>
<li>Why JS is still the #1 language to learn in 2026</li>
</ul>
<hr />
<h2>JavaScript — the language that makes web pages do things</h2>
<p>At its simplest, JavaScript is a programming language.</p>
<p>A programming language is a set of rules that lets you write instructions a computer can understand and execute.</p>
<p>Python, Java, Ruby — they're all programming languages. So is JavaScript.</p>
<p>But JavaScript has a specific superpower — it's the only language that runs natively inside every web browser. You don't install it. You don't compile anything.</p>
<p>Open Chrome, press F12, click Console, and you're running JavaScript right now.
That's what makes it unique.</p>
<p>Python is great for data science. Java dominates enterprise backends. Ruby is elegant for certain APIs.</p>
<p>But if you want to make a webpage respond to a user clicking a button — JavaScript is the only tool that does that job natively, everywhere, without any setup.</p>
<p>Here's the technical short version, just so you have it in your head:</p>
<ul>
<li><strong>Interpreted</strong> (or more accurately, JIT-compiled — your browser compiles it on the fly as it runs)</li>
<li><strong>Dynamically typed</strong> (you don't declare what type a variable is — we'll cover this properly in a later article)</li>
<li><strong>Single-threaded</strong> (it handles one thing at a time, which sounds limiting until you learn about the event loop — in a later article)</li>
<li><strong>Prototypal inheritance</strong> (its object model is different from Java or Python — in a later article)</li>
</ul>
<p>Don't worry about understanding all of that right now.</p>
<p>What matters at this stage is simple — JavaScript runs in your browser, right now, without you doing anything to set it up.</p>
<hr />
<h2>JavaScript's place in the HTML / CSS / JS trio</h2>
<p>If you're new to web development, you've probably heard these three names together. Here's what they actually do:</p>
<ul>
<li><strong>HTML</strong> gives structure. It says — "there's a heading here, a paragraph there, a button at the bottom."</li>
<li><strong>CSS</strong> handles presentation. It says — "that heading should be blue and 32px, the button should have rounded corners."</li>
<li><strong>JavaScript</strong> handles behaviour. It says — "when someone clicks that button, show this message."</li>
</ul>
<p>Without JavaScript, a web page is like a printed flyer.</p>
<p>It looks nice, it has information, but it can't respond to you. JavaScript is what turns a document into an application.</p>
<hr />
<h2>JavaScript vs ECMAScript — the naming confusion 🤔</h2>
<p>You'll see both terms used. They're related but not the same thing.</p>
<p><strong>JavaScript</strong> is the language you write. <strong>ECMAScript</strong> (often abbreviated as ES) is the official specification — the formal rulebook that defines what JavaScript should do. The people at <a href="https://tc39.es/">TC39</a> (a committee of language experts) maintain that spec and release new versions of it periodically.</p>
<p>When you hear "ES6" or "ES2015", that means the 2015 version of the ECMAScript specification. Features like <code>let</code>, <code>const</code>, arrow functions, and template literals all came from that release.</p>
<p>You can read the <a href="https://tc39.es/ecma262/">full ECMAScript spec</a> if you want to go deep — but fair warning, it reads like a legal document.</p>
<p>So, ECMAScript is the standard.</p>
<p>JavaScript is the implementation of that standard that your browser runs.</p>
<p>Same idea, different contexts.</p>
<p>You'll write JavaScript. Occasionally you'll read about ECMAScript when trying to understand why a specific feature exists or behaves the way it does.</p>
<hr />
<h2>Where JavaScript actually runs</h2>
<p>JavaScript started in the browser. That's still where most people learn it. But it runs in several places now.</p>
<p><strong>In the browser (client-side):</strong>
This is the original home of JavaScript.</p>
<p>When a webpage loads, the browser parses the HTML, finds the <code>&lt;script&gt;</code> tag, and hands the JavaScript to its built-in engine to execute.</p>
<p>Chrome uses V8. Firefox uses SpiderMonkey. Safari uses JavaScriptCore.</p>
<p>Each is different under the hood, but they all run the same JavaScript you wrote.</p>
<pre><code class="language-javascript">// This runs in the browser — the engine finds the button and waits for a click
document.querySelector("button").addEventListener("click", () =&gt; {
  console.log("Button clicked!"); // → appears in your browser console
});
</code></pre>
<p>Each browser has its own engine, but the JavaScript you write works across all of them. That cross-browser consistency is something the ECMAScript spec is specifically designed to guarantee.</p>
<p><strong>On the server (Node.js, Deno, Bun):</strong>
In 2009, Node.js took the V8 engine out of Chrome and put it on a server.</p>
<p>Now JavaScript can read files, talk to databases, and handle HTTP requests — the things that server-side languages like Python or Ruby traditionally handled. Same language, completely different environment.</p>
<pre><code class="language-javascript">// This runs in Node.js on a server — not available in a browser
const fs = require("fs");
const content = fs.readFileSync("data.txt", "utf8");
console.log(content); // → reads a file from the server's filesystem
</code></pre>
<p>The key distinction — the browser gives JavaScript access to the DOM, <code>fetch</code>, <code>alert</code>, and browser-specific APIs. Node.js gives it access to the filesystem, network sockets, and server APIs. The core language is identical. </p>
<p>What's available depends on where it's running.</p>
<hr />
<h2>What JavaScript can and can't do</h2>
<p>This trips up a lot of beginners. </p>
<p>JavaScript is powerful, but it has real limits — and most of them are there on purpose.</p>
<p><strong>What it can do:</strong></p>
<ul>
<li>Respond to user events: clicks, keypresses, form submissions, scroll position</li>
<li>Update the page dynamically without a full reload — the foundation of modern SPAs</li>
<li>Make network requests to APIs via <code>fetch</code> and handle the responses</li>
<li>Store data in the browser via <code>localStorage</code> or cookies</li>
<li>Run server logic, query databases, handle file I/O — when running in Node.js</li>
</ul>
<p><strong>What it can't do (in a browser):</strong></p>
<ul>
<li>Access your local filesystem directly (⚠️ this is a security feature, not a limitation)</li>
<li>Run multiple tasks truly in parallel — it's single-threaded; asynchronous execution is not the same as multi-threaded</li>
<li>Do anything the browser's security sandbox doesn't explicitly permit</li>
</ul>
<p>The filesystem restriction is intentional. If a random webpage could read your files just by you visiting it, that would be a serious problem. </p>
<p>The browser sandbox keeps JavaScript contained to what it's permitted to touch. You'll appreciate this design decision more once you start thinking about security.</p>
<hr />
<h2>Adding JavaScript to a webpage</h2>
<p>There are three ways to include JavaScript in a webpage. You'll see all three in the wild, so knowing the difference matters.</p>
<p><strong>Inline</strong> — directly on an HTML element. Works for one-off tests, doesn't scale to anything real. Don't build real projects this way.</p>
<pre><code class="language-javascript">// ❌ Before: inline event handlers get unmanageable fast
&lt;button onclick="alert('clicked')"&gt;Click me&lt;/button&gt;
</code></pre>
<p><strong>Internal script</strong> — a <code>&lt;script&gt;</code> block inside your HTML file. Fine for learning and quick experiments.</p>
<pre><code class="language-html">&lt;!-- Works, but mixing JS and HTML becomes painful at any real scale --&gt;
&lt;script&gt;
  console.log("Page loaded"); // → shows in your browser console
&lt;/script&gt;
</code></pre>
<p><strong>External file</strong> — a separate <code>.js</code> file linked from your HTML. This is how real projects work.</p>
<pre><code class="language-html">&lt;!-- In index.html, inside &lt;body&gt; — the right way to do it --&gt;
&lt;script src="script.js" defer&gt;&lt;/script&gt;
</code></pre>
<pre><code class="language-javascript">// In script.js — clean, separate, and actually maintainable
document.querySelector("h1").textContent = "Hello from an external file";
// ✅ JS lives here, HTML lives in its own file — no spaghetti
</code></pre>
<p>💡 That <code>defer</code> attribute tells the browser, "don't run this script until the HTML has fully loaded." </p>
<p>Without it, your JavaScript might try to find a button that doesn't exist in the DOM yet — and fail silently.</p>
<p>The external file approach is the right default. It keeps your HTML clean, makes your JavaScript testable, and keeps your future self from having a bad day.</p>
<hr />
<h2>Frontend vs backend JavaScript</h2>
<p>Same language. Completely different environment and purpose.</p>
<p><strong>Frontend (browser):</strong> 
JavaScript here controls what the user sees and interacts with. </p>
<p>Frameworks like React, Vue, and Svelte are all JavaScript that runs in the browser and manages the UI layer.</p>
<p><strong>Backend (Node.js):</strong> 
JavaScript here handles the server side — routing, database queries, authentication, and API logic. </p>
<p>Frameworks like Express and NestJS run on top of Node.js and bring structure to that work.</p>
<p>The reason this distinction matters early — when you hit a wall because "JavaScript can't access the filesystem", it's because you're thinking about browser JavaScript. When someone says "just use Node for that", they mean switch environments — same language, different capabilities. Context is everything.</p>
<hr />
<h2>Why JavaScript is still the language to learn in 2026</h2>
<p>Some questions come up endlessly in developer communities — "Is JavaScript dying? Should I just learn Python? Is TypeScript replacing it?"</p>
<p>Short answers: No. Depends what you want to build. TypeScript compiles to JavaScript, so they coexist rather than compete.</p>
<p>JavaScript consistently tops developer surveys and GitHub usage data, and it remains one of the most in-demand skills in engineering job listings globally. </p>
<p>It runs on browsers, servers, mobile apps (React Native), desktop apps (Electron), serverless functions at the edge, and IoT devices. The npm ecosystem — with over two million open-source packages — is the largest package registry that exists.</p>
<p>That's not marketing. That's infrastructure. Knowing JavaScript means you can go full-stack with a single language. That matters when you're learning, and it matters when you're interviewing.</p>
<hr />
<h2>Summary</h2>
<p>JavaScript is the language of interactivity on the web. It runs natively in every browser, powers the server via Node.js, and sits at the centre of the most active developer ecosystem in the world. It has real quirks and deliberate limitations — and you'll meet all of them in this series. But it's the foundational skill for anyone building for the web, and everything in the JavaScript Mastery Series builds on exactly what you just read.</p>
<hr />
<h2>TL;DR</h2>
<ul>
<li>JavaScript is the only language that runs natively in every browser — no setup required</li>
<li>ECMAScript is the official spec; JavaScript is the implementation your browser runs</li>
<li>In the browser, JS controls the UI. In Node.js, JS handles the server. Same language, different environment</li>
<li>Use external <code>.js</code> files with <code>defer</code> — not inline scripts</li>
<li><strong>JavaScript is still the most employable, versatile language for anyone building for the web in 2026</strong></li>
</ul>
<hr />
<p><strong>Up next →</strong> <a href="#">JavaScript's History Is a Mess — And That's Why It's Great</a> — why understanding JS's origin story explains half of its weird decisions.</p>
<p><strong>Also worth reading →</strong> <a href="#">What Can We Actually Build With JavaScript?</a> — a practical look at what you can make once you know the basics.</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #17 - Number letter counts]]></title><description><![CDATA[Problem
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...]]></description><link>https://blog.theintrovertcoder.in/project-euler-17-number-letter-counts</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-17-number-letter-counts</guid><category><![CDATA[2Articles1Week]]></category><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Wed, 18 Jan 2023 00:26:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673689608316/cceabf2e-5989-43db-acb0-cc0e9cf9860c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>If the numbers <code>1 to 5</code> are written out in words: <code>one</code>, <code>two</code>, <code>three</code>, <code>four</code>, <code>five</code>, then there are <code>3 + 3 + 5 + 4 + 4 = 19</code> letters used in total.</p>
<p>If all the numbers from <code>1 to 1000</code> (one thousand) inclusive were written out in words, how many letters would be used?</p>
<p><strong>NOTE:</strong> Do not count spaces or hyphens. For example, <code>342</code> (three hundred and forty-two) contains 23 letters and <code>115</code> (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>Well, writing the numbers in words form and counting the number of alphabets used is our problem here. Looks interesting, right?!</p>
<p>Yeah, we have to write out all the numbers from <code>1</code> to <code>1000</code> in words. And count the total number of alphabets, ignoring spaces and hyphens.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>Yes, this problem can be approached in N different ways.</p>
<p>The way I approached this(inspired by a freecodecamp article/video 😎) is to split the number into thousands, hundreds and tens. Convert the number to words, append it to a string and then count the number of alphabets using <code>String.prototype.length</code>.</p>
<p>The <a target="_blank" href="https://www.hackerrank.com/contests/projecteuler/challenges/euler017/problem">Hackerrank</a> version of this problem is quite different. Their word spelling, using space and <code>and</code> rules vary too. The Numbers in their test cases are quite big - <code>0 &lt;= N &lt;= 1,000,000,000,000</code></p>
<p>For the Hackerrank solution, I used <code>Map</code> object in Javascript and split the given number in <code>100s</code> before combining them with proper denominations like <code>billion</code>, <code>million</code> etc.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> words = {
    <span class="hljs-string">'units'</span>: [<span class="hljs-string">''</span>, <span class="hljs-string">'one'</span>, <span class="hljs-string">'two'</span>, <span class="hljs-string">'three'</span>, <span class="hljs-string">'four'</span>, <span class="hljs-string">'five'</span>, <span class="hljs-string">'six'</span>, <span class="hljs-string">'seven'</span>, <span class="hljs-string">'eight'</span>, <span class="hljs-string">'nine'</span>,
        <span class="hljs-string">'ten'</span>, <span class="hljs-string">'eleven'</span>, <span class="hljs-string">'twelve'</span>, <span class="hljs-string">'thirteen'</span>, <span class="hljs-string">'fourteen'</span>, <span class="hljs-string">'fifteen'</span>, <span class="hljs-string">'sixteen'</span>, <span class="hljs-string">'seventeen'</span>, <span class="hljs-string">'eighteen'</span>, <span class="hljs-string">'nineteen'</span>],
    <span class="hljs-string">'tenth'</span>: [<span class="hljs-string">''</span>, <span class="hljs-string">'ten'</span>, <span class="hljs-string">'twenty'</span>, <span class="hljs-string">'thirty'</span>, <span class="hljs-string">'forty'</span>, <span class="hljs-string">'fifty'</span>, <span class="hljs-string">'sixty'</span>, <span class="hljs-string">'seventy'</span>, <span class="hljs-string">'eighty'</span>, <span class="hljs-string">'ninety'</span>]
}

<span class="hljs-keyword">let</span> numToWordsCount = <span class="hljs-function">(<span class="hljs-params">limit</span>) =&gt;</span> {

    <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt;= limit; i++) {

        <span class="hljs-keyword">let</span> num = i
        <span class="hljs-keyword">let</span> numStr = <span class="hljs-string">''</span>

        <span class="hljs-keyword">let</span> thousands = <span class="hljs-built_in">Math</span>.floor(num / <span class="hljs-number">1000</span>)
        <span class="hljs-keyword">if</span> (thousands &gt; <span class="hljs-number">0</span>) {
            numStr = numStr + words[<span class="hljs-string">'units'</span>][thousands] + <span class="hljs-string">'thousand'</span>
            num = num - (thousands * <span class="hljs-number">1000</span>)
        }

        <span class="hljs-keyword">let</span> hundreds = <span class="hljs-built_in">Math</span>.floor(num / <span class="hljs-number">100</span>)
        <span class="hljs-keyword">if</span> (hundreds &gt; <span class="hljs-number">0</span>) {
            numStr = numStr + words[<span class="hljs-string">'units'</span>][hundreds] + <span class="hljs-string">'hundred'</span>
            num = num - (hundreds * <span class="hljs-number">100</span>)
            <span class="hljs-keyword">if</span> (num &gt; <span class="hljs-number">0</span>) {
                numStr = numStr + <span class="hljs-string">'and'</span>
            }
        }

        <span class="hljs-keyword">if</span> (num &lt; <span class="hljs-number">20</span>) {
            numStr = numStr + words[<span class="hljs-string">'units'</span>][num]
        } <span class="hljs-keyword">else</span> {
           <span class="hljs-keyword">let</span> tens = <span class="hljs-built_in">Math</span>.floor(num / <span class="hljs-number">10</span>)
            numStr = numStr + words[<span class="hljs-string">'tenth'</span>][tens]
            num = num - (tens * <span class="hljs-number">10</span>)
            numStr = numStr + words[<span class="hljs-string">'units'</span>][num]
        }

        sum = sum + numStr.length
    }

    <span class="hljs-keyword">return</span> sum
}

<span class="hljs-built_in">console</span>.log(numToWordsCount(<span class="hljs-number">1000</span>))
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/17"><strong>17</strong></a></p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
<p><strong>Reference:</strong></p>
<ul>
<li>More on <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map">Javascript Maps at MDN.</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #16 - Power digit sum]]></title><description><![CDATA[Problem
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?

Problem Description
The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....]]></description><link>https://blog.theintrovertcoder.in/project-euler-16-power-digit-sum</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-16-power-digit-sum</guid><category><![CDATA[2Articles1Week]]></category><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Tue, 17 Jan 2023 00:58:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673623445378/4112a506-a18e-4a9b-a0b4-39f6452e75df.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>2<sup>15</sup> = 32768 and the sum of its digits is <code>3 + 2 + 7 + 6 + 8 = 26</code>.</p>
<p>What is the sum of the digits of the number 2<sup>1000</sup>?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>The problem is pretty self-explanatory. 2<sup>15</sup> is <code>32768</code> and the sum of its digits is <code>3 + 2 + 7 + 6 + 8 = 26</code>. So what will be the sum of digits of 2<sup>1000</sup>?</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>While handling bigger power, the resulting large number can not be handled by Javascript.</p>
<p>One approach is to use column multiplication using arrays.</p>
<p>Another approach, which I have opted for is to use <code>BigInt</code> with <code>2 ** n</code>, where <code>n</code> is the power.</p>
<blockquote>
<p><strong><em>BigInt</em></strong> is a built-in <em>object in JavaScript</em> that represents <strong>whole numbers larger than 2<sup>53</sup>.</strong></p>
</blockquote>
<p>Note that, We could have used <code>Math.pow()</code> but <code>BigInt</code> can not be used with a <code>Math</code> object as they expect <code>Number</code> as arguments. And <code>BigInt</code> can't accurately convert to a Number. So we settle with <code>2 ** n</code>.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> digitSum = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> power = <span class="hljs-number">2n</span> ** BigInt(n);
    <span class="hljs-comment">// Convert BigInt into a String by concatenating with an empty string.</span>
    <span class="hljs-keyword">let</span> strNum = <span class="hljs-string">''</span> + power; 

    <span class="hljs-keyword">return</span> strNum.split(<span class="hljs-string">''</span>).map(<span class="hljs-built_in">Number</span>).reduce(<span class="hljs-function">(<span class="hljs-params">acc, curr</span>) =&gt;</span> acc + curr, <span class="hljs-number">0</span>);
}

<span class="hljs-built_in">console</span>.log(digitSum(<span class="hljs-number">1000</span>));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/16"><strong>16</strong></a></p>
<p>For <a target="_blank" href="https://www.hackerrank.com/contests/projecteuler/challenges/euler016/problem"><strong>Hackerrank</strong></a>, the above solution works fine for all 10 test cases.</p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
<p><strong>Reference:</strong></p>
<ul>
<li><em>For more on BigInt, refer to the articles in</em> <a target="_blank" href="https://www.smashingmagazine.com/2019/07/essential-guide-javascript-newest-data-type-bigint/">smashing magazine</a> <em>and</em> <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"><em>MDN</em></a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #15 - Lattice paths]]></title><description><![CDATA[Problem
Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20*20 grid?

Problem Description
We are given a...]]></description><link>https://blog.theintrovertcoder.in/project-euler-15-lattice-paths</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-15-lattice-paths</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Mon, 16 Jan 2023 01:57:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673582949879/21696d61-172a-4309-9dea-5f8f0a309538.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-problem">Problem</h2>
<p>Starting in the top left corner of a <code>2*2</code> grid, and only being able to move to the right and down, there are exactly <code>6</code> routes to the bottom right corner.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672792099816/377824b4-8005-410f-bd42-b54690679723.png" alt class="image--center mx-auto" /></p>
<p>How many such routes are there through a <code>20*20</code> grid?</p>
<hr />
<h2 id="heading-problem-description"><strong>Problem Description</strong></h2>
<p>We are given a <code>2*2</code> grid and the number of paths to travel from the <strong>top-right corner</strong> to the <strong>bottom-left corner</strong> are <code>6</code> routes. Note that it is from the top-right <strong>corner</strong> to the bottom-right <strong>corner</strong> and not from the top-right cell to the bottom-right cell.</p>
<p>In a <code>n*m</code> grid, we have <code>(n + 1) * (m + 1)</code> corners or points through which we trace the path.</p>
<p>At any given point, we can either move <code>RIGHT</code> or <code>DOWN</code>. No other direction is possible.</p>
<p>Now, we must find the number of such paths in a <code>20*20</code> grid.</p>
<hr />
<h2 id="heading-approach"><strong>Approach</strong></h2>
<p>Well, this is another problem that gave me sleepless nights. I had to do some research before figuring out the best solution. 🧾</p>
<p>First let's understand what is a Lattice, and a Lattice Path. 🤔</p>
<h3 id="heading-lattice-points-andamp-lattice-paths">Lattice Points &amp; Lattice Paths:</h3>
<p>A <strong>Lattice point</strong> is a <strong>repeating point arranged in a pattern</strong> like a square, rectangle or hexagon. The below diagram is a 2D square lattice arrangement.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672793344719/87401c09-b596-42ad-b11b-8cafdd0d074d.png" alt="Lattice points in a 2d Square" class="image--center mx-auto" /></p>
<p>Now, the <strong>lattice path</strong> is a <strong>sequence of steps connecting adjacent lattice points.</strong> Also, we restrict the movement to two directions, one-way horizontal and one-way vertical.</p>
<p>Since we are starting <strong>from the top-left corner</strong>, we move <strong>either RIGHT or DOWN.</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673583891609/9cd486de-a958-41ef-a724-b69f52fa630d.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-counting-the-paths">Counting the Paths:</h3>
<p>In the given problem, all possible lattice paths in a <code>2*2</code> grid is calculated as <code>6</code> paths. Remember, we can either go <code>Right</code> or <code>Down</code>.</p>
<p>One way to count lattice paths is by <strong>adding the number of ways in which we can reach each dot in the lattice.</strong> Remember, we can only go RIGHT or DOWN and always start from the same corner.</p>
<p>In the example <code>4x4</code> lattice below, the blue and red paths indicate two possible ways to get to the first bottom-right point. So, we marked it with a <code>2</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673592000895/a5d1bd6a-be87-4596-9955-d14470e51e07.png" alt class="image--center mx-auto" /></p>
<p>In the same way, to reach the next point, we calculate that there are <code>3</code> possible paths(2 from left &amp; 1 from top), marked in blue, green and red.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673593261178/1a4cfc4d-29af-4761-8861-df0e2d705241.png" alt class="image--center mx-auto" /></p>
<p>And for the next point to the right, there are <code>4</code> possible paths (3 from left &amp; 1 from top) and so on.</p>
<p>All the points along the left are marked with <code>1</code>. From the top-left corner to the bottom-left corner, there is only one path available - remember we can either move RIGHT or DOWN. In the same way, all the points along the top are marked with <code>1</code>.</p>
<p>Following this process, we count all paths till the desired point.</p>
<p>We use a few methods to count the lattice paths and can be used to count the paths of a lattice of any size. Let us consider the above <code>4 * 4</code> grid and assign cartesian coordinates to each dot in the lattice for path calculation.</p>
<p>The top-left corner is marked as <code>(0, 0)</code> and the bottom-right corner is marked as <code>(4 4)</code>. Let us count the paths from <code>(0, 0)</code> to <code>(3, 4)</code> in this grid (marked in red).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673607756867/e24bb8d2-1aed-4ed9-8053-09a4f31901ba.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-method-1-recursive-method">Method 1: Recursive method</h3>
<p>In this method, we count the paths recursively from <code>(3, 4)</code> to <code>(0, 0)</code>. That is, from <code>(m, n)</code> to <code>(0, 0)</code>. Using variables, <code>(m, n)</code> is <code>(3, 4)</code>.</p>
<p>On one look at the diagram above, we can say that the number of paths from <code>(0, 0)</code> to <code>(3, 4)</code> is equal to the number of paths from <code>(0, 0)</code> to <code>(2, 4)</code> plus the number of paths from <code>(0, 0)</code> to <code>(3, 3)</code>.</p>
<p>Thus, to count the path at <code>(m, n)</code> we use <code>(0, 0) -&gt; (m-1, n) + (0, 0) -&gt; (m, n-1)</code>.</p>
<p>This way, we calculate the path recursively till <code>(0, 0)</code> and add all the counts. When it is at any of the <code>0</code> coordinates, we return <code>1</code>.</p>
<pre><code class="lang-plaintext">function countPath(m, n)
    if n = 0 or m = 0 then
        return 1 
    else
        return countPath(m-1, n) + countPath(m, n-1)
end function
</code></pre>
<p>But this is too slow to answer in a reasonable time. If time constraints or space constraints are involved, then it would eventually <code>time out</code>. If you are executing this solution in Hackerrank, then it would time out for more than half of your test cases.</p>
<h3 id="heading-method-2-dynamic-programming-or-iterative-method">Method 2: Dynamic Programming or Iterative method</h3>
<p>Instead of starting from <code>m = 20</code> &amp; <code>n = 20</code> and counting all the way to <code>m = 0</code> &amp; <code>n = 0</code>, we start from our base all the way to the desired point, saving our result in a 2D-like array.</p>
<p>This approach is more like dynamic programming, where we use <code>grid[i][j]</code> for every point. I have not tried this method yet.</p>
<h3 id="heading-method-3-combinatorics">Method 3: Combinatorics</h3>
<p>The number of lattice paths from <code>(0, 0)</code> to <code>(m, n)</code> is equal to the number of combinations of <code>m</code> objects out of <code>m + n</code> options. And the formula for counting the number of combinations <code>m</code> out of <code>n</code> objects(n choose m) is,</p>
<p>$${k}C_n = \frac{k!}{(k - n)! * n!} where (k = m + n)$$</p>
<p>At the location <code>(3, 4)</code>, <code>m = 3</code> and <code>n = 4</code>. Then the number of lattice paths is the combination of <code>m = 3</code> out of <code>m + n = (3+4) = 7</code> options.</p>
<p>In other words, it's the number given by <strong>"7 choose 3"</strong>, which is,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673617439186/5ff17803-95ed-4677-b917-1f5967963e3e.png" alt class="image--center mx-auto" /></p>
<p>And that is the number of lattice paths from <code>(0, 0)</code> to <code>(3, 4)</code> in the above <code>4 * 4</code> grid.</p>
<p>Similarly, we can calculate for <code>20 * 20</code> grid.</p>
<h3 id="heading-method-4-binomial-coefficient"><strong>Method 4: Binomial Coefficient</strong></h3>
<p>The count at the point <code>(n, m)</code> corresponds to the binomial coefficient,</p>
<p>$$\binom{n + m}{m} OR \binom{n + m}{n}$$</p>
<p>which is equivalent to the <code>"n + m choose m"</code> or <code>"n + m choose n"</code> expression from the above combinatorics. The formula used is the same as above where <code>k = n + m</code></p>
<p>$$\binom{k}{n} = \frac{k!}{(n - k)! * n!}$$</p>
<h3 id="heading-method-5-pascals-triangle">Method 5: Pascal's Triangle</h3>
<p>There is another method where the binomial coefficient is arranged in pascal's triangle from which we can count the number of lattice paths in the <code>n * m</code> grid. No, I have not tried this method too. You can refer to the wiki link provided at the end of this post for more information.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h2 id="heading-solution"><strong>Solution</strong></h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> latticePathsRecursive = <span class="hljs-function">(<span class="hljs-params">m, n = m</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (m &lt; <span class="hljs-number">0</span> || n &lt; <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-comment">// to make sure of any negatives</span>
    <span class="hljs-keyword">if</span> (m === <span class="hljs-number">0</span> &amp;&amp; n === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    <span class="hljs-keyword">return</span> (latticePathsRecursive(m - <span class="hljs-number">1</span>, n) + latticePathsRecursive(m, n - <span class="hljs-number">1</span>));
}

<span class="hljs-keyword">const</span> latticePathsCombinatorics = <span class="hljs-function">(<span class="hljs-params">m, n</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> factorial = <span class="hljs-function">(<span class="hljs-params">number</span>) =&gt;</span> {
        <span class="hljs-keyword">if</span> (number === <span class="hljs-number">1</span> || number === <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> number * factorial(number - <span class="hljs-number">1</span>);
        }
    }

    <span class="hljs-keyword">let</span> k = m + n;
    <span class="hljs-keyword">let</span> res = factorial(k) / (factorial(k-n) * factorial(n));
    <span class="hljs-keyword">return</span> res;
}

<span class="hljs-built_in">console</span>.log(latticePathsCombinatorics(<span class="hljs-number">4</span>, <span class="hljs-number">4</span>)); <span class="hljs-comment">// 70</span>
<span class="hljs-built_in">console</span>.log(latticePathsCombinatorics(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)); <span class="hljs-comment">// 35</span>
<span class="hljs-built_in">console</span>.log(latticePathsCombinatorics(<span class="hljs-number">20</span>, <span class="hljs-number">20</span>)); <span class="hljs-comment">// 137846528820</span>
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/15">15</a></p>
<p>For <a target="_blank" href="https://www.hackerrank.com/contests/projecteuler/challenges/euler015/problem"><strong>Hackerrank</strong></a>, the recursive method timed out for 8 out of 11 test cases. But the combinatorics method worked fine along with <code>BigInt</code> and the modulo (1000000007) given in the problem.</p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other project Euler solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
<p><strong>Reference note:</strong></p>
<ul>
<li><p>Most of the diagrams here are from <em>stemhash.com. My examples are based on these diagrams.</em></p>
</li>
<li><p>Refer to these wiki pages for more on <a target="_blank" href="https://en.wikipedia.org/wiki/Lattice_(group)">Lattice</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Binomial_coefficient"><strong>binomial coefficients</strong></a>.</p>
</li>
<li><p>Another great video source on Binomial Coefficients &amp; Lattice paths is <a target="_blank" href="https://sites.gatech.edu/math3012openresources/lecture-videos/lecture-3/">here</a>.</p>
</li>
<li><p>One of the best <a target="_blank" href="https://stackoverflow.com/questions/70868297/pathfinding-in-a-lattice-help-to-understand-example">stackoverflow answers</a> that explain the concept.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #14 - Longest Collatz sequence]]></title><description><![CDATA[Problem
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)   n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...]]></description><link>https://blog.theintrovertcoder.in/project-euler-14-longest-collatz-sequence</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-14-longest-collatz-sequence</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sun, 15 Jan 2023 00:30:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673429482426/d23636ca-5b04-4a14-aaee-9e8f5869bc61.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The following iterative sequence is defined for the set of positive integers:</p>
<p><code>n → n/2 (n is even)   n → 3n + 1 (n is odd)</code></p>
<p>Using the rule above and starting with <code>13</code>, we generate the following sequence:</p>
<p><code>13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1</code></p>
<p>It can be seen that this sequence (starting at <code>13</code> and finishing at <code>1</code>) contains <code>10</code> terms. Although it has not been proved yet (<strong>Collatz Problem</strong>), it is thought that all starting numbers finish at <code>1</code>.</p>
<p>Which starting number, under <code>one million</code>, produces the longest chain?</p>
<p><strong>NOTE:</strong> Once the chain starts the terms are allowed to go above one million.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>According to <a target="_blank" href="https://en.wikipedia.org/wiki/Collatz_conjecture#:~:text=The%20Collatz%20conjecture%20is%20one,every%20positive%20integer%20into%201.">Wikipedia</a>, the Collatz conjecture or Collatz problem is,</p>
<blockquote>
<p>The <strong>Collatz conjecture</strong> is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.</p>
</blockquote>
<p>Depending on whether <code>n</code> is odd or even, we perform either one of the operations until we reach <code>1</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673430661937/81f7077a-4140-482a-9027-1d3f0792b471.png" alt class="image--center mx-auto" /></p>
<p>Example sequence of <code>13</code> is <code>13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1</code> and it contains 10 terms</p>
<p>We need to find the number with the longest sequence under <code>1000000</code></p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>We can approach this in two steps.</p>
<p>First, for each <code>n</code>, we apply the above formula until we reach <code>1</code> and count the number of times we have applied the formula. This gives the length of the chain for <code>n</code>.</p>
<p>Then we compare it with the longest sequence so far until we reach the limit of one million. This way we can return the number with the longest sequence under one million.</p>
<p>Another smart way is to cache all the sequence lengths along the way. Look up the cache for the sequence length of <code>n</code> and if found, then return the length or else continue with the calculation.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> collatzSeqCount = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> seqCount = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) seqCount = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">while</span> (n &gt; <span class="hljs-number">1</span>) {
        <span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) {
            n = n / <span class="hljs-number">2</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// You can skip to next possible odd using (3n + 1)/2 while adding 2 to the counter </span>
            <span class="hljs-comment">// as (3n + 1) always results in even which again is divided by 2 in the next step.</span>
            <span class="hljs-comment">// Check HackerreankSolution.js for that approach.</span>
            n = (<span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>);
        }
        seqCount++;
    }
    <span class="hljs-keyword">return</span> seqCount;
}

<span class="hljs-keyword">const</span> calcLongestSeq = <span class="hljs-function"><span class="hljs-params">limit</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> longestSeq = <span class="hljs-number">1</span>,
        longestVal = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt;= limit; i++) {
        <span class="hljs-keyword">let</span> getSeqArrCount = collatzSeqCount(i);

        <span class="hljs-keyword">if</span> (getSeqArrCount &gt;= longestSeq) {
            longestSeq = getSeqArrCount;
            longestVal = i;
        }
    }
    <span class="hljs-keyword">return</span> longestVal;

}

<span class="hljs-built_in">console</span>.log(calcLongestSeq(<span class="hljs-number">1000000</span>));
</code></pre>
<p>Using the caching technique, the <code>collatzSeqCount</code> method can be rewritten as,</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> seqCountMap = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
<span class="hljs-keyword">const</span> collatzSeqCountAnoterMethod = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> seqCount = <span class="hljs-number">1</span>,
        initial = n;

    <span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) {
        seqCount = <span class="hljs-number">1</span>;
    }

    <span class="hljs-keyword">while</span> (n &gt; <span class="hljs-number">1</span>) {
        <span class="hljs-keyword">if</span> (seqCountMap.has(n)) {
            seqCount += seqCountMap.get(n);
            <span class="hljs-keyword">return</span> seqCount;
        }

        <span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) {
            n = n / <span class="hljs-number">2</span>;
        } <span class="hljs-keyword">else</span> {
            n = (<span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>);
        }
        seqCount++;
    }
    seqCountMap.set(initial, seqCount);
    <span class="hljs-keyword">return</span> seqCount;
}
</code></pre>
<p>Used Javascript <code>Map()</code> object for caching the calculated sequence length. This function can be further optimised.</p>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/14">14</a></p>
<p>For <a target="_blank" href="https://www.hackerrank.com/contests/projecteuler/challenges/euler014/problem"><strong>Hackerrank</strong></a>, modified the solution a bit as we know the upper bound of all the test cases to be 5 million. HR solution can be found in this <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/blob/main/14/HackerrankSolution.js">repo</a>. As said before, the code can be further optimised.</p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #13 - Large sum]]></title><description><![CDATA[Problem
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
9...]]></description><link>https://blog.theintrovertcoder.in/project-euler-13-large-sum</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-13-large-sum</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sat, 14 Jan 2023 03:27:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673424272777/3a5dec5c-a46f-4284-aa90-4137cd339bf8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.</p>
<pre><code class="lang-plaintext">37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690
</code></pre>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>We are given <code>100</code> <code>50-digit</code> numbers. We need to find the first <code>10 digits</code> of the sum of these <code>100</code> numbers.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>Since we have <code>50-digit</code> numbers, the sum of these numbers will be even bigger.</p>
<p>Languages with <code>BigInteger</code> support(Python or Javascript), can solve this problem in two lines of code.</p>
<p>In Javascript, we can approach this with or without using the <code>BigInt</code> data type.</p>
<p>Simply iterating through the given <code>100</code> numbers and adding to <code>sum</code> results in an even bigger number that is represented in a scientific format. Eg: <code>3.43608945e+38</code></p>
<p>We can then use <code>Number.prototype.toLocaleString()</code> function to convert the sum into a string and slice the first <code>10 digits</code> from it. Refer to <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString">MDN</a> for more on the parameters used in <code>.toLoacleString()</code>.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> findSum = <span class="hljs-function"><span class="hljs-params">input</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> values = input.split(<span class="hljs-string">'\n'</span>),
        sum = <span class="hljs-number">0</span>;

    values.forEach(<span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> {
        <span class="hljs-comment">// `+` is prefixed to convert to `Number` as it is passed as `String`</span>
        <span class="hljs-comment">// console.log(typeOf value); // string</span>
        sum += +(value);
    })
    <span class="hljs-comment">// to display full width digits for bigger numbers represented in scientific format eg 3.84568473654e+49</span>
    <span class="hljs-comment">// 'useGrouping' to say whether to use grouping separators, such as thousands separators</span>
    <span class="hljs-keyword">return</span> sum.toLocaleString(<span class="hljs-string">'fullwide'</span>, { <span class="hljs-attr">useGrouping</span>: <span class="hljs-literal">false</span> }).slice(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>);
}

<span class="hljs-keyword">const</span> input = <span class="hljs-string">`37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690`</span>;

<span class="hljs-built_in">console</span>.log(findSum(input));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/13">13</a></p>
<p>For <strong>Hackerrank</strong>, both the above approach and the <code>BigInt</code> approach works for all test cases.</p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #12 - Highly divisible triangular number]]></title><description><![CDATA[Problem
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of...]]></description><link>https://blog.theintrovertcoder.in/project-euler-12-highly-divisible-triangular-number</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-12-highly-divisible-triangular-number</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Fri, 13 Jan 2023 02:47:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672673532582/d00ccdba-e9a0-40f8-8316-4ff2f85102c0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The sequence of triangle numbers is generated by adding the natural numbers. So the <code>7th</code> triangle number would be <code>1 + 2 + 3 + 4 + 5 + 6 + 7 = 28</code>. The first ten terms would be:</p>
<p><code>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...</code></p>
<p>Let us list the factors of the first seven triangle numbers:</p>
<blockquote>
<p><strong>1</strong>: 1<br /> <strong>3</strong>: 1,3<br /> <strong>6</strong>: 1,2,3,6<br /><strong>10</strong>: 1,2,5,10<br /><strong>15</strong>: 1,3,5,15<br /><strong>21</strong>: 1,3,7,21<br /><strong>28</strong>: 1,2,4,7,14,28</p>
</blockquote>
<p>We can see that <code>28</code> is the first triangle number to have over <code>5 divisors</code>.</p>
<p>What is the value of the first triangle number to have over <code>five hundred divisors</code>?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>The <strong>triangular number sequence</strong> is the representation of the numbers in the form of an equilateral triangle arranged in a series or sequence.</p>
<p>The sum of the previous number and the order of the succeeding number results in the sequence of triangular numbers.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672673546346/52151da8-68f0-48b3-8733-063a8a0fcec0.png" alt class="image--center mx-auto" /></p>
<p>Given in the problem is an example of factors of the first <code>7</code> triangular sequence numbers. The triangular Number <code>28</code> which is the 7th has <code>6</code> factors/divisors. This is the first triangular number in the sequence that has more than <code>5</code> divisors.</p>
<p>We need to find the first triangular number that has over <code>500</code> divisors.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>This is an interesting problem involving triangular numbers.</p>
<p>More on triangular numbers on this wiki <a target="_blank" href="https://en.wikipedia.org/wiki/Triangular_number">page</a>.</p>
<p>If we are to find the <code>nth</code> triangular number, then we can use the formula,</p>
<p>$$T_n = \frac{n(n + 1)}{2}$$</p>
<p>But we don't need the <code>nth</code> number, instead, we need to find the number with over <code>500</code> divisors.</p>
<p>We can approach this in two steps.</p>
<ol>
<li><p>Find the triangular number <code>n</code></p>
</li>
<li><p>Find the number of divisors of <code>n</code> and if the count is greater than or equals <code>500</code>, then print that number <code>n</code>.</p>
</li>
</ol>
<p>For finding the triangular number of <code>n</code>, we can either use the formula above or can use a global variable to add all the previous triangular numbers till <code>n</code>.</p>
<p>For getting all the divisors of <code>n</code>, we can iterate through <code>i &lt;= sqrt(n)</code> instead of <code>i &lt;= n</code>. For every valid divisor <code>i</code>, there is another divisor <code>j</code>, as <code>j = n/i</code>. More on this algorithm at <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/ds-problem-js/find-proper-divisors-of-integer-n">finding the proper divisor of n</a></p>
<p>Count the divisors of <code>n</code> and when it is greater than or equals <code>500</code>, then that is our Answer.</p>
<p><strong>Note:</strong> <em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to my solution below.</em></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> properDivisors = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> divisorsArr = [<span class="hljs-number">1</span>];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i * i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (n % i === <span class="hljs-number">0</span>) {
            divisorsArr.push(i, n / i);
            <span class="hljs-keyword">if</span> (i === <span class="hljs-built_in">Math</span>.sqrt(n)) { 
                divisorsArr.pop();
            }
        }
    }
    <span class="hljs-keyword">return</span> divisorsArr.sort(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>{ <span class="hljs-keyword">return</span> a - b; });
}

<span class="hljs-keyword">const</span> findTriangularNum = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> (n * (n + <span class="hljs-number">1</span>)) / <span class="hljs-number">2</span>;

<span class="hljs-keyword">const</span> findDivTriNum = <span class="hljs-function"><span class="hljs-params">nDivs</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>,
        triNum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">let</span> divCount = <span class="hljs-number">0</span>;

        <span class="hljs-comment">// Either use formula or a simple addition of previous numbers.</span>
        <span class="hljs-comment">//triNum = findTriangularNum(i); </span>
        triNum += i;
        divCount = properDivisors(triNum).length;

        <span class="hljs-keyword">if</span> (divCount &gt;= nDivs) <span class="hljs-keyword">return</span> triNum;

        i += <span class="hljs-number">1</span>;
    }
}

<span class="hljs-built_in">console</span>.log(findDivTriNum(<span class="hljs-number">500</span>));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/12">12</a></p>
<p>For <a target="_blank" href="https://www.hackerrank.com/contests/projecteuler/challenges/euler012/problem">Hackerrank</a>, I have modified the above code slightly to pass all the test cases. The test cases involve finding the first triangle number to have over <code>N</code> divisors, where <code>1 &lt;= N &lt;= 1000</code>. So, instead of finding the triangle number starting from <code>1</code> for each test case, made use of the upper bound given. You can find <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/blob/main/12/HackerrankSolution.js">Hackerrank Solution</a> on GitHub.</p>
<p>If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Find Proper Divisors of integer 'n']]></title><description><![CDATA[Problem: Divisors of 'n'
Given a positive integer n. Find all the proper divisors of the integer n.
Approach
We can approach this in two ways.

Brute force method

Squareroot of n method


Brute force method:
The most basic approach is to apply the b...]]></description><link>https://blog.theintrovertcoder.in/find-proper-divisors-of-integer-n</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/find-proper-divisors-of-integer-n</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Problem Solving]]></category><category><![CDATA[data structures]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Thu, 12 Jan 2023 00:56:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673343265564/1e8f1a87-fb51-43cb-b760-9f151a3d4b13.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-problem-divisors-of-n">Problem: Divisors of 'n'</h2>
<p>Given a positive integer <code>n</code>. Find all the proper divisors of the integer <code>n</code>.</p>
<h2 id="heading-approach">Approach</h2>
<p>We can approach this in two ways.</p>
<ul>
<li><p>Brute force method</p>
</li>
<li><p>Squareroot of n method</p>
</li>
</ul>
<h3 id="heading-brute-force-method"><strong>Brute force method:</strong></h3>
<p>The most basic approach is to apply the brute force method. Brute force solves a problem through exhaustion. It goes through all possible choices until a solution is found.</p>
<p>To find the divisors for a positive integer <code>n</code>, we iterate all the integers from <code>1, 2, 3, . . . , n</code>, and divide <code>n</code> by each of those integers <code>i</code>, and those that divide evenly make up the set of divisors for <code>n</code>.</p>
<p>This is rather plain, easy and simple but is not quite an efficient solution.</p>
<p>The time complexity to find divisors of <code>n</code> is <code>O(n)</code>.</p>
<p>Consider that we need to find the divisors of the number <code>100000</code>. Then using the brute force method, we divide <code>100000</code> by <code>1 to 100000</code> taking <code>100000</code> iterations. This would take a long time to compute all the divisors of <code>100000</code>.</p>
<p>So to solve this problem, we can use a more efficient solution like that of <code>Squareroot of n</code> method</p>
<h3 id="heading-squareroot-of-n-method"><strong>Squareroot of n method:</strong></h3>
<p>Let us observe the divisors of <code>100</code> first as an example.</p>
<p>The proper divisors of <code>100</code> are <code>1, 2, 4, 5, 10, 20, 25, and 50</code> which can be written as <code>1, 2, 4, 5, 10, 100/5, 100/4, and 100/2</code>.</p>
<p>If <code>i</code> is a divisor of <code>n</code>, then <code>d = n/i</code> is also a divisor of <code>n</code> because <code>i * d = n</code>. Hence the divisors can be organised into pairs of <code>(i, d) = (i, n/i)</code>. The exception for this is if <code>n</code> is a perfect square and <code>i = sqrt(n)</code>, then in this case both <code>i</code> and <code>d</code> are equal.</p>
<p>Thus the divisors of a number <code>n</code> usually, appear in pairs and using this method, the time complexity is <code>O(sqrt(n))</code></p>
<p>For finding divisors of <code>100</code>, it involves <code>10</code> iterations and for <code>100000</code>, it takes roughly <code>316</code> iterations.</p>
<p>Example of finding divisors of <code>n = 100</code> (Code given below)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673398434971/1907c2e9-1d21-4f59-9df3-8dbdcda1ab5e.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-code">Code</h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> properDivisors = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> divisors = [<span class="hljs-number">1</span>];

    <span class="hljs-comment">// Condition can either be `i &lt;= sqrt(n)` OR `i * i &lt;= n`. Both are equivalent.</span>
    <span class="hljs-comment">// if square of `i` = `n`, then always square root of `n` = `i`</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i * i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (n % i === <span class="hljs-number">0</span>) {
            divisors.push(i, n / i);
            <span class="hljs-keyword">if</span> (i === <span class="hljs-built_in">Math</span>.sqrt(n)) { <span class="hljs-comment">// Condition can either be `i === sqrt(n)` OR `i * i === n`. Both are equivalent.</span>
                divisors.pop();
            }
        }
    }
    <span class="hljs-keyword">return</span> divisors.sort(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>{ <span class="hljs-keyword">return</span> a - b; });
}

<span class="hljs-built_in">console</span>.log(properDivisors(<span class="hljs-number">100</span>)); <span class="hljs-comment">//  1,2,4,5,10,20,25,50</span>
<span class="hljs-built_in">console</span>.log(properDivisors(<span class="hljs-number">144</span>)); <span class="hljs-comment">// 1,2,3,4,6,8,9,12,16,18,24,36,48,72</span>
</code></pre>
<p>You can find my solution here at <a target="_blank" href="https://github.com/sansk/datastructures-and-problem-solving-javascript/tree/main/01-Basic/ProperDivisors">ProperDivisors</a></p>
<p>I hope you learn something new and enjoy coding. If you have any feedback, please leave it below.</p>
<p>If you have any suggestions to improve my code or another way to approach this problem, leave it in the comment.</p>
<p>Thanks &amp; Happy Coding!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #11 - Largest product in a grid]]></title><description><![CDATA[Problem
In the 20×20 grid below, four numbers along a diagonal line have been marked in yellow.
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67 ...]]></description><link>https://blog.theintrovertcoder.in/project-euler-11-largest-product-in-a-grid</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-11-largest-product-in-a-grid</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Wed, 11 Jan 2023 03:56:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672651254941/f98d0dbf-82c0-471a-9a4d-9068c102a80f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>In the <code>20×20</code> grid below, four numbers along a diagonal line have been marked in yellow.</p>
<p>08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08<br />49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00<br />81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65<br />52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91<br />22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80<br />24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50<br />32 98 81 28 64 23 67 10 <strong><mark>26</mark></strong> 38 40 67 59 54 70 66 18 38 64 70<br />67 26 20 68 02 62 12 20 95 <strong><mark>63</mark></strong> 94 39 63 08 40 91 66 49 94 21<br />24 55 58 05 66 73 99 26 97 17 <strong><mark>78</mark></strong> 78 96 83 14 88 34 89 63 72<br />21 36 23 09 75 00 76 44 20 45 35 <strong><mark>14</mark></strong> 00 61 33 97 34 31 33 95<br />78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92<br />16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57<br />86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58<br />19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40<br />04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66<br />88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69<br />04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36<br />20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16<br />20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54<br />01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48</p>
<p>The product of these numbers is <code>26 × 63 × 78 × 14 = 1788696</code>.</p>
<p>What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the <code>20×20</code> grid?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>In this problem, given the numbers are in a <code>20 x 20</code> grid.</p>
<p>We need to find the greatest product of <code>4</code> adjacent numbers.</p>
<p>The <code>4</code> adjacent numbers should be in the same direction - UP or DOWN or LEFT or RIGHT or DIAGONAL, in the grid.</p>
<p>The example is highlighted in yellow in the above grid.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>Well, this one hit me hard. I had two sleepless nights before solving this with the help of Scrimba and some comments in hacker rank.</p>
<p>We convert the given grid into an Array of arrays. We have an array of 20 items and each item is again an array of 20 numbers.</p>
<p>Then, we loop through the array horizontally, vertically and diagonally.</p>
<p>I have linked the Scrimba video <a target="_blank" href="https://scrimba.com/learn/projecteuler/largest-product-in-a-grid-cg3reDA4">here</a>.</p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> grid = [
    [<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">22</span>, <span class="hljs-number">97</span>, <span class="hljs-number">38</span>, <span class="hljs-number">15</span>, <span class="hljs-number">0</span>, <span class="hljs-number">40</span>, <span class="hljs-number">0</span>, <span class="hljs-number">75</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">78</span>, <span class="hljs-number">52</span>, <span class="hljs-number">12</span>, <span class="hljs-number">50</span>, <span class="hljs-number">77</span>, <span class="hljs-number">91</span>, <span class="hljs-number">8</span>],
    [<span class="hljs-number">49</span>, <span class="hljs-number">49</span>, <span class="hljs-number">99</span>, <span class="hljs-number">40</span>, <span class="hljs-number">17</span>, <span class="hljs-number">81</span>, <span class="hljs-number">18</span>, <span class="hljs-number">57</span>, <span class="hljs-number">60</span>, <span class="hljs-number">87</span>, <span class="hljs-number">17</span>, <span class="hljs-number">40</span>, <span class="hljs-number">98</span>, <span class="hljs-number">43</span>, <span class="hljs-number">69</span>, <span class="hljs-number">48</span>, <span class="hljs-number">4</span>, <span class="hljs-number">56</span>, <span class="hljs-number">62</span>, <span class="hljs-number">0</span>],
    [<span class="hljs-number">81</span>, <span class="hljs-number">49</span>, <span class="hljs-number">31</span>, <span class="hljs-number">73</span>, <span class="hljs-number">55</span>, <span class="hljs-number">79</span>, <span class="hljs-number">14</span>, <span class="hljs-number">29</span>, <span class="hljs-number">93</span>, <span class="hljs-number">71</span>, <span class="hljs-number">40</span>, <span class="hljs-number">67</span>, <span class="hljs-number">53</span>, <span class="hljs-number">88</span>, <span class="hljs-number">30</span>, <span class="hljs-number">3</span>, <span class="hljs-number">49</span>, <span class="hljs-number">13</span>, <span class="hljs-number">36</span>, <span class="hljs-number">65</span>],
    [<span class="hljs-number">52</span>, <span class="hljs-number">70</span>, <span class="hljs-number">95</span>, <span class="hljs-number">23</span>, <span class="hljs-number">4</span>, <span class="hljs-number">60</span>, <span class="hljs-number">11</span>, <span class="hljs-number">42</span>, <span class="hljs-number">69</span>, <span class="hljs-number">24</span>, <span class="hljs-number">68</span>, <span class="hljs-number">56</span>, <span class="hljs-number">1</span>, <span class="hljs-number">32</span>, <span class="hljs-number">56</span>, <span class="hljs-number">71</span>, <span class="hljs-number">37</span>, <span class="hljs-number">2</span>, <span class="hljs-number">36</span>, <span class="hljs-number">91</span>],
    [<span class="hljs-number">22</span>, <span class="hljs-number">31</span>, <span class="hljs-number">16</span>, <span class="hljs-number">71</span>, <span class="hljs-number">51</span>, <span class="hljs-number">67</span>, <span class="hljs-number">63</span>, <span class="hljs-number">89</span>, <span class="hljs-number">41</span>, <span class="hljs-number">92</span>, <span class="hljs-number">36</span>, <span class="hljs-number">54</span>, <span class="hljs-number">22</span>, <span class="hljs-number">40</span>, <span class="hljs-number">40</span>, <span class="hljs-number">28</span>, <span class="hljs-number">66</span>, <span class="hljs-number">33</span>, <span class="hljs-number">13</span>, <span class="hljs-number">80</span>],
    [<span class="hljs-number">24</span>, <span class="hljs-number">47</span>, <span class="hljs-number">32</span>, <span class="hljs-number">60</span>, <span class="hljs-number">99</span>, <span class="hljs-number">3</span>, <span class="hljs-number">45</span>, <span class="hljs-number">2</span>, <span class="hljs-number">44</span>, <span class="hljs-number">75</span>, <span class="hljs-number">33</span>, <span class="hljs-number">53</span>, <span class="hljs-number">78</span>, <span class="hljs-number">36</span>, <span class="hljs-number">84</span>, <span class="hljs-number">20</span>, <span class="hljs-number">35</span>, <span class="hljs-number">17</span>, <span class="hljs-number">12</span>, <span class="hljs-number">50</span>],
    [<span class="hljs-number">32</span>, <span class="hljs-number">98</span>, <span class="hljs-number">81</span>, <span class="hljs-number">28</span>, <span class="hljs-number">64</span>, <span class="hljs-number">23</span>, <span class="hljs-number">67</span>, <span class="hljs-number">10</span>, <span class="hljs-number">26</span>, <span class="hljs-number">38</span>, <span class="hljs-number">40</span>, <span class="hljs-number">67</span>, <span class="hljs-number">59</span>, <span class="hljs-number">54</span>, <span class="hljs-number">70</span>, <span class="hljs-number">66</span>, <span class="hljs-number">18</span>, <span class="hljs-number">38</span>, <span class="hljs-number">64</span>, <span class="hljs-number">70</span>],
    [<span class="hljs-number">67</span>, <span class="hljs-number">26</span>, <span class="hljs-number">20</span>, <span class="hljs-number">68</span>, <span class="hljs-number">2</span>, <span class="hljs-number">62</span>, <span class="hljs-number">12</span>, <span class="hljs-number">20</span>, <span class="hljs-number">95</span>, <span class="hljs-number">63</span>, <span class="hljs-number">94</span>, <span class="hljs-number">39</span>, <span class="hljs-number">63</span>, <span class="hljs-number">8</span>, <span class="hljs-number">40</span>, <span class="hljs-number">91</span>, <span class="hljs-number">66</span>, <span class="hljs-number">49</span>, <span class="hljs-number">94</span>, <span class="hljs-number">21</span>],
    [<span class="hljs-number">24</span>, <span class="hljs-number">55</span>, <span class="hljs-number">58</span>, <span class="hljs-number">5</span>, <span class="hljs-number">66</span>, <span class="hljs-number">73</span>, <span class="hljs-number">99</span>, <span class="hljs-number">26</span>, <span class="hljs-number">97</span>, <span class="hljs-number">17</span>, <span class="hljs-number">78</span>, <span class="hljs-number">78</span>, <span class="hljs-number">96</span>, <span class="hljs-number">83</span>, <span class="hljs-number">14</span>, <span class="hljs-number">88</span>, <span class="hljs-number">34</span>, <span class="hljs-number">89</span>, <span class="hljs-number">63</span>, <span class="hljs-number">72</span>],
    [<span class="hljs-number">21</span>, <span class="hljs-number">36</span>, <span class="hljs-number">23</span>, <span class="hljs-number">9</span>, <span class="hljs-number">75</span>, <span class="hljs-number">0</span>, <span class="hljs-number">76</span>, <span class="hljs-number">44</span>, <span class="hljs-number">20</span>, <span class="hljs-number">45</span>, <span class="hljs-number">35</span>, <span class="hljs-number">14</span>, <span class="hljs-number">0</span>, <span class="hljs-number">61</span>, <span class="hljs-number">33</span>, <span class="hljs-number">97</span>, <span class="hljs-number">34</span>, <span class="hljs-number">31</span>, <span class="hljs-number">33</span>, <span class="hljs-number">95</span>],
    [<span class="hljs-number">78</span>, <span class="hljs-number">17</span>, <span class="hljs-number">53</span>, <span class="hljs-number">28</span>, <span class="hljs-number">22</span>, <span class="hljs-number">75</span>, <span class="hljs-number">31</span>, <span class="hljs-number">67</span>, <span class="hljs-number">15</span>, <span class="hljs-number">94</span>, <span class="hljs-number">3</span>, <span class="hljs-number">80</span>, <span class="hljs-number">4</span>, <span class="hljs-number">62</span>, <span class="hljs-number">16</span>, <span class="hljs-number">14</span>, <span class="hljs-number">9</span>, <span class="hljs-number">53</span>, <span class="hljs-number">56</span>, <span class="hljs-number">92</span>],
    [<span class="hljs-number">16</span>, <span class="hljs-number">39</span>, <span class="hljs-number">5</span>, <span class="hljs-number">42</span>, <span class="hljs-number">96</span>, <span class="hljs-number">35</span>, <span class="hljs-number">31</span>, <span class="hljs-number">47</span>, <span class="hljs-number">55</span>, <span class="hljs-number">58</span>, <span class="hljs-number">88</span>, <span class="hljs-number">24</span>, <span class="hljs-number">0</span>, <span class="hljs-number">17</span>, <span class="hljs-number">54</span>, <span class="hljs-number">24</span>, <span class="hljs-number">36</span>, <span class="hljs-number">29</span>, <span class="hljs-number">85</span>, <span class="hljs-number">57</span>],
    [<span class="hljs-number">86</span>, <span class="hljs-number">56</span>, <span class="hljs-number">0</span>, <span class="hljs-number">48</span>, <span class="hljs-number">35</span>, <span class="hljs-number">71</span>, <span class="hljs-number">89</span>, <span class="hljs-number">7</span>, <span class="hljs-number">5</span>, <span class="hljs-number">44</span>, <span class="hljs-number">44</span>, <span class="hljs-number">37</span>, <span class="hljs-number">44</span>, <span class="hljs-number">60</span>, <span class="hljs-number">21</span>, <span class="hljs-number">58</span>, <span class="hljs-number">51</span>, <span class="hljs-number">54</span>, <span class="hljs-number">17</span>, <span class="hljs-number">58</span>],
    [<span class="hljs-number">19</span>, <span class="hljs-number">80</span>, <span class="hljs-number">81</span>, <span class="hljs-number">68</span>, <span class="hljs-number">5</span>, <span class="hljs-number">94</span>, <span class="hljs-number">47</span>, <span class="hljs-number">69</span>, <span class="hljs-number">28</span>, <span class="hljs-number">73</span>, <span class="hljs-number">92</span>, <span class="hljs-number">13</span>, <span class="hljs-number">86</span>, <span class="hljs-number">52</span>, <span class="hljs-number">17</span>, <span class="hljs-number">77</span>, <span class="hljs-number">4</span>, <span class="hljs-number">89</span>, <span class="hljs-number">55</span>, <span class="hljs-number">40</span>],
    [<span class="hljs-number">4</span>, <span class="hljs-number">52</span>, <span class="hljs-number">8</span>, <span class="hljs-number">83</span>, <span class="hljs-number">97</span>, <span class="hljs-number">35</span>, <span class="hljs-number">99</span>, <span class="hljs-number">16</span>, <span class="hljs-number">7</span>, <span class="hljs-number">97</span>, <span class="hljs-number">57</span>, <span class="hljs-number">32</span>, <span class="hljs-number">16</span>, <span class="hljs-number">26</span>, <span class="hljs-number">26</span>, <span class="hljs-number">79</span>, <span class="hljs-number">33</span>, <span class="hljs-number">27</span>, <span class="hljs-number">98</span>, <span class="hljs-number">66</span>],
    [<span class="hljs-number">88</span>, <span class="hljs-number">36</span>, <span class="hljs-number">68</span>, <span class="hljs-number">87</span>, <span class="hljs-number">57</span>, <span class="hljs-number">62</span>, <span class="hljs-number">20</span>, <span class="hljs-number">72</span>, <span class="hljs-number">3</span>, <span class="hljs-number">46</span>, <span class="hljs-number">33</span>, <span class="hljs-number">67</span>, <span class="hljs-number">46</span>, <span class="hljs-number">55</span>, <span class="hljs-number">12</span>, <span class="hljs-number">32</span>, <span class="hljs-number">63</span>, <span class="hljs-number">93</span>, <span class="hljs-number">53</span>, <span class="hljs-number">69</span>],
    [<span class="hljs-number">4</span>, <span class="hljs-number">42</span>, <span class="hljs-number">16</span>, <span class="hljs-number">73</span>, <span class="hljs-number">38</span>, <span class="hljs-number">25</span>, <span class="hljs-number">39</span>, <span class="hljs-number">11</span>, <span class="hljs-number">24</span>, <span class="hljs-number">94</span>, <span class="hljs-number">72</span>, <span class="hljs-number">18</span>, <span class="hljs-number">8</span>, <span class="hljs-number">46</span>, <span class="hljs-number">29</span>, <span class="hljs-number">32</span>, <span class="hljs-number">40</span>, <span class="hljs-number">62</span>, <span class="hljs-number">76</span>, <span class="hljs-number">36</span>],
    [<span class="hljs-number">20</span>, <span class="hljs-number">69</span>, <span class="hljs-number">36</span>, <span class="hljs-number">41</span>, <span class="hljs-number">72</span>, <span class="hljs-number">30</span>, <span class="hljs-number">23</span>, <span class="hljs-number">88</span>, <span class="hljs-number">34</span>, <span class="hljs-number">62</span>, <span class="hljs-number">99</span>, <span class="hljs-number">69</span>, <span class="hljs-number">82</span>, <span class="hljs-number">67</span>, <span class="hljs-number">59</span>, <span class="hljs-number">85</span>, <span class="hljs-number">74</span>, <span class="hljs-number">4</span>, <span class="hljs-number">36</span>, <span class="hljs-number">16</span>],
    [<span class="hljs-number">20</span>, <span class="hljs-number">73</span>, <span class="hljs-number">35</span>, <span class="hljs-number">29</span>, <span class="hljs-number">78</span>, <span class="hljs-number">31</span>, <span class="hljs-number">90</span>, <span class="hljs-number">1</span>, <span class="hljs-number">74</span>, <span class="hljs-number">31</span>, <span class="hljs-number">49</span>, <span class="hljs-number">71</span>, <span class="hljs-number">48</span>, <span class="hljs-number">86</span>, <span class="hljs-number">81</span>, <span class="hljs-number">16</span>, <span class="hljs-number">23</span>, <span class="hljs-number">57</span>, <span class="hljs-number">5</span>, <span class="hljs-number">54</span>],
    [<span class="hljs-number">1</span>, <span class="hljs-number">70</span>, <span class="hljs-number">54</span>, <span class="hljs-number">71</span>, <span class="hljs-number">83</span>, <span class="hljs-number">51</span>, <span class="hljs-number">54</span>, <span class="hljs-number">69</span>, <span class="hljs-number">16</span>, <span class="hljs-number">92</span>, <span class="hljs-number">33</span>, <span class="hljs-number">48</span>, <span class="hljs-number">61</span>, <span class="hljs-number">43</span>, <span class="hljs-number">52</span>, <span class="hljs-number">1</span>, <span class="hljs-number">89</span>, <span class="hljs-number">19</span>, <span class="hljs-number">67</span>, <span class="hljs-number">48</span>]
];

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">largestGridProduct</span>(<span class="hljs-params">arr</span>) </span>{
    <span class="hljs-keyword">let</span> maxProduct = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">const</span> maxProductChecker = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
        <span class="hljs-keyword">if</span> (n &gt; maxProduct) {
            <span class="hljs-keyword">return</span> maxProduct = n;
        }
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> r = <span class="hljs-number">0</span>; r &lt; arr.length; r++) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> c = <span class="hljs-number">0</span>; c &lt; arr.length; c++) {
            <span class="hljs-keyword">let</span> currProduct = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">const</span> limit = arr.length - <span class="hljs-number">3</span>;

            <span class="hljs-comment">// Horizontal Check - Left/Right adjacent numbers </span>
            <span class="hljs-keyword">if</span> (c &lt; limit) {
                currProduct = arr[r][c] * arr[r][c + <span class="hljs-number">1</span>] * arr[r][c + <span class="hljs-number">2</span>] * arr[r][c + <span class="hljs-number">3</span>];
                maxProductChecker(currProduct);
            }

            <span class="hljs-comment">// Verticle Check - Up/Down adjacent check</span>
            <span class="hljs-keyword">if</span> (r &lt; limit) {
                currProduct = arr[r][c] * arr[r + <span class="hljs-number">1</span>][c] * arr[r + <span class="hljs-number">2</span>][c] * arr[r + <span class="hljs-number">3</span>][c];
                maxProductChecker(currProduct);
            }

            <span class="hljs-comment">// Right Diagonal check</span>
            <span class="hljs-keyword">if</span> (c &lt; limit &amp;&amp; r &lt; limit) {
                currProduct = arr[r][c] * arr[r + <span class="hljs-number">1</span>][c + <span class="hljs-number">1</span>] * arr[r + <span class="hljs-number">2</span>][c + <span class="hljs-number">2</span>] * arr[r + <span class="hljs-number">3</span>][c + <span class="hljs-number">3</span>];
                maxProductChecker(currProduct);
            }

            <span class="hljs-comment">// Left Diagonal Check</span>
            <span class="hljs-keyword">if</span> (c &gt;= <span class="hljs-number">3</span> &amp;&amp; r &lt; limit) {
                currProduct = arr[r][c] * arr[r + <span class="hljs-number">1</span>][c - <span class="hljs-number">1</span>] * arr[r + <span class="hljs-number">2</span>][c - <span class="hljs-number">2</span>] * arr[r + <span class="hljs-number">3</span>][c - <span class="hljs-number">3</span>];
                maxProductChecker(currProduct);
            }
        }
    }

    <span class="hljs-built_in">console</span>.log(maxProduct);
    <span class="hljs-keyword">return</span> maxProduct;
}

largestGridProduct(grid);
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/11">11</a>.</p>
<p>This particular solution was executed in Hacker rank without failing a single test case.</p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #10 - Summation of primes]]></title><description><![CDATA[Problem
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

Problem Description
The problem is pretty self-explanatory!
Sum of primes below 10 is 17.
Find the sum of primes below 2000000.

Approach...]]></description><link>https://blog.theintrovertcoder.in/project-euler-10-summation-of-primes</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-10-summation-of-primes</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Tue, 10 Jan 2023 03:44:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672649253893/8ac4e53e-728e-4723-958a-58a336afb52a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The sum of the primes below <code>10</code> is <code>2 + 3 + 5 + 7 = 17</code>.</p>
<p>Find the sum of all the primes below <code>two million</code>.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>The problem is pretty self-explanatory!</p>
<p>Sum of primes below <code>10</code> is <code>17</code>.</p>
<p>Find the sum of primes below <code>2000000</code>.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>To calculate the sum of primes, we can use the brute force algorithm method, which <strong>solves a problem through exhaustion -</strong> it goes through all possible choices until a solution is found.</p>
<p>The brute force method is simple and consistent but very slow. It affects the time complexity of the program as it depends on the size of the input, in our case we need to loop two million times.</p>
<p>We loop through the numbers till <code>2 million</code> and push them to an array <code>allPrimes</code>, if it is a prime number.</p>
<p>The steps involved in determining <code>isPrime(n)</code> is the same as we did with problem #3 - Largest Prime factor and problem #7 - 10001st Prime. Except that here, I opted for a <code>while</code> loop instead of <code>for</code> loop.</p>
<p>Finally, using the array <code>reduce()</code> method, the sum of all the primes in the array <code>allPrimes</code> is calculated.</p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> isPrime = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">if</span> (n === <span class="hljs-number">2</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    <span class="hljs-keyword">if</span> (n === <span class="hljs-number">3</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    <span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    <span class="hljs-keyword">if</span> (n % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

    <span class="hljs-keyword">let</span> i = <span class="hljs-number">5</span>,
        next = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">while</span> (i * i &lt;= n) {
        <span class="hljs-keyword">if</span> (n % i === <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        }
        i += next;
        next = <span class="hljs-number">6</span> - next;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}

<span class="hljs-keyword">let</span> allPrimes = [];

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt;= <span class="hljs-number">2000000</span>; i++) {
    <span class="hljs-keyword">if</span> (isPrime(i)) allPrimes.push(i);
}

<span class="hljs-built_in">console</span>.log(allPrimes.reduce(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b, <span class="hljs-number">0</span>));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/10">10</a>.</p>
<p>The above solution failed 3 out of 10 test cases in Hacker rank(Of course Hacker rank uses a variety of test cases from a small number to a very big number).</p>
<p>Hence I tweaked the solution to achieve the time complexity for passing all the test cases.</p>
<p>First, I adjusted a bit while calculating <code>isPrime(n)</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> isPrime = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
            <span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

            <span class="hljs-keyword">if</span> (n &lt; <span class="hljs-number">4</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; <span class="hljs-comment">// 2 &amp; 3 are prime</span>
            <span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

            <span class="hljs-comment">// We have excluded 4, 6 &amp; 8 in the previous step.</span>
            <span class="hljs-keyword">if</span> (n &lt; <span class="hljs-number">9</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; 
            <span class="hljs-keyword">if</span> (n % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

            <span class="hljs-keyword">let</span> i = <span class="hljs-number">5</span>;

            <span class="hljs-comment">// n rounded to the greatest integer s so that s*s &lt;= n</span>
            <span class="hljs-keyword">let</span> s = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.sqrt(n)); 
            <span class="hljs-keyword">while</span> (i &lt;= s) {
                <span class="hljs-keyword">if</span> (n % i === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
                <span class="hljs-keyword">if</span> (n % (i + <span class="hljs-number">2</span>) === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
                i = i + <span class="hljs-number">6</span>;
            }

            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
        }
</code></pre>
<p><strong>Note: The below code is only for the Hacker Rank solution to pass all test cases. 👇</strong></p>
<p>Even after this, 2 test cases were timed out. So instead of getting the <code>allPrimes</code> for every test case, declare the <code>allPrimes</code> array as global to all test cases, push the prime numbers into this array from where we left in the last test case till we reach the current <code>n</code>. In this way, we won't repeat the same process again and again for each test case.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> t = <span class="hljs-built_in">parseInt</span>(readLine());

    <span class="hljs-keyword">let</span> allPrimes = []; <span class="hljs-comment">// Moved this here. Common to all the input.</span>

    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> a0 = <span class="hljs-number">0</span>; a0 &lt; t; a0++){
        <span class="hljs-keyword">var</span> n = <span class="hljs-built_in">parseInt</span>(readLine());

        <span class="hljs-comment">// let allPrimes = [];</span>
        <span class="hljs-keyword">let</span> sum = <span class="hljs-number">0</span>, 
            temp = <span class="hljs-literal">true</span>;
        <span class="hljs-comment">// If allPrimes has items, get the last item to lastNum</span>
        <span class="hljs-comment">// Else initialise lastNum to 1.</span>
        <span class="hljs-comment">// We will loop through from lastNum till we reach n</span>
        <span class="hljs-comment">// and add the prime numbers into the array allPrimes</span>
        <span class="hljs-keyword">let</span> lastNum = allPrimes.length !== <span class="hljs-number">0</span> ? 
                            allPrimes[allPrimes.length<span class="hljs-number">-1</span>] : <span class="hljs-number">1</span>;
        <span class="hljs-comment">// If lastNum is greater than n, then we already have </span>
        <span class="hljs-comment">// the prime numbers till n in the allPrime array.</span>
        <span class="hljs-comment">// So loop through allPrime array till &lt;= n, and </span>
        <span class="hljs-comment">// sum all the prime numbers</span>
        <span class="hljs-keyword">if</span>(lastNum &gt; n){
            temp = <span class="hljs-literal">false</span>;
            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> l = <span class="hljs-number">0</span> ; l &lt; allPrimes.length ; l++ ){
                <span class="hljs-keyword">if</span>(allPrimes[l] &lt;= n){
                    sum += allPrimes[l];  
                }
            }
        }
        <span class="hljs-comment">// Else, from lastNum + 1, we need to find the prime till n</span>
        lastNum++
        <span class="hljs-keyword">while</span>(lastNum &lt;= n){
            <span class="hljs-keyword">if</span>(isPrime(lastNum)){
                allPrimes.push(lastNum)
            }
            lastNum++
        }
        <span class="hljs-keyword">if</span>(temp){
            sum = allPrimes.reduce(<span class="hljs-function">(<span class="hljs-params">acc,cur</span>) =&gt;</span> acc + cur, <span class="hljs-number">0</span>)
        }
        <span class="hljs-built_in">console</span>.log(sum)
    }

}
</code></pre>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #9 - Special Pythagorean triplet]]></title><description><![CDATA[Problem
A Pythagorean triplet is a set of three natural numbers, a < b < c for which,
$$a^2 + b^2 = c^2$$
For example,
$$3^2 + 4^2 = 9 + 16 = 25 = 5^2$$
There exists exactly one Pythagorean triplet for which a + b + c = 1000.Find the product abc.

Pr...]]></description><link>https://blog.theintrovertcoder.in/project-euler-9-special-pythagorean-triplet</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-9-special-pythagorean-triplet</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Mon, 09 Jan 2023 04:55:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672590928270/3be48535-df2f-4be9-8da2-2e4ca9e4bd54.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>A Pythagorean triplet is a set of three natural numbers, <code>a &lt; b &lt; c</code> for which,</p>
<p>$$a^2 + b^2 = c^2$$</p>
<p>For example,</p>
<p>$$3^2 + 4^2 = 9 + 16 = 25 = 5^2$$</p>
<p>There exists exactly one Pythagorean triplet for which <code>a + b + c = 1000</code>.<br />Find the product <code>abc</code>.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>In a Pythagorean triplet of natural numbers, <code>a &lt; b &lt; c</code> for which,</p>
<p>$$a^2 + b^2 = c^2$$</p>
<p>Example,</p>
<p>$$3^2 + 4^2 = 9 + 16 = 25 = 5^2$$</p>
<p>In this case <code>a + b + c</code> is <code>12</code> and <code>a * b * c</code> is <code>60</code></p>
<p>Same way, there exists one triplet, where <code>a + b + c = 1000</code> and we need to find <code>a * b * c</code> of that triplet.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>Let's approach this problem as finding some triplets where <code>a + b + c = n</code>.</p>
<p>There are as many different mathematical approaches to solving this(I have known only 2. But <a target="_blank" href="https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples">Wikipedia</a> has more than that 😎).</p>
<p>The straightforward approach is to loop over a and b to check,</p>
<p>$$a^2 + b^2 = (n − a − b)^2$$</p>
<p>And since we have <code>a &lt; b &lt; c</code>, we use the below formula while looping through <code>a</code> and <code>b</code> - <code>a &lt;= (n - 3)/3 &amp; b &lt; (n - a)/2</code></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> pythagoreanTriplet = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> maxProduct = <span class="hljs-number">-1</span>;

    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> a = <span class="hljs-number">1</span>; a &lt;= (n - <span class="hljs-number">3</span>)/<span class="hljs-number">3</span>; a++) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> b = a; b &lt;= (n - a)/<span class="hljs-number">2</span> ; b++) {
            <span class="hljs-keyword">if</span>( (a*a) + (b*b) === (n-a-b)*(n-a-b)) {
                maxProduct = a*b*(n-a-b);
            }
        }
    }
    <span class="hljs-keyword">return</span> maxProduct;
}
<span class="hljs-built_in">console</span>.log(pythagoreanTriplet(<span class="hljs-number">1000</span>));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/09">09</a>. My GitHub solution is different from the above. To achieve 100% success(all test cases passed) in Hacker Rank, I tweaked a bit using the formula.</p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #8 - Largest product in a series]]></title><description><![CDATA[Problem
The four adjacent digits in the 1000-digit number that has the greatest product is 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195...]]></description><link>https://blog.theintrovertcoder.in/project-euler-8-largest-product-in-a-series</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-8-largest-product-in-a-series</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sun, 08 Jan 2023 01:33:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672590810067/9dd45f31-30f9-4a72-a4b2-45034a938f77.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The four adjacent digits in the <code>1000</code>-digit number that has the greatest product is <code>9 × 9 × 8 × 9 = 5832</code>.</p>
<p>73167176531330624919225119674426574742355349194934<br />96983520312774506326239578318016984801869478851843<br />85861560789112949495459501737958331952853208805511<br />12540698747158523863050715693290963295227443043557<br />66896648950445244523161731856403098711121722383113<br />62229893423380308135336276614282806444486645238749<br />30358907296290491560440772390713810515859307960866<br />70172427121883998797908792274921901699720888093776<br />65727333001053367881220235421809751254540594752243<br />52584907711670556013604839586446706324415722155397<br />53697817977846174064955149290862569321978468622482<br />83972241375657056057490261407972968652414535100474<br />82166370484403199890008895243450658541227588666881<br />16427171479924442928230863465674813919123162824586<br />17866458359124566529476545682848912883142607690042<br />24219022671055626321111109370544217506941658960408<br />07198403850962455444362981230987879927244284909188<br />84580156166097919133875499200524063689912560717606<br />05886116467109405077541002256983155200055935729725<br />71636269561882670428252483600823257530420752963450</p>
<p>Find the <code>thirteen adjacent digits</code> in the <code>1000</code>-digit number that has the greatest product. What is the value of this product?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>Well, a <code>1000</code>-digit number is given to us.</p>
<p>The <code>4</code> adjacent digits with the greatest product are <code>9 × 9 × 8 × 9 = 5832</code>.</p>
<p>We need to find the product of a <code>13</code> adjacent digits in this number and to top it off, it is the greatest product of any <code>13</code> adjacent digits in this <code>1000</code>-digit number.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>To solve this problem, first, we are going to convert this <code>1000</code>-digit number to an array of length <code>1000</code>. We use <code>String.prototype.split()</code> function to convert the number to an array.</p>
<p>Then, we loop through the array till we reach the last <code>13</code> digits.</p>
<p>Slice the array for every 13 digits using the Array <code>slice()</code> method and then calculate the product using the Array <code>reduce()</code> method.</p>
<p>Push this product to a new array.</p>
<p>Once all the digits are handled, then using <code>Math.max()</code>, return the greatest of the calculated product.</p>
<p>Slicing the array and calculating its product is a much better solution compared to looping through every digit and calculating its product in terms of time complexity.</p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> givenNumber = <span class="hljs-string">'7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'</span>;

<span class="hljs-keyword">const</span> gvnNumArr = givenNumber.trim().split(<span class="hljs-string">''</span>).map(<span class="hljs-built_in">Number</span>);

<span class="hljs-keyword">const</span> largestProduct = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> prod = <span class="hljs-number">0</span>,
        prodArray = [];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt;= gvnNumArr.length - n; i++) {
        <span class="hljs-keyword">const</span> last = i + n;

        prod = gvnNumArr.slice(i, last).reduce(<span class="hljs-function">(<span class="hljs-params">acc, curr</span>) =&gt;</span> acc * curr, <span class="hljs-number">1</span>);
        prodArray.push(prod);
    }

    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.max(...prodArray);
}

<span class="hljs-built_in">console</span>.log(largestProduct(<span class="hljs-number">13</span>));
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/08">08</a></p>
<p>This particular solution was executed with a better complexity in Hacker rank. If you have another solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #7 - 10001st prime]]></title><description><![CDATA[Problem
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?

Problem Description
We all know about Prime Numbers. A number that is divisible by 1 and itself is called ...]]></description><link>https://blog.theintrovertcoder.in/project-euler-7-10001st-prime</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-7-10001st-prime</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[project-euler-solution]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sat, 07 Jan 2023 06:16:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672474604831/6c0a3b2f-9fd9-40df-ad90-f2758b6c74e1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>By listing the first six prime numbers: <code>2, 3, 5, 7, 11, and 13</code>, we can see that the <code>6</code>th prime is <code>13</code>.</p>
<p>What is the <code>10001</code>st prime number?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>We all know about Prime Numbers. A number that is divisible by <code>1</code> and <code>itself</code> is called a <strong>Prime Number</strong>. In other words, it has only 2 factors - <code>1</code> and <code>itself</code>.</p>
<p>So we need to find the <code>nth</code> prime number. For example, <code>3rd</code> prime number is <code>5</code>, <code>4th</code> prime is <code>7</code>, <code>6th</code> prime is <code>13</code>, <code>10th</code> prime is <code>29</code> and so on.</p>
<p>We need to find the <code>10001</code>st prime number.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>To find <code>nth</code> prime number, we need to loop through a range of numbers from <code>2</code> to <code>Number.MAX_SAFE_INTEGER</code>.</p>
<p>Increase the counter by <code>1</code> for each prime number and once we find the <code>nth</code> prime, we break out of the loop.</p>
<p>To Check for the prime number, we use the same technique as in <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler">Problem #3 Largest prime factor</a></p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> isPrime = <span class="hljs-function"><span class="hljs-params">num</span> =&gt;</span> {
    <span class="hljs-keyword">if</span> (num === <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    <span class="hljs-keyword">if</span> (num === <span class="hljs-number">2</span> || num === <span class="hljs-number">3</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    <span class="hljs-keyword">if</span> (num %<span class="hljs-number">2</span> === <span class="hljs-number">0</span> || num % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">5</span>; i &lt;= <span class="hljs-built_in">Math</span>.ceil(num / <span class="hljs-number">2</span>); i += <span class="hljs-number">6</span>) {
        <span class="hljs-keyword">if</span> (num % i === <span class="hljs-number">0</span> || num % (i+<span class="hljs-number">2</span>) === <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}

<span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt; <span class="hljs-built_in">Number</span>.MAX_SAFE_INTEGER; i++) {
    <span class="hljs-keyword">if</span> (isPrime(i)) count++;

    <span class="hljs-keyword">if</span> (count === n) {
        <span class="hljs-built_in">console</span>.log(i);
        <span class="hljs-keyword">break</span>;
    }
}

<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"10th"</span>);
<span class="hljs-built_in">console</span>.log(nthPrime(<span class="hljs-number">10</span>)); <span class="hljs-comment">// 29</span>
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"10th"</span>); <span class="hljs-comment">// 10th: 0.221923828125 ms</span>

<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"100th"</span>);
<span class="hljs-built_in">console</span>.log(nthPrime(<span class="hljs-number">100</span>)); <span class="hljs-comment">// 541</span>
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"100th"</span>); <span class="hljs-comment">// 100th: 0.68701171875 ms</span>

<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"10001th"</span>);
<span class="hljs-built_in">console</span>.log(nthPrime(<span class="hljs-number">10001</span>)); <span class="hljs-comment">// 104743</span>
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"10001th"</span>); <span class="hljs-comment">// 10001th: 827.61181640625 ms</span>
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/07"><strong>07</strong></a></p>
<p>This particular solution was <code>timed out</code> in Hacker rank for 2 out of 7 test cases. So a little bit of tweaking is necessary to achieve a better time complexity.</p>
<p>If you have a better solution, please leave it in the comments below.</p>
<p>But no worries, the above code works perfectly fine for our use case, <code>10001th</code> prime number.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #6 - Sum square difference]]></title><description><![CDATA[Problem
The sum of the squares of the first ten natural numbers is,
$$1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 = 385$$
The square of the sum of the first ten natural numbers is,
$$(1+2+3+4+5+6+7+8+9+10)^2 = 55^2 = 3025$$
Hence the d...]]></description><link>https://blog.theintrovertcoder.in/project-euler-6-sum-square-difference</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-6-sum-square-difference</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[project-euler-solution]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Fri, 06 Jan 2023 03:44:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672473700895/99f6ffd3-c268-44fc-92cc-678b403b10f3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The sum of the squares of the first ten natural numbers is,</p>
<p>$$1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 = 385$$</p>
<p>The square of the sum of the first ten natural numbers is,</p>
<p>$$(1+2+3+4+5+6+7+8+9+10)^2 = 55^2 = 3025$$</p>
<p>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is</p>
<p>$$3025 − 385 = 2640$$</p>
<p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>Well, the problem itself is self-explanatory.</p>
<p>The example given is for first <code>10</code> natural numbers. We need to find the difference between the Sum of Squares &amp; Squares of the Sum of first <code>100</code> natural numbers.</p>
<p>i.e. <code>1 to 100</code>.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>This is a detailed approach.</p>
<p>Two functions are involved. One function is to calculate the square of each number from <code>1</code> to <code>100</code> and add those squares.</p>
<p>Another function is to add the numbers from <code>1</code> to <code>100</code> and square their sum.</p>
<p>Finally, find the difference between both.</p>
<p>Very simple, Right?</p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> sumOfSqrNums = <span class="hljs-function">(<span class="hljs-params">start, end</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> sumOfSqrs = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">if</span> (start &gt; end) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${start}</span> &gt; <span class="hljs-subst">${end}</span>! Enter a valid Start &amp; End.`</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt;= end; i++) {
            sumOfSqrs += <span class="hljs-built_in">Math</span>.pow(i, <span class="hljs-number">2</span>);
        }
    }
    <span class="hljs-keyword">return</span> sumOfSqrs;
}

<span class="hljs-keyword">const</span> sqrOfSumNums = <span class="hljs-function">(<span class="hljs-params">start, end</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> sumOfNums = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">if</span> (start &gt; end) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${start}</span> &gt; <span class="hljs-subst">${end}</span>! Enter a valid Start &amp; End.`</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt;= end; i++) {
            sumOfNums += i;
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.pow(sumOfNums, <span class="hljs-number">2</span>);
}

<span class="hljs-keyword">let</span> difference = sqrOfSumNums(<span class="hljs-number">1</span>, <span class="hljs-number">100</span>) - sumOfSqrNums(<span class="hljs-number">1</span>, <span class="hljs-number">100</span>);
<span class="hljs-built_in">console</span>.log(difference);
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/06"><strong>06</strong></a></p>
<p>This particular solution was executed with a better complexity in Hacker rank. But there are still much better solutions to this. If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #5 - Smallest multiple]]></title><description><![CDATA[Problem
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Problem Description
The Problem is s...]]></description><link>https://blog.theintrovertcoder.in/project-euler-5-smallest-multiple</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-5-smallest-multiple</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[project-euler-solution]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Thu, 05 Jan 2023 00:05:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672407126794/4f7d1fd3-31f3-44e5-b9ea-00968471a1b7.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem">Problem</h3>
<p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p>
<p>What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</p>
<hr />
<h3 id="heading-problem-description">Problem Description</h3>
<p>The Problem is straightforward. The smallest number that is divisible by all the numbers from <code>1</code> to <code>10</code> is <code>2520</code>.</p>
<p>We need to find the smallest positive number that is divisible by all the numbers from <code>1</code> to <code>20</code></p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>To calculate, let's break down all the prime numbers between <code>2</code> and <code>20</code>.</p>
<p>So we will have an array like <code>[2, 3, 5, 7, 11, 13, 17, 19]</code>.</p>
<p>Then for each of these prime components, we need to find the maximum number of occurrences in each number. In other words, factorize each number from 2 to 20 and find the number of times a particular prime number occurs.</p>
<p>For example, <code>12</code> has <code>2</code> occurrence of prime number 2 and <code>1</code> occurrence of prime number 3. Below is a chart with the maximum occurrence for each number.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672415507609/7ff88537-1091-4e9f-9246-476d117fe37d.png" alt class="image--center mx-auto" /></p>
<p>Then you raise each prime number to its maximum occurrence: <code>2^4, 3^2, 5^1, …</code> and multiply the result: <code>16 * 9 * 5 * ...</code></p>
<p>In this calculation, we involve only prime numbers between 1 and 20 because the other numbers are eventually multiples of these prime numbers.</p>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> findPrimeNumbers = <span class="hljs-function">(<span class="hljs-params">start, end</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> primeArray = [];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt;= end; i++) {
        <span class="hljs-keyword">let</span> isPrime = <span class="hljs-literal">true</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">2</span>; j &lt;= <span class="hljs-built_in">Math</span>.ceil(i / <span class="hljs-number">2</span>); j++) {
            <span class="hljs-keyword">if</span> ((i % j) === <span class="hljs-number">0</span>) {
                isPrime = <span class="hljs-literal">false</span>;
                <span class="hljs-keyword">break</span>;
            }
        }
        <span class="hljs-keyword">if</span> (isPrime === <span class="hljs-literal">true</span>) {
            primeArray.push(i);
        }
    }
    <span class="hljs-keyword">return</span> primeArray;
};

<span class="hljs-keyword">const</span> findFactors = <span class="hljs-function">(<span class="hljs-params">number</span>) =&gt;</span> {
    <span class="hljs-keyword">var</span> factorsArray = [];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt;= number; i++) {
        <span class="hljs-keyword">while</span> ((number % i) === <span class="hljs-number">0</span>) {
            factorsArray.push(i);
            number /= i;
        }
    }
    <span class="hljs-keyword">return</span> factorsArray;
}

<span class="hljs-keyword">const</span> smallestDivNumber = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> primeArr = findPrimeNumbers(<span class="hljs-number">2</span>, <span class="hljs-number">20</span>);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; primeArr.length; i++) {
        <span class="hljs-keyword">let</span> maxOccur = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">2</span>; j &lt;= <span class="hljs-number">20</span>; j++) {
            <span class="hljs-keyword">if</span> (!(primeArr[i] &gt; j)) {
                <span class="hljs-keyword">const</span> factArr = findFactors(j);
                <span class="hljs-keyword">let</span> countOccur = factArr
                                    .filter(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x === primeArr[i]).length;
                <span class="hljs-keyword">if</span> (countOccur &gt; maxOccurance) maxOccur = countOccur;
            }
        }
        <span class="hljs-keyword">if</span> (maxOccur &gt; <span class="hljs-number">0</span>) primeArr[i] = <span class="hljs-built_in">Math</span>.pow(primeArr[i], maxOccur);
    }

    <span class="hljs-keyword">return</span> primeArr.reduce(<span class="hljs-function">(<span class="hljs-params">acc, val</span>) =&gt;</span> acc * val, <span class="hljs-number">1</span>);
}

<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"Time"</span>);
<span class="hljs-built_in">console</span>.log(smallestDivNumber());
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"Time"</span>);
</code></pre>
<p>You can find my solution on GitHub <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/05">05</a></p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #4 - Largest palindrome product]]></title><description><![CDATA[Problem
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

Problem Description
A palindromic...]]></description><link>https://blog.theintrovertcoder.in/project-euler-4-largest-palindrome-product</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-4-largest-palindrome-product</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[project-euler-solution]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Wed, 04 Jan 2023 04:25:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672406686561/4f9b7ef5-f9c3-4686-9122-0261bab36f93.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.</p>
<p>Find the largest palindrome made from the product of two 3-digit numbers.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>A palindromic number reads the same both forward and also backward.</p>
<p>One such palindromic number is <code>9009</code> which is the largest palindrome made by multiplying two 2-digit numbers, <code>91 * 99</code></p>
<p>We need to find the largest Palindrome that can be made by multiplying two 3-digit numbers</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>The smallest 3-digit number is <code>100</code> and the largest is <code>999</code>. Since we need the largest palindrome, we approach this from the largest to the smallest, <code>999</code> -&gt; <code>100</code>.</p>
<p>Two reversed <code>for</code> loops starting from <code>999</code> for each number, eg: <code>99 * 99</code>, <code>99 * 98</code>, <code>99 * 97</code> etc</p>
<p>To check if the product is Palindrome,</p>
<ul>
<li><p>convert the number to <code>string</code>,</p>
</li>
<li><p>then <code>split</code> it into an array,</p>
</li>
<li><p><code>reverse</code> the array &amp;</p>
</li>
<li><p><code>join</code> the items to get the reversed number in <code>string</code> format.</p>
</li>
<li><p>Now convert the reversed number to a <code>number</code> format and compare with the original number.</p>
</li>
<li><p>If both are equal, then it is a palindrome.</p>
</li>
</ul>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> checkPalindrome = <span class="hljs-function">(<span class="hljs-params">number</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> reverse = <span class="hljs-built_in">String</span>(number)
        .split(<span class="hljs-string">''</span>)
        .reverse()
        .join(<span class="hljs-string">''</span>);
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">Number</span>(reverse) === number;
}

<span class="hljs-keyword">const</span> retLargestPalindrome = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">let</span> maxVal = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">999</span>; i &gt;= <span class="hljs-number">100</span>; i--) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> j = <span class="hljs-number">999</span>; j &gt;= <span class="hljs-number">100</span>; j--) {
            <span class="hljs-keyword">let</span> prod = i * j;
            <span class="hljs-keyword">if</span> (checkPalindrome(prod)) {
                <span class="hljs-keyword">if</span> (prod &gt; maxVal) maxVal = prod;
                <span class="hljs-comment">// Can also use maxVal = Math.max(maxVal, prod);</span>
                <span class="hljs-keyword">break</span>;
            }
        }
    }
    <span class="hljs-keyword">return</span> maxVal;
}

<span class="hljs-built_in">console</span>.log(retLargestPalindrome());
</code></pre>
<p>My Solution is found in <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/04">04</a></p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #3 - Largest prime factor]]></title><description><![CDATA[Problem
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?

Problem Description
If any given number, N is divisible by x without leaving a reminder, then x is a Factor of the Number N.
Similar...]]></description><link>https://blog.theintrovertcoder.in/project-euler-3-largest-prime-factor</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-3-largest-prime-factor</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Tue, 03 Jan 2023 02:27:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672216382924/cedb1fb1-286a-4fa5-8eb1-cda390c24c46.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143?</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>If any given number, <strong>N</strong> is divisible by <strong>x</strong> without leaving a reminder, then <code>x</code> <strong>is a Factor of the Number</strong> <code>N</code>.</p>
<p>Similarly, a <strong>Prime Number</strong> is a number that is divisible by <code>1</code> and <code>itself</code>. In other words, it has only 2 factors - <code>1</code> and <code>itself</code>.</p>
<p>Well, <strong>10</strong> is <strong>not a prime</strong> number as it has other factors like <strong>2 and 5</strong>. And <code>2</code> is the <strong>only even prime number</strong> we have and all the other prime numbers are odd. <code>1</code> is neither a prime nor composite number.</p>
<p>So, in our problem, we need to find the Largest Factor of a given number which is also a prime.</p>
<p>Given <code>13195</code>, the prime factors are <code>5, 7, 13 and 29</code>. Here <strong>29 is our largest prime factor.</strong></p>
<p>We need to find the Largest prime factor for <code>600851475143</code>.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>The approach here is to factorize the given number with a divisor starting from 2 and update the <code>max</code> with the divisor.</p>
<p>Here is how it is approached,</p>
<ul>
<li><p>Step: 1 - Initialise <code>max</code> to the lowest number possible. <code>max = 0</code></p>
</li>
<li><p>Step-2 - While the number <code>n</code> is divisible by 2, then assign <code>max = 2</code> and divide <code>n</code> by <code>2</code>. Continue this <code>while</code> loop till the number <code>n</code> is divisible by <code>2</code>. In this step, we remove all possible even factors.</p>
</li>
<li><p>Step-3 - Once the while loop(in step-2) finishes, <code>n</code> will be odd. Now while <code>n</code> is divisible by 3, then assign <code>max = 3</code> and divide <code>n</code> by <code>3</code>. Continue this <code>while</code> loop till the number <code>n</code> is divisible by <code>3</code>. In this step, we remove all possible <code>3</code> factors</p>
</li>
<li><p>Step-4 - We loop over all the possible <code>odd</code> factors and update the <code>max</code> to the largest prime factor. Now start a loop from <code>i = 5</code> to the square root of <code>n</code>.</p>
<ul>
<li><p>Step-4a - While <code>i</code> divides <code>n</code>, assign <code>max = i</code>, and divide <code>n</code> by <code>i</code>. After <code>i</code> fails to divide <code>n</code>, end this <code>while</code></p>
</li>
<li><p>Step-4b: While <code>i + 2</code> divides <code>n</code>, assign <code>max = i + 2</code>, and divide <code>n</code> by <code>i + 2</code>. After <code>i + 2</code> fails to divide <code>n</code>, end this <code>while</code></p>
</li>
<li><p>After both <code>while</code>, increment <code>i</code> by <code>6</code> and continue. In this way, we will iterate only for integers that do not have prime factors <code>2</code> and <code>3</code></p>
</li>
</ul>
</li>
<li><p>Check if <code>n</code> greater than <code>4</code>. If so, then that would be the largest prime number factor or else <code>max</code> will be the largest prime factor.</p>
</li>
</ul>
<hr />
<h3 id="heading-solution"><strong>Solution</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">largestPrimeFact</span>(<span class="hljs-params">n</span>) </span>{
    <span class="hljs-keyword">let</span> max = <span class="hljs-number">0</span>;

   <span class="hljs-keyword">while</span>(n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
        n /= <span class="hljs-number">2</span>; <span class="hljs-comment">// Equivalent to n = n/2 OR n &gt;&gt;= 1(Right shift assignment)</span>
        max = <span class="hljs-number">2</span>;
    }

    <span class="hljs-keyword">while</span>(n % <span class="hljs-number">3</span> == <span class="hljs-number">0</span>) {
        n /= <span class="hljs-number">3</span>; <span class="hljs-comment">// Equivalent to n = n/3</span>
        max = <span class="hljs-number">3</span>;
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">5</span>; i &lt;= <span class="hljs-built_in">Math</span>.sqrt(n); i += <span class="hljs-number">6</span>) {
        <span class="hljs-keyword">while</span> (n % i == <span class="hljs-number">0</span>) {
            max = i;
            n /= i; <span class="hljs-comment">// Equivalent to n = n/i</span>
        }

        <span class="hljs-keyword">while</span> (n % (i + <span class="hljs-number">2</span>) == <span class="hljs-number">0</span>) {
            maxPrime = i + <span class="hljs-number">2</span>;
            n = n / (i + <span class="hljs-number">2</span>);
        }
    }

    <span class="hljs-keyword">return</span> n &gt; <span class="hljs-number">4</span> ? n : max;
}
<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"L2"</span>);
<span class="hljs-built_in">console</span>.log(largestPrimeFact(<span class="hljs-number">600851475143</span>)); <span class="hljs-comment">// 6857</span>
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"L2"</span>); <span class="hljs-comment">// L2: 0.155029296875 ms</span>
</code></pre>
<p>I have another approach here on GitHub, using a straightforward <code>while</code> loop solution and it throws a <code>timeout</code> error for a very big number on HackerRank. You can find the code here <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/03">03</a>.</p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
<p><strong>Note:</strong></p>
<ol>
<li><p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift_assignment">Right Shift Assignment (&gt;&gt;=).</a></p>
</li>
<li><p>I have created a sheet with the iterations for both approaches <a target="_blank" href="https://docs.google.com/spreadsheets/d/1whvhQA6PqxPjWK0OgVYgsW1i2J3sXcB_xZ61MuopfPM/edit?usp=sharing">here.</a></p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #2 - Even Fibonacci numbers]]></title><description><![CDATA[Problem
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose value...]]></description><link>https://blog.theintrovertcoder.in/project-euler-2-even-fibonacci-numbers</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-2-even-fibonacci-numbers</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[project-euler-solution]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Mon, 02 Jan 2023 06:20:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672123424275/2d859436-acba-46b2-a987-d1ad67fd3c55.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem"><strong>Problem</strong></h3>
<p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with <code>1</code> and <code>2</code>, the first <code>10</code> terms will be:</p>
<p><code>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</code></p>
<p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p>
<hr />
<h3 id="heading-problem-description"><strong>Problem Description</strong></h3>
<p>A Fibonacci Sequence is a series in which each number is the sum of the two preceding ones.</p>
<blockquote>
<p>The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the first few values in the sequence are</p>
<p><code>0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672205631476/09f42241-ebbe-475a-b73c-ab8d9702ee6c.png" alt="A Fibonacci Sequence" class="image--center mx-auto" /></p>
</blockquote>
<p>In our problem, it is stated that the sequence starts with 1 and 2. And the first 10 terms would be, <code>1, 2, 3, 5, 8, 13, 21, 34, 55, 89.</code></p>
<p>We want to find the Fibonacci sequence within 4 million and then find the sum of all the even numbers in that sequence.</p>
<hr />
<h3 id="heading-approach"><strong>Approach</strong></h3>
<p>This problem can be approached using a <code>while</code> loop and solved using the mathematical formula,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672649440431/2ae8381c-2335-4603-8e56-8d0c49c94ab7.png" alt class="image--center mx-auto" /></p>
<p>We need to find the Fibonacci Sequence that starts with <code>1</code> and <code>2</code>, within 4 million. And our sequence looks like this, <code>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ... &lt; 4000000</code></p>
<p>Now, add the even numbers in the sequence, <code>2 + 8 + 34 + 144 + ...</code> which will be our result.</p>
<hr />
<h3 id="heading-solution">Solution</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fn_1 = <span class="hljs-number">1</span>, <span class="hljs-comment">// first</span>
    fn_2 = <span class="hljs-number">2</span>, <span class="hljs-comment">// second</span>
    sum = <span class="hljs-number">0</span>;

<span class="hljs-keyword">while</span> (fn_1 &lt; <span class="hljs-number">4000000</span>) {
    <span class="hljs-keyword">if</span> (fn_1 % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) sum += fn_1; 

    <span class="hljs-keyword">let</span> fn = fn_1 + fn_2; 
    [fn_1, fn_2] = [fn_2, fn];
}
<span class="hljs-built_in">console</span>.time(<span class="hljs-string">'p#2'</span>);
<span class="hljs-built_in">console</span>.log(sum);
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">'p#2'</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672209994039/4227c1e1-3aff-460c-bdcc-605c8339010a.png" alt="Project Euler Problem #2 Solution" class="image--center mx-auto" /></p>
<p>The GitHub repo for this problem is found at <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/02">02</a></p>
<p>If you have another or a better solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler"><strong>Project Euler Solutions in JS.</strong></a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: #1 - Multiples of 3 or 5]]></title><description><![CDATA[Problem
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Problem Description
This is a more straightforward prob...]]></description><link>https://blog.theintrovertcoder.in/project-euler-1-multiples-of-3-or-5</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-1-multiples-of-3-or-5</guid><category><![CDATA[Project Euler]]></category><category><![CDATA[Project Euler Solutions]]></category><category><![CDATA[2Articles1Week]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sun, 01 Jan 2023 10:30:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672051081725/c9d4655a-c636-449a-b758-3daff25c27e6.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-problem">Problem</h3>
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p>
<hr />
<h3 id="heading-problem-description">Problem Description</h3>
<p>This is a more straightforward problem.</p>
<p>The natural numbers below 10 would be <strong>1,2,3,4,5,6,7,8 and 9</strong> (Since it is below 10, 10 is exclusive)</p>
<p>And those multiples of 3 are <strong>3, 6 and 9</strong></p>
<p>And multiples of 5 would be <strong>5</strong>.</p>
<p>So now, the sum of all those multiples would be,</p>
<p>3 + 6 + 9 + 5 = 23.</p>
<p>So now, we want to find the sum of all the multiples of 3 and 5 below 1000 (1000 is exclusive)</p>
<hr />
<h3 id="heading-approach">Approach</h3>
<p>My approach is to get all the multiples of 3 or 5 in an array - <code>multiples</code> and then use <code>Array.prototype.reduce()</code> function to calculate the sum of all items in that array <code>multiples</code></p>
<p>For my solution, the time taken is between 0.11ms and 0.14ms.</p>
<p>Another approach can be to add the multiples directly to a variable, instead of pushing to an array. Inside the <code>if</code>, we can use <code>sum += i</code> and print the sum which is our result.</p>
<p>As I said before, there is more than one approach to each problem!</p>
<hr />
<h3 id="heading-solution">Solution</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> multiples = [];

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">1000</span>; i++) {
    <span class="hljs-keyword">if</span> (i % <span class="hljs-number">3</span> === <span class="hljs-number">0</span> || i % <span class="hljs-number">5</span> === <span class="hljs-number">0</span>) {
        multiples.push(i);
    }
}

<span class="hljs-built_in">console</span>.time(<span class="hljs-string">"p#1"</span>);
<span class="hljs-built_in">console</span>.log(multiples.reduce(<span class="hljs-function">(<span class="hljs-params">acc, curVal</span>) =&gt;</span> acc + curVal));
<span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">"p#1"</span>);
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1672049332926/8cc46463-a941-4f6b-865a-6927bba581c8.png" alt="Project Euler: #1 Solution Answer" /></p>
<p>The GitHub repo for this problem is found at <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript/tree/main/01">01</a></p>
<p>If you have another solution, please leave it in the comments below.</p>
<p>For the other Project Euler Solutions, please follow the series <a target="_blank" href="https://theintrovertcoder.hashnode.dev/series/project-euler">Project Euler Solutions in JS.</a></p>
<p>Thank you!</p>
]]></content:encoded></item><item><title><![CDATA[Project Euler: About the Solution Series]]></title><description><![CDATA[In this series, I will be discussing the solution to the 'Project Euler' Problems.
The 'Project Euler' is a series of challenges that will need more than mathematical insights to solve. It helps in improving problem-solving and programming skills.
We...]]></description><link>https://blog.theintrovertcoder.in/project-euler-about-the-solution-series</link><guid isPermaLink="true">https://blog.theintrovertcoder.in/project-euler-about-the-solution-series</guid><category><![CDATA[Project Euler]]></category><dc:creator><![CDATA[Sangy K]]></dc:creator><pubDate>Sat, 31 Dec 2022 18:35:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672047675456/c346735c-dac6-4929-839a-88f83d298951.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this series, I will be discussing the solution to the 'Project Euler' Problems.</p>
<p>The 'Project Euler' is a series of challenges that will need more than mathematical insights to solve. It helps in improving problem-solving and programming skills.</p>
<p>We can use any programming language to solve problems. In a way, it improves our data structure skills.</p>
<p>Usually, I try to solve the original <a target="_blank" href="https://projecteuler.net/">project euler</a> version of the problem. Then move on to the modified version of it in Hackerrank.</p>
<p>Hackerrank offers a heavily modified version of the problem with more test cases. It also has time and space constraints. So your code should be efficient to pass all the test cases. Most of the time, the brute force approach will time out in hackerrank.</p>
<p>Remember, there is no best solution for any problem.</p>
<p>Some problems involve many ways of solving them. I will be using Javascript to solve the problems.</p>
<p>If you guys find a better solution, please add it to the comment.</p>
<p>I have added all the solutions to GitHub. You can find the repository <a target="_blank" href="https://github.com/sansk/project-euler-solutions-javascript">project-euler-solutions-javascript</a>.</p>
<p><strong>Note:</strong></p>
<ul>
<li><p><em>Please note that the solution provided is only for learning purposes. Once you understand the problem, please try it on your own before referring to the solutions.</em></p>
</li>
<li><p>I am a software engineer and have limited knowledge of mathematics. So while solving some problems, I would refer and link to math-related forums to understand the formula used.</p>
</li>
</ul>
<hr />
<div class="hn-table">
<table>
<thead>
<tr>
<td>Problem</td><td>Title</td><td>PE Solved</td><td>HR Solved</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-1-multiples-of-3-or-5"><strong>Multiples of 3 or 5</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>2</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-2-even-fibonacci-numbers"><strong>Even Fibonacci numbers</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>3</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-3-largest-prime-factor"><strong>Largest prime factor</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>4</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-4-largest-palindrome-product"><strong>Largest palindrome product</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>5</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-5-smallest-multiple"><strong>Smallest multiple</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>6</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-6-sum-square-difference"><strong>Sum square difference</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>7</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-7-10001st-prime"><strong>10001st prime</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>8</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-8-largest-product-in-a-series"><strong>Largest product in a series</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>9</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-9-special-pythagorean-triplet"><strong>Special Pythagorean triplet</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>10</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-10-summation-of-primes"><strong>Summation of primes</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>11</td><td><a target="_blank" href="https://theintrovertcoder.hashnode.dev/project-euler-11-largest-product-in-a-grid"><strong>Largest product in a grid</strong></a></td><td>✅</td><td>✅</td></tr>
<tr>
<td>12</td><td><strong>Highly divisible triangular number</strong></td><td>✅</td><td>✅</td></tr>
<tr>
<td>13</td><td><strong>Large sum</strong></td><td>✅</td><td>✅</td></tr>
<tr>
<td>14</td><td><strong>Longest Collatz sequence</strong></td><td>✅</td><td>✅</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item></channel></rss>