You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">3</span>; i <= limit; i+=<spanclass="hljs-number">2</span>)
1823
+
<spanclass="hljs-keyword">if</span> (num % i === <spanclass="hljs-number">0</span>) <spanclass="hljs-keyword">return</span><spanclass="hljs-literal">false</span>;
1824
+
<spanclass="hljs-keyword">return</span> num > <spanclass="hljs-number">1</span>;
1825
+
};
1826
+
</code></pre>
1827
+
<p>We can keep creating slightly more efficient versions by eliminating multiples of known primes.</p>
1828
+
<blockquote>
1829
+
<p>For 2 & 3, it boils down to <code>6k + i</code>. We can even write more efficient versions following the same pattern <code>(p1 * p2 * ... * pn)k + i</code>.</p>
<spanclass="hljs-keyword">for</span> (<spanclass="hljs-keyword">let</span> i = <spanclass="hljs-number">5</span>; i <= limit; i+=<spanclass="hljs-number">6</span>)
1836
+
<spanclass="hljs-keyword">if</span> (num % i === <spanclass="hljs-number">0</span> || num % (i + <spanclass="hljs-number">2</span>) === <spanclass="hljs-number">0</span>) <spanclass="hljs-keyword">return</span><spanclass="hljs-literal">false</span>;
1837
+
<spanclass="hljs-keyword">return</span> num > <spanclass="hljs-number">1</span>;
0 commit comments