Find Prime Numbers in Range

Published: 2017-05-21 11:09:59 +0000
Categories: Javascript,




Will find and identify any prime numbers between the given start and end values. Primes are identified using a factorial.

Originally written as part of a solution for my May 2016 puzzle. The original solution was written in Python, this snippet is part of a (successful) attempt to rewrite that in javascript so that the solution can simply be dropped into the Developer Tools console to solve the puzzle in browser.

Similar to

Based On


// Calculate all primes between a lower and upper bound
function calculatePrimesForRange(l,h){
      var primes=[];
      for (i=l; i<h; i++){

          if (checkNumPrime(i)){

      return primes;

// Check whether a number is a prime
function checkNumPrime(n){

      // Check whether the number is 1,2 or 3
      if (n<4){
          return true;

      // Check the number isn't directly divisible by 2 or 3
      if (n%2 == 0 || n%3 == 0){
          return false;

      var di=2;
      var i = 5;

      // Don't calculcate higher than the square root (rounded down if needed)
      var lim = Math.floor(Math.sqrt(n));

      while (i < lim){

        if (n%i == 0){
            return false;

      // If we haven't already returned, n is prime
      return true;


Usage Example





Primes, numbers, range, factorial,

Latest Posts

Remotely backing up PFsense Configuration (BASH)
FFMPEG: Converting RMVB to X264 MP4 (BASH)
Recursively print table (print_r equivalent) (LUA)
Bulk Delete Comments from (Self-Hosted) JIRA Issues (Misc)
Add a static entry to the ARP table (BASH)
SSL Cipher Hex codes to Human Readable Names (Misc)
Convert Ascii to Binary (BASH)
Intercepting Outbound DNS Queries (BASH)
Handle Google Verification files within NGinx Configuration (NGinx)
Getting WhatsApp Rich Snippet Previews Working (Misc)

Copyright © 2018 Ben Tasker | Sitemap | Privacy Policy
Available at and snippets.6zdgh5a5e6zpchdz.onion