<p>Do a prime number search from <input id="from" value="1"> to <input id="to" value="500000" size="200">.</p>
<button onclick="doSearch()">Start Searching</button>

<div id="primeContainer">
</div>

<div id="status"></div>
body {
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  margin: 20px;
}

#primeContainer {
  border: solid 1px black;
  padding: 3px;
  height: 300px;
  max-width: 500px;
  overflow: scroll;
  overflow-x: hidden;
  font-size: x-small;
  margin-top: 20px;
  margin-bottom: 10px;
}

input {
  width: 80px;
}

button {
  padding: 3px;
}

p {
  margin-bottom: 3px;
}

#status {
  color: darkred;
}

function doSearch() {
  // Get the two numbers in the text boxes. This is the search range.
  var fromNumber = document.getElementById("from").value;
  var toNumber = document.getElementById("to").value;

  var statusDisplay = document.getElementById("status");
  statusDisplay.innerHTML = "Starting new search...";    
  
  // Perform the search.
  var primes = findPrimes(fromNumber, toNumber);

  // Take the results, loop over it,
  // and paste it into one long piece of text.
  var primeList = "";
  for (var i=0; i<primes.length; i++) {
    primeList += primes[i];
    if (i != primes.length-1) primeList += ", ";
  }
  
  // Show the prime number list on the page.
  var primeContainer = document.getElementById("primeContainer");
  primeContainer.innerHTML = primeList;

  var statusDisplay = document.getElementById("status");
  if (primeList.length == 0) {
    statusDisplay.innerHTML = "Search didn't find any results.";
  }
  else {
    statusDisplay.innerHTML = "The results are here!";
  }
}


function findPrimes(fromNumber, toNumber) {

  // Create an array containing all integers
  // between the two specified numbers.
  var list = [];
  for (var i=fromNumber; i<=toNumber; i++) {
    if (i>1) list.push(i);
  }

  // Test for primes.
  var maxDiv = Math.round(Math.sqrt(toNumber));
  var primes = [];

  for (var i=0; i<list.length; i++) {
    var failed = false;
    for (var j=2; j<=maxDiv; j++) {
      if ((list[i] != j) && (list[i] % j == 0)) {
        failed = true;
      } else if ((j==maxDiv) && (failed == false)) {
        primes.push(list[i]);
      }
    }
  }

  return primes;
}
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.