English 中文(简体)
在实施有吸引力的Fibonacci时,大量超量流入
原标题:Silent long overflow while implementing Fibonacci with Callables

我正尝试使用可打电话的器具执行Fibonacci序列,并用3,4,5,6和2000年,推出我的菲博森元的初步价值。 我获得的产出如下:

3 5 8 13 -820905900187520670

问题在于我试图用我可以说的话计算菲比(2000年)。 有些人可以看一下我下面提出的法典,看看我对我的做法有误:

import java.util.concurrent.*;
import java.util.*;

class FibonacciGen implements Callable<Long>{
    private Long fib;
    public FibonacciGen(long num){
        this.fib = num;
    }
    public Long call(){
        return calculateFibonacci(fib);
    }

    private long calculateFibonacci(long someNum){
        long firstNum = 0L;
        long secondNum = 1L;
        long counter = 0L;
        while(counter<someNum){
            long fibCalc = secondNum+firstNum;
            firstNum = secondNum;
            secondNum = fibCalc;
            counter= counter+1L;
        }
        return secondNum;
    }   

}

public class FibonacciCallable{
    public static void main(String[] args){
        ExecutorService exec = Executors.newCachedThreadPool();
        ArrayList<Callable<Long>> results = new ArrayList<Callable<Long>>();
        CompletionService<Long> ecs = new ExecutorCompletionService<Long>(exec);
        results.add(new FibonacciGen(3L));
        results.add(new FibonacciGen(4L));
        results.add(new FibonacciGen(5L));
        results.add(new FibonacciGen(6L));
        results.add(new FibonacciGen(2000L));
            try{
                for(Callable<Long> fs:results){
                    ecs.submit(fs);
                }
                System.out.println("Submitted all the tasks");
                int n = results.size();
                for(int i=0;i<n;++i){
                    System.out.println("Taking the first completed task");
                    Long r = ecs.take().get();
                    if(r != null)
                        System.out.println(r);
                    }   


            }
            catch(InterruptedException ex){System.out.println(ex);return;}
            catch(ExecutionException e){System.out.println(e);}
            finally{exec.shutdown();}
        }
}

成就

最佳回答

Java不例外,只是总结价值,因此,你重新取得奇怪的结果。 Fibonacci是一个快速增长的顺序,2000年。 内容超过<代码> 长期

采用<代码>BigInteger,将使你具有任意的准确性(显然以业绩成本计算)。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签