JSFiddle - React, Tailwind, and code Playground

by Andrew Poes

HTML

<!-- Euler Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million. -->

CSS

.print {
    position: relative;
    display: inline-block;
    background-color: black;
    color: white;
    font-family: Helvetica, Helvetica-Neue, sans-serif;
    font-weight: bold;
    font-size: 24px;
    letter-spacing: -1.5px;
    padding: 4px 8px;
}

body {
    background-color: #eeeeee;
}
}

JavaScript

$(document).ready(function() {
    var target = 2000000
    var sum = 0
    for (i = 2; i < target; ++i) {
        if (isPrime(i)) {
            sum += i
        }
    }
    print("sum below " + target, sum)
})

function isPrime(num) {
    if (num < 2) {
        return false
    }
    // Prime numbers other than two are odd...
    if (num == 2) {
        return true
    }
    else if (num%2 == 0) {
        return false
    }
    // Check it isn't divisible by up to its square root
    // (consider n=(root n)(root n) as factors)
    for (var i = 3; i <= Math.sqrt(num); ++i) {
        if (num%i == 0) {
            return false
        }
    }
	return true
}

function print() {
    var args = Array.prototype.slice.apply(arguments)
    var str = ""
    for (arg of args) {
        str += arg + ", "
    }
    str = str.substring(0, str.length - 2)
    var el = newel(str)
    $("body").append(el)
    $("body").append("</br>")
}

function newel(str) {
    var el = document.createElement("div")
    $(el).html(str)
    $(el).addClass("print")
    return el
}