Skip to content

Instantly share code, notes, and snippets.

@delonnewman
Created June 23, 2013 05:59
Show Gist options
  • Save delonnewman/5843976 to your computer and use it in GitHub Desktop.
Save delonnewman/5843976 to your computer and use it in GitHub Desktop.
open System
let funnysums n =
seq { 1 .. n } |> Seq.map (fun i -> seq { 0 .. i } |> Seq.reduce(+))
[<EntryPointAttribute>]
let main args =
let sums = funnysums(Int32.Parse(args.[0]))
printfn "%A" sums
0
import System.Environment
funnysum :: Int -> [Int]
funnysum n = map (\x -> sum [0..x]) [0..n]
main = do
argv <- getArgs
let num = (read (head argv))::Int
putStrLn $ show (funnysum num)
function funnysums(n) {
var i = 0;
var sums = [];
for (; i <= n; i++) {
var j = 0;
var sum = 0;
for (; j <= i; j++) {
sum += j;
}
sums.push(sum);
}
return sums;
}
console.log(funnysums(30));
#!/usr/local/bin/perl6
sub MAIN($n) {
(0..$n).map({ [+] 0..$_ }).say
}
#!/usr/bin/env python
import sys
def funnysums(n):
return map(lambda i: sum(range(1, i+1)), range(0, n+1))
if __name__ == '__main__':
n = int(sys.argv[1])
print funnysums(n)
module FunnySum
module Recursive
def sum n
if n == 0 then 0
else
n + sum(n - 1)
end
end
def sums n
if n == 0 then [ 0 ]
else
sum(n) + sums(n - 1)
end
end
end
def sums n
(0..n).map { |i| (0..i).reduce(:+) }
end
end
object FunnySum {
def funnysums(n : Int) : List[Int] = {
List.range(1, n+1).map(List.range(0, _).reduceLeft[Int](_+_))
}
def main(args : Array[String]) : Unit = {
println(funnysums(args(0).toInt))
}
}
#include "rb-funny-sum.h"
static VALUE rb_funny_sum(VALUE mod, VALUE n) {
long sum = funny_sum(NUM2INT(n));
return INT2NUM(sum);
}
static VALUE rb_funny_sums(VALUE mod, VALUE n) {
VALUE sums = rb_ary_new();
long i;
long num = NUM2INT(n);
for (i = 0; i <= num; i++) {
rb_ary_store(sums, i, INT2NUM(funny_sum(i)));
}
return sums;
}
void Init_FunnySum() {
mFunnySum = rb_define_module("FunnySum");
rb_define_module_function(mFunnySum, "sum", rb_funny_sum, 1);
rb_define_module_function(mFunnySum, "sums", rb_funny_sums, 1);
rb_define_global_function("funnysum", rb_funny_sum, 1);
rb_define_global_function("funnysums", rb_funny_sums, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment