Updates:

You can now choose from a number of pre-installed themes

Factorial competition ..

Started by Mad Penguin, July 30, 2013, 04:40:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mad Penguin

For anyone that's bored, what's the most concise code you can come up with to print out factorial(n) for n in the range 1-11 , in your language of choice?
- If there are any bored Perl programmers about I'm expecting Perl to win .. here's my best attempt in "C" ... be interesting to see how different languages compare [?!]

#include <stdio.h>

long factorial(long n) { return n==1?n:n*factorial(n-1); }

int main() { for(int i=1;i<12;i++) printf("Factorial %d = %ld\n",i,factorial(i)); }
https://twitter.com/garethbult
https://gareth.bult.co.uk

Mad Penguin

Ok, here's the Python submission;

def factorial(n):return reduce(lambda x,y:x*y,[1]+range(1,n+1))
Fractionally longer and IMHO a lot less readable [?]
https://twitter.com/garethbult
https://gareth.bult.co.uk


Mad Penguin

Anybody for LISP ?

(defun factorial (n)  (if (= n 0) 1 (* n (factorial (- n 1))) ) )
https://twitter.com/garethbult
https://gareth.bult.co.uk