1*2*3*4...10000=?

xinwd1 2012-04-19 04:40:45
1*2*3*4...10000=?编程怎么实现?
...全文
319 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
崔叫兽 2012-04-21
  • 打赏
  • 举报
回复
大数阶乘不能这样,得是数租,计算机没有那么大的 数来用算
  • 打赏
  • 举报
回复
如果想知道这 35660 是啥数字,可以看看下面的代码,这代码是经过精心优化过的,算法描述在代码后面:

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;
}
}
}



  • 打赏
  • 举报
回复
public class Stirling {

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);
}
}
  • 打赏
  • 举报
回复
这是阶乘啊,10000 的阶乘有 35660 位数字组成!

使用斯特林公式计算:10000! 约为 2.8462359621833295 * 10^35659
sdojqy1122 2012-04-20
  • 打赏
  • 举报
回复
小心溢出。。。
X497347200 2012-04-20
  • 打赏
  • 举报
回复
用int不行, 得用long
sffx123 2012-04-20
  • 打赏
  • 举报
回复

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就不行了,并且不建议使用递归到一定次数就导致堆栈溢出程序挂了。
she383 2012-04-20
  • 打赏
  • 举报
回复
int肯定不够用的啊 呵呵
漠北 2012-04-20
  • 打赏
  • 举报
回复
这就是个递归的问题:

public int recursion(int n) {
if(n != 1) {

return n * recursion(n - 1);
}
return n;
}
VanBaston 2012-04-19
  • 打赏
  • 举报
回复
刚刚有点小问题改了下
	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));
}
xinwd1 2012-04-19
  • 打赏
  • 举报
回复
谢谢大家!
VanBaston 2012-04-19
  • 打赏
  • 举报
回复
	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));
}
}
fw347969680 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

楼主看看这个:

http://blog.sina.com.cn/s/blog_5e2497580100fuva.html
[/Quote]

BigInteger,不错。
himi_ 2012-04-19
  • 打赏
  • 举报
回复
递归请看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);
}
himi_ 2012-04-19
  • 打赏
  • 举报
回复
你们几个发网址的,想晕死楼主呀?
老9 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
楼主看看这个:

http://blog.sina.com.cn/s/blog_5e2497580100fuva.html
[/Quote]

++
lz可以试试这个
五哥 2012-04-19
  • 打赏
  • 举报
回复
http://www.java3z.com/cwbwebhome/article/article5/51321.html?id=3972
安特矮油 2012-04-19
  • 打赏
  • 举报
回复
就是一个递归嘛,

public static int recursionMultiply(int x){
if(x != 1){
return x * recursionMultiply(x - 1);
}
return x;
}
五哥 2012-04-19
  • 打赏
  • 举报
回复
楼主看看这个:

http://blog.sina.com.cn/s/blog_5e2497580100fuva.html
himi_ 2012-04-19
  • 打赏
  • 举报
回复
你确定是从1连乘到10000吗?不是加?

51,402

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧