Groovy, ColdFusion, Java, PHP, C#... I enjoy programming. Father, software engineer, programming language nerd, and entrepreneur, I'm bringing nerdy back. Adam has posted 9 posts at DZone. View Full User Profile

Language Comparison: Compute Factorial - Groovy, Java, and ColdFusion

05.19.2011
| 10471 views |
  • submit to reddit
A friend of mine is in school and taking some classes in Java, and also happens to be learning ColdFusion on the side. He was talking about the class and how they were learning about recursion and how they used the example of computing the factorial of a given number. Just for fun I took that opportunity to do this in a couple of languages... just to see what they look like side by side.

Please note there are no advantages or disadvantages to any of these code samples, but instead is shown as something educational... food for the brain if you will. :)

**ColdFusion**
<cffunction name="findFactorialCF" returntype="numeric" access="public" output="false">
   <cfargument name="n" type="numeric" required="true">
   <cfreturn (arguments.n="" eq="" 1)="" ?="" 1="" :="" arguments.n="" *="" findfactorialcf(n="" -="">
</cfreturn></cfargument></cffunction>
 
<cfoutput>
#findFactorialCF(11)#
</cfoutput>


**Java**
package com.adampresley.factorial
 
class Factorial
{
   public static void main(String[] args) {
      System.out.println(findFactorialJava(11));
   }
 
   public static int findFactorialJava(int n) {
      return (n == 1) ? 1 : n * findFactorialJava(n - 1);
   }
}


**Groovy**
def findFactorialGroovy = { n -> (n == 1) ? 1 : n * call(n - 1) }
println findFactorialGroovy(11)
References
Published at DZone with permission of its author, Adam Presley. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Russel Winder replied on Thu, 2011/05/19 - 8:18am

Without tail recursion the implementations are S(n) = O(n) and T(n) = O(n) which really isn't good compared to the iterative version S(n) = O(1) and T(n) = O(n).

Raymond Camden replied on Thu, 2011/05/19 - 9:59am

The ColdFusion code sample is _seriously_ broken. I'm assuming a bad paste. But folks - please do not assume that is valid CFML.

Raymond Camden replied on Thu, 2011/05/19 - 10:04am

Here are two working examples. One using a loop, one using recursion. The loop is based on Rob Brooks-Bilson's UDF from CFLib.org. I just slimmed it down a bit. Was going to paste code, but it's too much trouble to encode: http://pastebin.com/CT4f2ME1

Carlos Hoces replied on Thu, 2011/05/19 - 10:07am

If I am not wrong, the code should be:

 public static int findFactorialJava(int n) {
      return n <= 1 ? 1 : n * findFactorialJava(n - 1);
   }
instead of the n == 1 posted. BTW, useless parenthesis make "noise" :)
I wonder what would happen in case I make a call like this one:
System.out.println(findFactorialJava(Integer.MAX_VALUE)); 
 

 

Mitch Pronschinske replied on Thu, 2011/05/19 - 10:09am in response to: Raymond Camden

Thanks Raymond,

 You can take a look at the source of the blog through the reference box, but I just reposted Adam's blog did a simple copy and paste.  Thanks for putting up some better examples in the comments.

darryl west replied on Thu, 2011/05/19 - 10:12am

I agree with Raymond's comment "cold fusion is seriously broken".  Who keeps paying to post the CF crap anyway?  Shouldn't it be in some HTML tag-hell forum?

Alex Tkachman replied on Thu, 2011/05/19 - 4:42pm in response to: Russel Winder

Groovy++ tail recursive version long fact(int n, long acc = 1) { n > 1 ? fact(n-1, n*acc) : acc }

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.