public class Jiecheng { public static void main(String args[]) { Factorial f = new Factorial(); int i = 100; System.out.println("factorial of " + i + " is: " + f.factorial(i)); } }class Factorial { int factorial(int i) { if (i == 0 || i == 1) return 1; else return factorial(i - 1) * i; } }
/** * 文件Test.java * */ public class Test { /** * 大数加法 */ public String add(String a,String b) { String big=a.length()>b.length()?a:b; String small=a.length()>b.length()?b:a; int dot=0; //进位 StringBuffer result=new StringBuffer(""); char[] cBig=((new StringBuffer(big)).reverse()).toString().toCharArray(); char[] cSmall=((new StringBuffer(small)).reverse()).toString().toCharArray();
/** * 主函数 */ public static void main(String[] args) { Test test=new Test(); System.out.println(test.factor("100"));
} }
你直接赋值给int型的肯定会超出int型所能表示的范围的,最后的结果肯定会出错的。
所以还是把int factorial(int i)改为double比较好
int[] res = new int[3000]; final int limit = 1000; res[1] = 1; int max_now = 1; for (int step = 1; step <= limit; step++) { int temp = 0; int now = max_now; int zero; for (zero = 1; zero <= now; zero++) { if (res[zero] != 0) break; } for (int carry = zero - 1; carry <= now; carry++) { res[carry] *= step; res[carry] += temp; temp = 0; if (res[carry] >= 10) { int carry_temp = carry; temp = res[carry]; if (carry_temp <= max_now) { res[carry_temp] = temp % 10; temp /= 10; carry_temp++; } if (carry_temp > max_now) { while (temp >= 10) { res[carry_temp] = temp % 10; temp /= 10; carry_temp++; } res[carry_temp] = temp; temp = 0; max_now = carry_temp; } } } } for (int j = max_now; j > 0; j--) { System.out.print(res[j]); }
TKS 剛學JAVA時寫的 沒經驗寫的不好
public class Factorial { /** * @param args */ public static void main(String[] args) { Factorial f = new Factorial(); BigInteger i = new BigInteger("100"); System.out.println("factorial of " + i + " is: " + f.factorial(i)); }
我的这个可以实现! import java.math.BigDecimal; import java.math.BigInteger; public class BigNumber { private BigNumber() { } /** * 提供精确的乘法运算。 * @param v1 被乘数 * @param v2 乘数 * @return 两个参数的积 */ public static double mul(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return (b1.multiply(b2)).doubleValue(); } /** * 计算Factorial阶乘! * @param n 任意大于等于0的int * @return n!的值 */ public static BigInteger getFactorial(int n) { if (n < 0) { System.err.println("n必须大于等于0!"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("0"); } //将数组换成字符串后构造BigInteger BigInteger result = new BigInteger("1"); for (; n > 0; n--) { //将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法 result = result.multiply(new BigInteger(new Integer(n).toString())); } return result; } public static void main(String[] args) { //计算阶乘,可以将n设得更大 int n = 100; System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n)); } }
import java.math.BigDecimal; import java.math.BigInteger; public class BigNumber { private BigNumber() { } /** * 提供精确的乘法运算。 * @param v1 被乘数 * @param v2 乘数 * @return 两个参数的积 */ public static double mul(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return (b1.multiply(b2)).doubleValue(); } /** * 计算Factorial阶乘! * @param n 任意大于等于0的int * @return n!的值 */ public static BigInteger getFactorial(int n) { if (n < 0) { System.err.println("n必须大于等于0!"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("0"); } //将数组换成字符串后构造BigInteger BigInteger result = new BigInteger("1"); for (; n > 0; n--) { //将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法 result = result.multiply(new BigInteger(new Integer(n).toString())); } return result; } public static void main(String[] args) { //计算阶乘,可以将n设得更大 int n = 100; System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n)); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger;public class TestJC { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一个整数:"); try { int a = Integer.parseInt(br.readLine()); System.out.println(a + "!=" + SuanFa(a)); } catch (NumberFormatException e) { System.out.println("您输入的数有误!"); } catch (IOException e) { System.out.println("您输入的数有误!"); } } public static BigInteger SuanFa(int b) { BigInteger sum = new BigInteger("1"); for (; b > 0; b--) { sum = sum.multiply(new BigInteger(new Integer(b).toString())); } return sum; }}
BigInteger对它很陌生,谁给介绍一下呀?
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub int[] res = new int[3000]; final int limit = 1000; res[1] = 1; int max_now = 1; for (int step = 1; step <= limit; step++) { int temp = 0; int now = max_now; int zero; for (zero = 1; zero <= now; zero++) { if (res[zero] != 0) break; } for (int carry = zero - 1; carry <= now; carry++) { res[carry] *= step; res[carry] += temp; temp = 0; if (res[carry] >= 10) { int carry_temp = carry; temp = res[carry]; if (carry_temp <= max_now) { res[carry_temp] = temp % 10; temp /= 10; carry_temp++; } if (carry_temp > max_now) { while (temp >= 10) { res[carry_temp] = temp % 10; temp /= 10; carry_temp++; } res[carry_temp] = temp; temp = 0; max_now = carry_temp; } } } } for (int j = max_now; j > 0; j--) { System.out.print(res[j]); } } copy过来就都乱了
去查下java.math.BigDecimal的API吧 就他可以实现了
import java.math.BigInteger;public class Test { public static void main(String[] args) { m(5); } public static void m(int n) { BigInteger sum = new BigInteger("1"); for(; n>0 ;n--) { sum = sum.multiply(new BigInteger("" + n)); } System.out.println(sum); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger;public class jieCheng { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入一个整数:"); try { int a = Integer.parseInt(br.readLine()); System.out.println(a + "!=" + SuanFa(a)); } catch (NumberFormatException e) { System.out.println("您输入的数有误!"); } catch (IOException e) { System.out.println("您输入的数有误!"); } } public static BigInteger SuanFa(int b) { BigInteger sum = new BigInteger("1"); for (; b > 0; b--) { sum = sum.multiply(new BigInteger(new Integer(b).toString())); } return sum; }} 这个是对的。。
/* 大数阶乘的java实现 */ import java.math.BigInteger; import java.util.*; public class Factorial { protected static ArrayList table=new ArrayList(); static { table.add(BigInteger.valueOf(1)); }
/** Creates a new instance of factorial */ public static synchronized BigInteger factorial(int x) { if(x<0) throw new IllegalArgumentException("x must be non-negative."); for(int size=table.size();size<=x;size++) { BigInteger lastfact=(BigInteger)table.get(size-1); BigInteger nextfact=lastfact.multiply(BigInteger.valueOf(size)); table.add(nextfact); } return (BigInteger) table.get(x); }
public static void main(String[] args) { System.out.println("100的阶乘:"+factorial(100)); System.out.println("1000的阶乘:"+factorial(1000));
{
public static void main(String args[])
{
Factorial f = new Factorial();
int i = 100;
System.out.println("factorial of " + i + " is: " + f.factorial(i));
}
}class Factorial
{
int factorial(int i)
{
if (i == 0 || i == 1)
return 1;
else
return factorial(i - 1) * i;
}
}
* 文件Test.java
*
*/
public class Test
{
/**
* 大数加法
*/
public String add(String a,String b)
{
String big=a.length()>b.length()?a:b;
String small=a.length()>b.length()?b:a;
int dot=0; //进位
StringBuffer result=new StringBuffer("");
char[] cBig=((new StringBuffer(big)).reverse()).toString().toCharArray();
char[] cSmall=((new StringBuffer(small)).reverse()).toString().toCharArray();
/*
* 计算最小的一个与大的一个小的部分的和
*/
for(int i=0;i<cSmall.length;i++)
{
int temp=Integer.parseInt(String.valueOf(cBig[i]))+Integer.parseInt(String.valueOf(cSmall[i]))+dot;
dot=temp/10;
result.append(temp%10);
}
/*
* 对大的一个进行计算
*/
for(int j=cSmall.length;j<cBig.length;j++)
{
int temp2=Integer.parseInt(String.valueOf(cBig[j]))+dot;
dot=temp2/10;
result.append(temp2);
}
/*
* 结果的位数超过最大的时候进行进位
*/
if(dot>0)
{
result.append(dot);
}
result=result.reverse();
return result.toString();
}
/**
* 大数乘法
*/
public String multiply(String a,String b)
{
char[] cA=((new StringBuffer(a)).reverse()).toString().toCharArray();
char[] cB=((new StringBuffer(b)).reverse()).toString().toCharArray();
String result="0";
for(int i=0;i<cA.length;i++)
{
StringBuffer tempSingle=new StringBuffer("");
/*
* 一个数的每一位数乘以另外一个数
*/
int dot=0; //进位
for(int j=0;j<cB.length;j++)
{
int temp=Integer.parseInt(String.valueOf(cA[i]))*Integer.parseInt(String.valueOf(cB[j]))+dot;
tempSingle.append(temp%10);
dot=temp/10;
}
/*
* 处理进位
*/
if(dot>0)
{
tempSingle.append(dot);
}
tempSingle=tempSingle.reverse();
for(int k=0;k<i;k++)
{
tempSingle.append("0");
}
/*
* 把每一个数乘以另一个数的结果调用加法方法相加起来
*/
result=add(result,tempSingle.toString());
}
return result;
}
/**
* 求n的阶乘
*/
public String factor(String a)
{
String n="1";
for(int i=1;i<=Integer.parseInt(a);i++)
{
n=multiply(n,String.valueOf(i));
}
return n;
}
/**
* 主函数
*/
public static void main(String[] args)
{
Test test=new Test();
System.out.println(test.factor("100"));
}
}
final int limit = 1000; res[1] = 1;
int max_now = 1; for (int step = 1; step <= limit; step++) {
int temp = 0;
int now = max_now;
int zero;
for (zero = 1; zero <= now; zero++)
{
if (res[zero] != 0)
break;
} for (int carry = zero - 1; carry <= now; carry++) {
res[carry] *= step;
res[carry] += temp;
temp = 0;
if (res[carry] >= 10) {
int carry_temp = carry;
temp = res[carry];
if (carry_temp <= max_now) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
if (carry_temp > max_now) {
while (temp >= 10) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
res[carry_temp] = temp;
temp = 0;
max_now = carry_temp;
}
}
} }
for (int j = max_now; j > 0; j--) {
System.out.print(res[j]);
}
剛學JAVA時寫的
沒經驗寫的不好
* @param args
*/
public static void main(String[] args) {
Factorial f = new Factorial();
BigInteger i = new BigInteger("100");
System.out.println("factorial of " + i + " is: " + f.factorial(i));
}
BigInteger factorial(BigInteger i){
if (i.compareTo(BigInteger.ONE)==0)
return BigInteger.ONE;
else
return factorial(i.subtract(BigInteger.ONE)).multiply(i);
}}
{
if (n < 0)
throw new IllegalArgumentException("n < 0");
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--)
{
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}
所以用int
剛才臨時把9改成100所以可能結果是負數或者報錯
把int型改下應該可以
初學時寫的代碼
應該容易看懂
只不過還是沒達到樓主要求
不是用數組做的
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigNumber {
private BigNumber() {
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return (b1.multiply(b2)).doubleValue();
}
/**
* 计算Factorial阶乘!
* @param n 任意大于等于0的int
* @return n!的值
*/
public static BigInteger getFactorial(int n) {
if (n < 0) {
System.err.println("n必须大于等于0!");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("0");
}
//将数组换成字符串后构造BigInteger
BigInteger result = new BigInteger("1");
for (; n > 0; n--) {
//将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
result = result.multiply(new BigInteger(new Integer(n).toString()));
}
return result;
} public static void main(String[] args) { //计算阶乘,可以将n设得更大
int n = 100;
System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
}
}
import java.math.BigInteger;
public class BigNumber {
private BigNumber() {
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return (b1.multiply(b2)).doubleValue();
}
/**
* 计算Factorial阶乘!
* @param n 任意大于等于0的int
* @return n!的值
*/
public static BigInteger getFactorial(int n) {
if (n < 0) {
System.err.println("n必须大于等于0!");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("0");
}
//将数组换成字符串后构造BigInteger
BigInteger result = new BigInteger("1");
for (; n > 0; n--) {
//将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
result = result.multiply(new BigInteger(new Integer(n).toString()));
}
return result;
} public static void main(String[] args) { //计算阶乘,可以将n设得更大
int n = 100;
System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;public class TestJC { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数:");
try {
int a = Integer.parseInt(br.readLine());
System.out.println(a + "!=" + SuanFa(a)); } catch (NumberFormatException e) {
System.out.println("您输入的数有误!");
} catch (IOException e) {
System.out.println("您输入的数有误!");
} } public static BigInteger SuanFa(int b) {
BigInteger sum = new BigInteger("1");
for (; b > 0; b--) {
sum = sum.multiply(new BigInteger(new Integer(b).toString()));
}
return sum;
}}
// TODO Auto-generated method stub
int[] res = new int[3000];
final int limit = 1000; res[1] = 1;
int max_now = 1; for (int step = 1; step <= limit; step++) {
int temp = 0;
int now = max_now;
int zero;
for (zero = 1; zero <= now; zero++)
{
if (res[zero] != 0)
break;
} for (int carry = zero - 1; carry <= now; carry++) {
res[carry] *= step;
res[carry] += temp;
temp = 0;
if (res[carry] >= 10) {
int carry_temp = carry;
temp = res[carry];
if (carry_temp <= max_now) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
if (carry_temp > max_now) {
while (temp >= 10) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
res[carry_temp] = temp;
temp = 0;
max_now = carry_temp;
}
}
} }
for (int j = max_now; j > 0; j--) {
System.out.print(res[j]);
}
}
copy过来就都乱了
import java.math.BigInteger;public class Test {
public static void main(String[] args) {
m(5);
} public static void m(int n) {
BigInteger sum = new BigInteger("1");
for(; n>0 ;n--) {
sum = sum.multiply(new BigInteger("" + n));
}
System.out.println(sum);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;public class jieCheng { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数:");
try {
int a = Integer.parseInt(br.readLine());
System.out.println(a + "!=" + SuanFa(a)); } catch (NumberFormatException e) {
System.out.println("您输入的数有误!");
} catch (IOException e) {
System.out.println("您输入的数有误!");
} } public static BigInteger SuanFa(int b) {
BigInteger sum = new BigInteger("1");
for (; b > 0; b--) {
sum = sum.multiply(new BigInteger(new Integer(b).toString()));
}
return sum;
}}
这个是对的。。
大数阶乘的java实现 */
import java.math.BigInteger;
import java.util.*;
public class Factorial
{
protected static ArrayList table=new ArrayList();
static
{
table.add(BigInteger.valueOf(1));
}
/** Creates a new instance of factorial */
public static synchronized BigInteger factorial(int x)
{
if(x<0) throw new IllegalArgumentException("x must be non-negative.");
for(int size=table.size();size<=x;size++)
{
BigInteger lastfact=(BigInteger)table.get(size-1);
BigInteger nextfact=lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}
public static void main(String[] args)
{ System.out.println("100的阶乘:"+factorial(100));
System.out.println("1000的阶乘:"+factorial(1000));
}
}
PS:不是本人自己写的,转载网上朋友的!效率很高的,希望对楼主有所帮助。