euler_one
https://projecteuler.net/problem=1 'standard' solution CallStream enabled improvement
by dbjdbj
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
CSS
* { font: 16px monospace; }
li.dbj_li { border:0px solid ; margin:2px; list-style: none outside; }
span.dbj_span { border: 1px solid; margin: 2px; overflow-x: hidden; }
JavaScript
if ( "undefined" == jQuery ) return alert("jQuery not here?");
// this code is inside my namespace
// toString shortcut defined elsewhere
var TOS = Object.prototype.toString ;
let roleof = function(o) {
return o === undefined ? "undefined" : o === null ? "null" :
TOS.call(o).match(/\w+/g)[1];
}
function print (s) {
return setTimeout(function () {
document.body.innerHTML += "<li class='dbj_li'>"+s+"</li>" ; }, 1
) ;
};
function prinx (exp) {
function evil ( arg ) {
try {
return eval(arg) ;
} catch (x) {
return "" + x ;
}
}
function wid ( str_, w_ )
{
let $outer = $("<span><div></div></span>");
let $inner = $outer.find("div").html(str_)
.css("float", "left")
.css("width", w_);
return $outer.html() ;
}
print(
wid(exp, "150px") +
wid(" → ", "50px") +
wid(evil(exp), "150px") +
"<br style='clear:both'/>" );
};
// "building block" function
function sum_n ( max, div ) {
var rez = 0 ;
while ( max-- > div) if ( max % div== 0 ) rez += max ;
return rez ;
}
// https://projecteuler.net/problem=1
// 'standard' solution
var result = sum_n(1000,3) + sum_n(1000,5) - sum_n(1000,3*5) ; // gives: 233168
/*
lazy initialization pattern
*/
function is4d(val) {
var rx = (/^[0-9]{4}$/);
is4d = function (val) {
return !! val.match(rx);
};
return is4d(val);
}
prinx( 'is4d("1234")' ) ;
prinx( 'roleof(is4d)' ) ;
prinx( "roleof('+')" ) ;
// print( "'+'.charCodeAt(0)")