Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.1/platform.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.0/benchmark.js"></script>

test of several primes-calculating algorythms, results in console
<div id="console"></div>
              
            
!

CSS

              
                		#console{
			font-size : 35px;
		}
              
            
!

JS

              
                var suite = new Benchmark.Suite;
function sumPrimesBasic(num) {
  var res = 0;

  // Function to get the primes up to max in an array
  function getPrimes(max) {
    var sieve = [];
    var i;
    var j;
    var primes = [];
    for (i = 2; i <= max; ++i) {
      if (!sieve[i]) {
        // i has not been marked -- it is prime
        primes.push(i);
        for (j = i << 1; j <= max; j += i) {
          sieve[j] = true;
        }
      }
    }

    return primes;
  }

  // Add the primes
  var primes = getPrimes(num);
  for (var p = 0; p < primes.length; p++) {
    res += primes[p];
  }

  return res;
}
function sumPrimesE(num) {

  var max_sqrt = Math.sqrt(num); //max # needed to be checked as a divisor
  //initialize array of "2" followed by all the odd #s <= num
  var primes = [2];
  for (var j=1; j<num/2; j++)
    primes.push(j*2+1); 
  
  var i = 1;  //iterate over array removing divisors of the current item
  while(primes[i] <= max_sqrt) {
    var pofISq = Math.pow(primes[i], 2);
    primes = primes.filter((primeFilt));
    i++;
  }
  
  function primeFilt(el) {
    if (el < pofISq) return true; //don't need to check items less than the current item squared
    return (el % primes[i] != 0); //non-divisors stay
  }
  
  return primes.reduce((a,b) => a+b);

}


function sumPrimesIntermediate(num) {
  // function to check if the number presented is prime
  function isPrime(number){
      for (i = 2; i <= number; i++){
          if(number % i === 0 && number!= i){
          // return true if it is divisible by any number that is not itself.
             return false;
          }
       }
       // if it passes the for loops conditions it is a prime
      return true;
  }
  // 1 is not a prime, so return nothing, also stops the recursive calls.
  if (num === 1){
    return 0;
  }
  // Check if your number is not prime
  if(isPrime(num) === false){
  // for non primes check the next number down from your maximum number, do not add anything to your answer
    return sumPrimesIntermediate(num - 1);
  }

  // Check if your number is prime
  if(isPrime(num) === true){
  // for primes add that number to the next number in the sequence through a recursive call to our sumPrimes function.
    return num + sumPrimesIntermediate(num - 1);
  }
}

function sumPrimesOshliaer(num) {
  var res = 1;
  var arr = [];
  var counter = 1;
  while(counter <= num){
    if(arr.filter(function(v){ return this % v === 0;}, counter).length < 2){
      arr.push(counter);
      res += counter;
    } 
    counter += 2;
  }
  return res;
}
function sumPrimesP1xt(num) {
  var sieve = new Array(num+1).fill(true);
  var maxCheck = Math.ceil(Math.sqrt(num));
  var i, j;
  var results = 0;

  for (i = 2; i <= maxCheck; i++) {
      if (sieve[i] === true) {
          for (j = i * i; j <= num; j += i) {
              sieve[j] = false;
          }
      }
  }

  for (i=2; i<=num; i++) {
      if (sieve[i]) results += i;
  }
  return results;
}
// add tests

var runs = 10;
suite
.add('sumPrimesBasic', function() {
    sumPrimesBasic(runs);
})
.add('sumPrimesIntermediate', function() {
    sumPrimesIntermediate(runs);
})
.add('sumPrimesOshliaer', function() {
    sumPrimesOshliaer(runs);
})
.add('sumPrimesP1xt', function() {
    sumPrimesP1xt(runs);
})
.add('sumPrimesE', function() {
    sumPrimesE(runs);
})



// add listeners
.on('cycle', function(event) {
   console.log(String(event.target));
})
.on('complete', function() {
   console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });

              
            
!
999px

Console