Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Example - Factorial

Factorial n (usually written n!) is the product of all integers up to and including n (1 * 2 * 3 * ... * n). This problem is often (inappropriately) used as a programming example, especially to show how to write recursive functions.

Recursive solution is a bad example

Writing a recursive factorial function is a mistake because
  • The recursive solution's memory usage is O(N) instead of O(1).
  • It's more complicated for students.
  • It's inappropriate when the iterative solution is better.
Recursion is a great tool, but using it where it's not appropriate doesn't set a great example.

Range and precision problems

Even the iterative solution has problems because the large numbers that factorial generates cannot be represented in the limited range of integers, or limited precision of floating-point numbers. Programs using these primitive types are very dangerous because they will produce garbage results without comment.

Solution with java.math.BigInteger

The proper solution is to use java.Math.BigInteger. Here is a program with int and BigInteger solutions.
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
// numbers/Factorial.java - Computes factorial
// Fred Swartz - 2003-11-02
import java.math.BigInteger;
public class Factorial {
    public static void main(String[] args) {
        
        //-- BigInteger solution.
        BigInteger n = BigInteger.ONE;
        for (int i=1; i<=20; i++) {
            n = n.multiply(BigInteger.valueOf(i));
            System.out.println(i + "! = " + n);
        }
        
        //-- int solution (BAD IDEA BECAUSE ONLY WORKS TO 12).
        int fact = 1;
        for (int i=1; i<=20; i++) {
            fact = fact * i;
            System.out.println(i + "! = " + fact);
        }
    }
}

Sample output

An int produces pure garbage after 12, and long only makes it to 20. The shame of integer arithmetic is that it doesn't stop when the values are bad -- it just produces garbage by throwing away bits in the result that don't fit in an int (32 bits) or long (64-bits). BigInteger values are limited only by the amount of memory.      
BigInteger outputint output
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504 BAD
14! = 1278945280 BAD
15! = 2004310016 BAD
16! = 2004189184 BAD
17! = -288522240 BAD
18! = -898433024 BAD
19! = 109641728 BAD
20! = -2102132736 BAD
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (post your own) View All Comments Latest 10 Comments:

give me for c programing that with do..while statement for factorial number

Posted by chaneateya on Tuesday, 03.10.09 @ 21:54pm | #85712

ANOTHER JAVA FACTORIAL

Posted by rjay on Tuesday, 02.24.09 @ 20:26pm | #85176

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]