Euler Problem #10

by gcarino

HTML

<div id='header'><h1>Project Euler Problem 4:</h1></div>
<div id='container'><h4>The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.</h4>
<p id='ans'></p>
</div>

CSS

h1{
    text-align:center;
    color:#FFFFFF;
}

#header {
    height:80px;
    width:400px;
    background-color: #1E90FF;
    border-radius:20px;
    margin:auto;
    border:solid 4px #008B8B;
}

#container {
    background-color:gainsboro;
    height:130px;
    padding: 10px;
    margin-top:15px;
    border:solid 4px grey;
    border-radius: 5px;

}

JavaScript

function isPrime(n){
    if(n===2 || n ===3){
        return true;
    } else if(n%2 === 0){
        return false;
    }
    var max = Math.sqrt(n);
    for(var i=3 ; i<=max; i+=2){
        if(n%i === 0){
            return false;
        }
    }
    return true;
}

var sum = 2;
for( var i=3 ; i<= 2000000; i+=2){
    if(isPrime(i)){
        sum += i;
        console.log(i);
    }
}

var ans = document.createTextNode('Answer = ' + sum);
document.getElementById('ans').appendChild(ans);