Three ways to implement factorial in Java

Source: Internet
Author: User

There are three ways to implement the n! implementation using the BigDecimal class, because the int is correctly calculated to 12!, with a long correct count to 20!. Nonsense not much to say, directly on the code:

The first method:

Java code

  1. 1.package factorial;
  2. 2.
  3. 3.import Java.math.BigDecimal;
  4. 4.import Java.util.Scanner;
  5. 5.public class Factorial1 {
  6. 6. public static BigDecimal factorial (int n) {
  7. 7. BigDecimal result = new BigDecimal (1);
  8. 8. BigDecimal A;
  9. 9. for (int i = 2; I <= n; i++) {
  10. A = new BigDecimal (i);
  11. One. Result = Result.multiply (a);
  12. 12.}
  13. return result;
  14. 14.}
  15. 15.
  16. public static void Main (string[] arguments) {
  17. Scanner sc = new Scanner (system.in);
  18. int a = Sc.nextint ();
  19. System.out.println (A + "! =" + factorial (a));
  20. 20.}
  21. 21st.
  22. 22.}
  23. package factorial;

  24. Import Java.math.BigDecimal;
  25. Import Java.util.Scanner;
  26. public class Factorial1 {
  27. public static BigDecimal factorial (int n) {
  28. BigDecimal result = new BigDecimal (1);
  29. BigDecimal A;
  30. for (int i = 2; I <= n; i++) {
  31. A = new BigDecimal (i);
  32. result = Result.multiply (a);
  33. }
  34. return result;
  35. }

  36. public static void Main (string[] arguments) {
  37. Scanner sc = new Scanner (system.in);
  38. int a = Sc.nextint ();
  39. System.out.println (A + "! =" + factorial (a));
  40. }

  41. }
Copy Code

The second method:





Java code

  1. The second method of 1.//
  2. 2.package factorial;
  3. 3.
  4. 4.import Java.math.BigDecimal;
  5. 5.import Java.util.Scanner;
  6. 6.public class Factorial3 {
  7. 7. public static BigDecimal factorial (BigDecimal N) {
  8. 8. BigDecimal BD1 = new BigDecimal (1);
  9. 9. BigDecimal Bd2 = new BigDecimal (2);
  10. Ten. BigDecimal result = BD1;
  11. One. while (N.compareto (BD1) > 0) {
  12. result = Result.multiply (n.multiply (N.subtract (BD1)));
  13. n = n.subtract (BD2);
  14. 14.}
  15. . return result;
  16. 16.}
  17. . public static void Main (string[] arguments) {
  18. Scanner sc = new Scanner (system.in);
  19. BigDecimal n = sc.nextbigdecimal ();
  20. 20.
  21. System.out.print (n + "! =" + factorial (n));
  22. 22.}
  23. 23.
  24. 24.}

The second method of

  1. package factorial;

  2. Import Java.math.BigDecimal;
  3. Import Java.util.Scanner;
  4. public class Factorial3 {
  5. public static BigDecimal factorial (BigDecimal N) {
  6. BigDecimal bd1 = new BigDecimal (1);
  7. BigDecimal bd2 = new BigDecimal (2);
  8. BigDecimal result = BD1;
  9. while (N.compareto (BD1) > 0) {
  10. result = Result.multiply (n.multiply (N.subtract (BD1)));
  11. n = n.subtract (BD2);
  12. }
  13. return result;
  14. }
  15. public static void Main (string[] arguments) {
  16. Scanner sc = new Scanner (system.in);
  17. BigDecimal n = sc.nextbigdecimal ();

  18. System.out.print (n + "! =" + factorial (n));
  19. }

  20. }

The third method, implemented by recursion:

Java code

    1. 1.package factorial;
    2. 2.
    3. 3.import Java.util.Scanner;
    4. 4.import Java.math.BigDecimal;
    5. 5.public class Factorial2 {
    6. 6. public static BigDecimal factorial (BigDecimal N) {
    7. 7. BigDecimal bd1 = new BigDecimal (1);
    8. 8. if (N.equals (new BigDecimal (1))) {
    9. 9. return BD1;
    10. 10.}
    11. One. Else
    12. Return n.multiply (Factorial (n.subtract (BD1)));
    13. 13.}
    14. public static void Main (string[] arguments) {
    15. Scanner sc = new Scanner (system.in);
    16. BigDecimal a = Sc.nextbigdecimal ();
    17. BigDecimal result = factorial (a);
    18. System.out.println (A + "! =" +result);
    19. 19.
    20. 20.}
    21. 21st.
    22. 22.}

Three ways to implement factorial in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.