Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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)

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}