Euler Problem 20
by Andrew Poes
HTML
<!-- n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100! -->
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 a = [1];
var count = 1;
var target = 100;
while (count <= target) {
a = multiplyArray(a, count);
++count;
}
var total = sum.apply(null, a);
print(total)
})
function sum() {
var f = 0;
for (var i = 0; i < arguments.length; ++i) {
f += arguments[i];
}
return f;
}
function multiplyArray(a, b) {
var carry = 0;
for (var i = 0; i < a.length; ++i) {
var d = a[i] * b + carry;
a[i] = d%10;
carry = (d / 10) << 0;
}
while (carry > 0) {
a[i++] = carry%10;
carry = (carry/10)<<0;
}
return a;
}
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
}