[–] lili3 0 points 1 points (+1|-0) ago 

in as few characters as possible

have you heard of jsfuck? all JS code can be reduced down to 6 characters: ()[]!+

I entered your solution and while it only used 6 characters, it did so 114,000 times hahaha

[–] Satirical [S] 0 points 0 points (+0|-0) ago 

Well, lowest character count then, if we're being pedantic :^)

[–] ThisIsMyRealName 0 points 1 points (+1|-0) ago 

That's disgusting and unreadable. Good job.

[–] boredTech 0 points 1 points (+1|-0) ago 

Still not at expert level though

#include <stdio.h>
main(t,_,a)
char *a;
{
return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')#\
}'+}##(!!/")
  :t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
    :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}

[–] Satirical [S] 0 points 0 points (+0|-0) ago 

bravo

[–] ThisIsMyRealName 0 points 0 points (+0|-0) ago 

That's C though.

[–] RevanProdigalKnight 0 points 0 points (+0|-0) ago  (edited ago)

I'm a few days weeks late to the party, but you can actually accomplish the same in fewer characters.

First, in your current solution, the c variable is completely unused, so it can be stripped.

for(s=0,i=0,j=1;;i=j,j=x){x=i+j;if(x<4000000){if(x%2===0){s+=x}}else{console.log(s);break;}}

That strips (4 + 9 + 4) = 17 characters from the solution without affecting the output. But we can still do better.

4000000 === 4e6, which strips a further 4 characters.

for(s=0,i=0,j=1;;i=j,j=x){x=i+j;if(x<4e6){if(x%2===0){s+=x}}else{console.log(s);break;}}

So far we're down 21 characters, but there's still more we can strip. In JS, we can replace if statements with truthy conditions using &&. Now, we can't replace the outer if/else block with this, and a ternary causes a syntax error, but the inner if statement can be replaced. Unfortunately, x%2===0&&s+=x also causes a ReferenceError to be thrown, because the left hand side of that statement is evaluated as x%2==0&&s. This means we must wrap s+=x in parentheses, and we have saved only 2 characters.

for(s=0,i=0,j=1;;i=j,j=x){x=i+j;if(x<4e6){x%2===0&&(s+=x)}else{console.log(s);break;}}

Now, as you may have guessed from the above, if an if statement with a truthy condition can be replaced with &&, then an if statements with a falsy condition can be replaced with ||, which allows the much shorter overall syntax of x%2||(s+=x). This saves us another 4 characters.

for(s=0,i=0,j=1;;i=j,j=x){x=i+j;if(x<4e6){x%2||(s+=x)}else{console.log(s);break;}}

With the new inner statement, we can also omit the curly braces and simply separate the if and the else with a semicolon, which saves us one more character, and we can also omit the semicolon after the break statement at the end for one last character. This brings us to a total savings of 29 characters, resulting in the final solution of:

for(s=0,i=0,j=1;;i=j,j=x){x=i+j;if(x<4e6)x%2||(s+=x);else{console.log(s);break}}

EDIT:

Realized there's actually two more characters that can be cut out by assigning x inside the outer if statement, for 31 total characters in savings (and a total length of 78):

for(s=0,i=0,j=1;;i=j,j=x)if((x=i+j)<4e6)x%2||(s+=x);else{console.log(s);break}

EDIT 2:

Another two characters can be cut out by assigning s=i=0 in the for loop declaration, for 33 characters saved and total length of 76.

I think that's all the more that can be cut out.

for(s=i=0,j=1;;i=j,j=x)if((x=i+j)<4e6)x%2||(s+=x);else{console.log(s);break}

[–] rcb 1 points -1 points (+0|-1) ago  (edited ago)

Like this?

<!DOCTYPE html>
<head>
<script>
N = 1000;
a = [1, 1];
len=a.length;

while(a[len-1] <= N) { tmp=a[len-1]+a[len-2]; a.push(tmp); len=a.length; }
a.pop();

<!-- [https://code.tutsplus.com/tutorials/how-to-use-map-filter-reduce-in-javascript--cms-26209](https://code.tutsplus.com/tutorials/how-to-use-map-filter-reduce-in-javascript--cms-26209) -->
<!-- [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) -->

ans = a.filter( function(x) { return (x % 2 == 0); } )
       .reduce( function(acc, val) { return acc+val; }, 0 );
</script>
</head>
<body>
<h1>JavaScript solution to Project Euler &#35;2</h1>
<p>The answer is:
<noscript>[Javascript is required]</noscript>
<script>document.write(ans);</script>.
</body>
</html>