[–] 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>
char *a;
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
;#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' ')#\
"!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 


[–] 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.


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.


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.


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.


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:



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



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.


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

Like this?

<!DOCTYPE html>
N = 1000;
a = [1, 1];

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

<!-- [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 );
<h1>JavaScript solution to Project Euler &#35;2</h1>
<p>The answer is:
<noscript>[Javascript is required]</noscript>