求999的阶乘!!!!
解决方案 »
- JBOSS上部署项目就报这个错误。求解决办法
- 如何解决GBK转换为UTF8中出现的偶数字符可以,奇数字符乱码的问题
- renameto 函数总是不成功,有人遇到过么?
- 新手问:难道构造函数就不能用void修饰吗?
- 哥哥姐姐们:能给俺推荐几本适合初学者学习java的好书么?
- 请问使用java语言作为载体的经典算法书籍有哪些?注意是算法书籍哦
- 大家见过这种错误吗??反正我是第一次见!!
- jPasswordField 用getpassword().toString 问题 为什么它每次拿出来的字符不一样啊???
- 请教java高手
- 关于正则表达式的概念性问题。。。。。头疼死了,,求大神详解
- 令人慧解的信息技术大赛的java试题2,求详细解答!
- 这有几个选择题,大家帮我看下 在线等
protected static ArrayList<BigInteger> list = new ArrayList<BigInteger>();
static {
list.add(BigInteger.valueOf(1));
}
public static BigInteger factorial(int x) {
for (int size = list.size(); size <= x; size++) {
BigInteger lastfact = (BigInteger) list.get(size - 1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
list.add(nextfact);
}
return (BigInteger)list.get(x);
} public static void main(String[] args) { System.out.println(999 + "!=" + factorial(6));
}
换成999,我测试了下OK的
* Default thread count(CPU count * 4)
*/
static final int THREAD_COUNT;
static {
THREAD_COUNT = Runtime.getRuntime().availableProcessors() << 2;
} /**
* Never instantiate this class.
*/
private Factorial() {
} /**
* Calculates the factorial of n. A new thread pool will be initialized to
* perform the calculation task, and destroyed thereafter.
*
* @param n
* the value of n
* @return the factorial of the given n
* @throws Exception
* if exception occurs
*/
public static final BigInteger factorial(final int n) throws Exception {
ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
try {
return factorial(n, threadPool, THREAD_COUNT);
} finally {
threadPool.shutdown();
}
} /**
* Calculates the factorial of n. A specified thread pool will be used to
* perform the calculation task, and will not be automatically destroyed.
*
* @param n
* the value of n
* @param threadPool
* the given thread pool to perform the calculation task
* @param threadCount
* the number of threads to perform the calculation task
* @return the factorial of the given n
* @throws Exception
* if exception occurs
*/
public static BigInteger factorial(final int n, ExecutorService threadPool, final int threadCount) throws Exception { List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(threadCount);
for (int i = 1; i <= threadCount; i++) {
final int threadNo = i;
Callable<BigInteger> worker = new Callable<BigInteger>() { @Override
public BigInteger call() throws Exception {
BigInteger result = BigInteger.ONE;
for (int x = threadNo; x <= n; x += threadCount) {
result = BigInteger.valueOf(x).multiply(result);
}
return result;
} };
tasks.add(worker);
} BigInteger result = BigInteger.ONE;
List<Future<BigInteger>> futureList = threadPool.invokeAll(tasks);
for (Future<BigInteger> future : futureList) {
result = future.get().multiply(result);
}
return result;
}
}402387260077.... 耗时15ms这段代码算999!可能大材小用了,不过,如果计算99999!就体现出优势了。在我的电脑上99999!在20秒左右。分线程计算的原因,在以前一个C#讨论99999!的帖子里,我回复过。可以搜一下。
for(int i = 1; i<1000; i++ ){
multiply*i
}
return multiply;
15ms不是15s,9999的时候,我快半秒,99999的时候,比你快40多秒。
BigInteger bi = factorial(999);
System.out.println(bi);
}
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);
}
}算 99999! 的话需要 15~16s,但是要输出结果就很耗时。
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;
}
在性能和稳定性上,值得借鉴。
学习了。
他只贴了方法而没有写main函数,你自己写个main函数测试一下就可以了
速度没有火龙果的快, 但是写起来简单..
package com.lzw;import java.math.BigInteger;public class Factorial
{
private static BigInteger temp = BigInteger.ONE;
private static BigInteger count = BigInteger.ONE;
public static BigInteger factorial(int num)
{
for (int i = 1; i <= num; i++)
{
temp = temp.multiply(count);
count = count.add(BigInteger.ONE);
}
return null;
}
public static void main(String[] args)
{
long start = System.currentTimeMillis();
BigInteger bi = factorial(99999);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
可以实现你要的
private int JC(int Number)
(
if(number==1)
{
return 1;
}
return Number * JC(Number-1);
)
而且递归的话次数高会出现栈溢出……
问个低级问题,这里的return 1;执行 之后,为什么return Number * JC(Number-1);还执行
不是直接return回去?
#include<stdlib.h>
#include<string.h>
int main()
{
int buffer[500];
memset(buffer,0,sizeof(buffer));
int p ;
printf("输入整数:");
scanf("%d",&p);
bool flag = false;
bool flag1[500]={false};
int add = 0;
memcpy(buffer+499,&p,sizeof(p));
for(int m=p-1;m>=1;--m)
{
for(int i=sizeof(buffer)/sizeof(int)-1;i>=0;--i)
{
if(flag)
{
buffer[i]= buffer[i]*m+add;
flag = false ;
add = 0;
}
else
{
buffer[i]= buffer[i]*m ;
}
if(buffer[i]>9)
{
add = buffer[i]/10;
buffer[i] = buffer[i]%10;
if(buffer[i]==0)
{
flag1[i] = true ;
}
flag = true ;
}
}
}
printf("%d!的阶乘为",p);
for(int j=0;j<=sizeof(buffer)/sizeof(int)-1;j++)
{
if(flag1[j]||buffer[j]!=0)
{
printf("%d",buffer[j]);
}
}
printf("\n");
return 0;
}
(if (= n 1)
1
(* n(f (- n 1)))))