Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Polyglot Factorial

Someone on Hacker News mentioned the number of orderings of a deck of cards. I took up the challenge in some of my favorite and not so favorite languages, I’ll let you guess :).

Ruby

ruby-1.9.2-p180> (1..52).inject{|a,b|a*b}
=>
80658175170943878571660636856403766975289505440883277824000000000000

Scala

scala> BigInt(1).to(BigInt(52)).toList.foldLeft(BigInt(1))((a,b) => a*b)
res23: scala.math.BigInt =>
80658175170943878571660636856403766975289505440883277824000000000000

Haskell

Prelude> foldl1 (x -> y -> x*y) [1..52]
80658175170943878571660636856403766975289505440883277824000000000000

CoffeeScript

coffee> [1..52].reduce (a,b) -> a*b

8.065817517094388e+67

Java

Non-existent REPL>
import java.math.*;
public class Factorial {
public static void main(String[] args) {
BigInteger result = BigInteger.ONE;
BigInteger fiftyTwo = new BigInteger(”52″);
for(BigInteger i = BigInteger.ONE; i.compareTo(fiftyTwo) <= 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(i);
}
System.out.println(result);
}
}

$ java Factorial
80658175170943878571660636856403766975289505440883277824000000000000

** I could have used the product function in most of those examples, but I wanted to play with foldL ;)

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This