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

  • Home
  • SEARCH
  • 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 6643719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:04:59+00:00 2026-05-26T00:04:59+00:00

import java.math.BigInteger; import java.util.concurrent.*; public class MultiThreadedFib { private ExecutorService executorService; public MultiThreadedFib(final int

  • 0
import java.math.BigInteger;
import java.util.concurrent.*;

public class MultiThreadedFib {

private ExecutorService executorService;

public MultiThreadedFib(final int numberOfThreads) {
  executorService = Executors.newFixedThreadPool(numberOfThreads);
}

public BigInteger getFibNumberAtIndex(final int index) 
  throws InterruptedException, ExecutionException {

  Future<BigInteger> indexMinusOne = executorService.submit(
    new Callable<BigInteger>() {
      public BigInteger call() 
      throws InterruptedException, ExecutionException {
        return getNumber(index - 1);
      }
  });

  Future<BigInteger> indexMinusTwo = executorService.submit(
    new Callable<BigInteger>() {
      public BigInteger call() 
      throws InterruptedException, ExecutionException {
        return getNumber(index - 2);
      }
  });

  return indexMinusOne.get().add(indexMinusTwo.get());
}

public BigInteger getNumber(final int index) 
throws InterruptedException, ExecutionException {
  if (index == 0 || index == 1)
    return BigInteger.valueOf(index);

  return getFibNumberAtIndex(index - 1).add(getFibNumberAtIndex(index - 2));
  }
}

im gonig to calculate fibonacci sequence with java threads in order to decrease calculating time but answer is wrong Although its seem to be true.
another problem is out of memory exception that occurred in starting new threads for number 35 to the top.
please help me
many regards…

  • 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-05-26T00:05:00+00:00Added an answer on May 26, 2026 at 12:05 am

    You need to limit the number of threads. I would recommend that you only use two threads: one for finding index – 1, the other for finding index – 2. When each of these methods recursively find the previous index – 1 and index – 2, do this in the existing thread.

    public BigInteger getNumber(final int index) 
        throws InterruptedException, ExecutionException {
       if (index == 0 || index == 1)
         return BigInteger.valueOf(index + 1);
    
       return getNumber(index - 1).add(getNumber(index - 2));
    }
    

    Finally, I believe the first two Fibonacci numbers (the root numbers) are 1 & 2, not 0 & 1. Notice that I changed getNumber to return index + 1 and recursively call itself rather than going back to the service.

    1 + 2 = 3 + 2 = 5 …

    0 + 1 = 1 (error)

    Another problem this alg has is that is does a lot of work twice because it recalculates values. For example if you are looking for the

    F5 = F4 + F3

    Thread 1: F4 = F3 + F2

    Thread 2: F3 = F2 + F1

    Notice that you calculate F3 twice. In fact you are calculating just about each number twice.

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

Sidebar

Related Questions

I have a problem import java.math.BigInteger; import java.io.*; import java.util.*; import java.lang.*; public class
import java.util.Scanner; public class Test { private static int decimalNum = 0; private static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
import java.util.*; import org.directwebremoting.util.Logger; public class People { public People() { people = new
package ewa; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.logging.Level; import java.util.logging.Logger; import java.math.BigInteger;
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
This is the code : import java.io.*; class tester { public static void main(String
Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman *
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
This is the code : import java.awt.*; import javax.swing.*; class tester { JFrame fr;

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.