Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:23:33+00:00 2026-06-02T09:23:33+00:00

I am trying to implement the Fibonacci sequence using callables and seeded the initial

  • 0

I am trying to implement the Fibonacci sequence using callables and seeded the initial values of my Fibonacci callable with 3,4,5,6 and 2000. The output I get is as follows:

3 5 8 13
-820905900187520670

The problem is when I am trying to calculate fib(2000) in my callable. Can somebody take a look at my code provided below to see where I am going wrong with my approach:

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

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:23:36+00:00Added an answer on June 2, 2026 at 9:23 am

    Java doesn’t throw an exception on overflow, just wraps the value around, that’s why you’re getting the strange result. Fibonacci is a fast growing sequence, the 2000. element is way beyond long

    Try using BigInteger, it will give you arbitrary precision (at the cost of performance obviously).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to implement the active record pattern using Java/JDBC and MySQL along with
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine
I'm trying to implement a recursive Fibonacci series which returns the value at an
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
Trying to implement AVAudioplayer and get some metering data of the played music, but
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
I'm trying to implement a view tracking web service on my website. I'm using
I am trying implement the Data transformation using Reflection 1 example in my code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.