1*2*3*4...10000=?
1*2*3*4...10000=?编程怎么实现? --------------------编程问答-------------------- 你确定是从1连乘到10000吗?不是加? --------------------编程问答-------------------- 楼主看看这个:http://blog.sina.com.cn/s/blog_5e2497580100fuva.html --------------------编程问答-------------------- 就是一个递归嘛,
--------------------编程问答-------------------- http://www.java3z.com/cwbwebhome/article/article5/51321.html?id=3972 --------------------编程问答--------------------
public static int recursionMultiply(int x){
if(x != 1){
return x * recursionMultiply(x - 1);
}
return x;
}
++
lz可以试试这个 --------------------编程问答-------------------- 你们几个发网址的,想晕死楼主呀? --------------------编程问答-------------------- 递归请看3楼,我贴个简单的for循环:
int length = 6; // 要连乘到几
int result = 1; // 初始值
for (int i = 2; i < length+1; i++) {
result*=i; // 最终结果
}
这里跟上注释和输出语句。
--------------------编程问答--------------------
public static void main(String[] args) {
int length = 6; // 要连乘到几
int result = 1; // 初始值
String out = "1*"; // 输出语句
for (int i = 2; i < length + 1; i++) {
out += i + "*";
result *= i;
}
out = out.substring(0, out.length() - 1); // 去掉最后一个*号
System.out.println(out + "=" + result);
}
BigInteger,不错。 --------------------编程问答--------------------
public static void main(String[] args) {--------------------编程问答-------------------- 谢谢大家! --------------------编程问答-------------------- 刚刚有点小问题改了下
BigInteger result = BigInteger.valueOf(1);
for(int i = 1; i <= 10000; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
int length = result.toString().length();
for(int i = 0; i < length / 1000 + 1; i++) {
System.out.println(result.toString().substring(i * 1000, (i + 1) * 1000));
}
}
public static void main(String[] args) {--------------------编程问答-------------------- 这就是个递归的问题:
BigInteger result = BigInteger.valueOf(1);
for(int i = 1; i <= 10000; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
int length = result.toString().length();
int i = 0;
for(;i < length / 1000; i++) {
System.out.println(result.toString().substring(i * 1000, (i + 1) * 1000));
}
System.out.println(result.toString().substring(i * 1000));
}
public int recursion(int n) {
if(n != 1) {
return n * recursion(n - 1);
}
return n;
} --------------------编程问答-------------------- int肯定不够用的啊 呵呵
--------------------编程问答--------------------
public static BigInteger multiplyBigNumber(){
BigInteger sum = new BigInteger("1");
System.out.println(sum);
for(int i = 1; i<10;i ++){
sum = sum.multiply(new BigInteger("" + i));
System.out.println(sum);
}
return sum;
}
但是好像BigInteger也存放不了,1400以内还可以,1500就不行了,并且不建议使用递归到一定次数就导致堆栈溢出程序挂了。 --------------------编程问答-------------------- 用int不行, 得用long --------------------编程问答-------------------- 小心溢出。。。 --------------------编程问答-------------------- 这是阶乘啊,10000 的阶乘有 35660 位数字组成!
使用斯特林公式计算:10000! 约为 2.8462359621833295 * 10^35659 --------------------编程问答--------------------
public class Stirling {--------------------编程问答-------------------- 如果想知道这 35660 是啥数字,可以看看下面的代码,这代码是经过精心优化过的,算法描述在代码后面:
public static void main(String[] args) {
stirling(10000);
}
/**
* Stirling:
* n! = sqrt(2 * pi * n) * pow(n / e, n)
* @param n
*/
private static void stirling(int n) {
double a = n * Math.log10(n / Math.E);
double s = 0.5 * Math.log10(2 * Math.PI * n);
double base10 = a + s;
int exponent = (int)base10;
double base = Math.pow(10, base10 - exponent);
System.out.println(n + "! = " + base + " * 10^" + exponent);
}
}
import java.math.BigInteger;
public class Factorial {
public static void main(String args[]) {
BigInteger bi = factorial(10000);
String str = bi.toString();
output(str, 100);
}
public static BigInteger factorial(int n) {
if(n < 2) {
return BigInteger.ONE;
}
int[] oddCount = new int[Integer.numberOfTrailingZeros(Integer.highestOneBit(n))];
int shift = init(oddCount, n);
BigInteger result = BigInteger.ONE;
BigInteger bg = BigInteger.ONE;
BigInteger tmp = BigInteger.ONE;
int max = oddCount[oddCount.length - 1];
int offset = (oddCount[0] + 1) & 1;
int oddStart = 1;
while(oddStart <= max) {
tmp = tmp.multiply(new BigInteger(String.valueOf(2 * oddStart + 1)));
if(oddCount[offset] == oddStart) {
offset++;
bg = bg.multiply(tmp);
tmp = BigInteger.ONE;
result = result.multiply(bg);
}
oddStart++;
}
return result.shiftLeft(shift);
}
private static int init(int[] oddCount, int n) {
int s = n;
int r = 0;
int k = oddCount.length;
while(k > 0) {
s >>= 1;
oddCount[--k] = n - s - 1;
n = s;
r += s;
}
return r;
}
private static void output(String str, int lineLength) {
char[] chs = str.toCharArray();
int offset = 0;
while (offset < chs.length) {
int max = Math.min(chs.length - offset, lineLength);
System.out.println(new String(chs, offset, max));
offset += max;
}
}
}
--------------------编程问答-------------------- 大数阶乘不能这样,得是数租,计算机没有那么大的 数来用算
补充:Java , Java相关