Given an integer n, calculate the sum of all even numbers between 0 and n


n     output
100   2550
255   16256
1000  2502500

Shortest code in bytes wins

  • \$\begingroup\$ What are the expected inputs and outputs? What's the winning criterion? Please put them in your question instead of in the tags. Also, I'm fairly sure this is a dupe. \$\endgroup\$ – clismique Oct 7 '16 at 1:20
  • \$\begingroup\$ I closed as a dupe of summing odds. I don't think evens and lack of a lower bound changes much. Be aware that that question was poorly received, likely for being too easy. In the future, I'd suggest running challenge ideas through the sandbox on meta. \$\endgroup\$ – xnor Oct 7 '16 at 1:27
  • \$\begingroup\$ @xnor Fair enough. Though this allows for something much simpler like (n/=2)*n+n. \$\endgroup\$ – Geobits Oct 7 '16 at 1:30

Scala - 18 bytes

shortest I could figure:

(0 to i by 2).sum

Using an anonymous function:

val i = (N:Int) => (0 to N by 2).sum
  • \$\begingroup\$ Your first snippet is neither a function nor a program, you can't assume that a predefined variable exists. \$\endgroup\$ – corvus_192 Oct 7 '16 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.