Euler Problem 23

by Andrew Poes

HTML

<!-- A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. -->

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;
}
}

input {
    padding: 20px;
}
}

JavaScript

$(document).ready(function() {
    var seive = seiveOfAbundants(28123);
    var allSums = allSumsOfAbundants(seive);
    var s = 0;
    for (var i = 1; i <= 28123; ++i) {
        var key = i.toString();
        if (allSums[key] == undefined) {
            s += i;
        }
    }
    print(s);
})

function seiveOfAbundants(limit) {
    var ret = [];
    for (var i = 1; i < limit; ++i) {
        //var f = factors(i);
        //var s = sum.apply(null, f);
        if (divisorsSum(i) > i) {
            ret.push(i);
        }
    }
    return ret;
}

function factors(num) {
    var limit = Math.sqrt(num) << 0
	if (limit == 1) {
        if (num > 1) {
            return [1];
        }
        else {
            return [0];
        }
    }
    var factors = [];
    for (var i = 1; i <= limit; ++i) {
        var d = num / i;
        if (d%1 == 0) {
            factors.push(i);
            if (d != num && d != i) {
	            factors.push(d);
            }
        }
    }
    return factors;
}

function divisorsSum(n) {
    var sum = 0;
    var limit = Math.sqrt(n)<<0;
	for(var i = 2; i <= limit; ++i) {
        if (n%i==0) {
            sum += i + n/i;
        }
    }
    if ((limit * limit) == n) {
        return sum - limit;
    }
    return sum;
}

function allSumsOfAbundants(abundants) {
    var sums = {};
    for (var i = 0; i < abundants.length; ++i) {
        for (var j = 0; j < abundants.length; ++j) {
            var sum = abundants[i] + abundants[j];
            if (sum > 28123) {
                continue;
            }
            var key = sum.toString();
            sums[key] = true;
        }
    }
	return sums;
}

function sum() {
    var s = 0;
    for (var i = 0; i < arguments.length; ++i) {
        s += arguments[i];
    }
    return s;
}

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)
   ...