(reduce + (range 1000000001))
Written that way, though, it takes a long time (108 sec, compared to 2.6 sec in C). So that's not fast, as elegant as it may be.This is what I have for C. It's probably not great C code.
int main() {
long res = 0;
int i;
for (i = 0; i <= 1000000000; i++) {
res += i;
}
printf("%ld\n", res);
return 0;
}
Faster than (naive?) C is this Clojure loop (no range object). user=> (time (loop [i 0 res 0]
(if (> i 1000000000)
res
(recur (inc i) (+ res i)))))
"Elapsed time: 1612.094 msecs"
500000000500000000