JAVA求100的阶乘

JAVA求100的阶乘2009年08月19日 星期三 11:17package sbjava;

import java.math.BigInteger; //大数字的运算需要这个类
import java.util.Scanner; //输入类

public class Factorial {

/**
* @param args
*/
public static void main(String[] args) {
   // TODO 自动生成方法存根
   System.out.println("请输入您要计算的阶乘数(输入值需为自然数):");
   Scanner sc = new Scanner(System.in); // 创建输入类的格式之一,请熟记
   int k = sc.nextInt();
   if (k == 0) {
    System.out.println("0的阶乘被特别地定义为1");
   } else {
    String str = new String(""); // 建立空字符串用于保留转换得来的数字
    String str1 = new String("");
    String str2 = new String("");
    String str3 = new String("");
    BigInteger product = new BigInteger("1"); // 建立初始大数字,并在此基础上累乘
    BigInteger product1 = new BigInteger("1");
    long starTime = System.currentTimeMillis(); // 无聊而建的代码No.1,看看用大数字计算阶乘花费的时间
    int i;
    for (i = 1; i <= k; i++) { // i的最大值即表示这个数的阶乘
     str = String.format("%d", i); // 将整数i格式化为字符串"i"
     BigInteger big = new BigInteger(str); // 每次循环都会重新将该次的需要运算的大数字赋值
     product = product.multiply(big); // 自乘运算

    }
    for (int j = i - 1; j >= 1; j -= 2) { // j的最大值即表示这个数的双阶乘
     str2 = String.format("%d", j); // 将整数j格式化为字符串"j"
     BigInteger big1 = new BigInteger(str2); // 每次循环都会重新将该次的需要运算的大数字赋值
     product1 = product1.multiply(big1); // 自乘运算

    }
    long endTime = System.currentTimeMillis();
    long Time = endTime - starTime;
    str1 = String.format("%d", product);
    str3 = String.format("%d", product1);
    System.out.println((i - 1) + "的阶乘值为:" + str1);
    System.out.println((i - 1) + "的双阶乘值为:" + str3);
    System.out.println('\n');
    int size;
    System.out.println("该阶乘共有" + (size = str1.length()) + "位"); // 无聊而建的代码No.2,用于查看该阶乘有多少位
    System.out.println("该双阶乘共有" + (size = str3.length()) + "位");
    System.out.println('\n');
    System.out.println("本次计算所用总时间为:" + Time + "毫秒"); // 此次计算所用时间
   }
}

##################################################################################################################################################################################################################
使用BigInteger大容量运算类计算100的阶乘
使用BigInteger大容量运算类计算100的阶乘

一.一般算法(循环)

view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        int result = 1;  
        for (int i = 1; i <= 100; i++) {  
            result *= i;  
        }  
        System.out.println(result);  
    }  

public class Test {
public static void main(String[] args) {
  int result = 1;
  for (int i = 1; i <= 100; i++) {
   result *= i;
  }
  System.out.println(result);
}
}

输出结果为0,因为int无法保存下100的阶乘的结果,100的阶乘的长度至少大于50位,也要大于long,double

二.使用BigInteger大容量运算类

view plaincopy to clipboardprint?
import java.math.BigInteger;  
 
public class Test {  
    public static void main(String[] args) {  
        BigInteger result = new BigInteger("1");//为result赋初始值,为1  
        for (int i = 1; i <= 100; i++) {  
            BigInteger num = new BigInteger(String.valueOf(i));  
            result = result.multiply(num);//调用自乘方法  
        }  
        System.out.println(result);//输出结果  
        System.out.println(String.valueOf(result).length());//输出长度  
    }  

import java.math.BigInteger;

public class Test {
public static void main(String[] args) {
  BigInteger result = new BigInteger("1");//为result赋初始值,为1
  for (int i = 1; i <= 100; i++) {
   BigInteger num = new BigInteger(String.valueOf(i));
   result = result.multiply(num);//调用自乘方法
  }
  System.out.println(result);//输出结果
  System.out.println(String.valueOf(result).length());//输出长度
}
}

计算结果为:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
产度:158

本篇文章来源于:开发学院 http://edu.codepub.com   原文链接:http://edu.codepub.com/2009/1114/17647.php

你可能感兴趣的:(java,算法,PHP,J#)