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 7409829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:08:29+00:00 2026-05-29T06:08:29+00:00

I am trying to implement Factorial function via divide and conquer strategy. I used

  • 0

I am trying to implement Factorial function via divide and conquer strategy. I used the ForkJoin framework to fork each recursive task to speed up the computation.
But I found that its not speeding up as i expected.
It took 28 seconds to calculate the factorial of 50000 without using the ForkJoin, while
it took 25 seconds when I used ForkJoin.
this is the code without forkjoin:

public static BigInteger factorial(long p, long q) {
    if (q < p) {
        return new BigInteger("1");
    }
    if (p == q) {
        return new BigInteger("" + p);
    }
    BigInteger fact = new BigInteger("1");
    fact = fact.multiply(factorial(p, (p + q) / 2)).multiply(factorial((p + q) / 2 + 1, q));
    return fact;
}

And this is the code with forkJoin:

public class Factorial extends RecursiveTask<BigInteger>{
private long p, q;
public Factorial(long p, long q) {
    this.p = p;
    this.q = q;
}
@Override
public BigInteger compute() {
    if(q < p) {
        return new BigInteger("1");
    } 
    if( p == q) {
        return new BigInteger(""+p);
    }
    Factorial f1 = new Factorial(p, (p+q)/2);
    Factorial f2 = new Factorial((p+q)/2 + 1, q);
    f2.fork();
    return f1.compute().multiply(f2.join());        
}    
}

Where am I going wrong? I don’t think this would be the result of Fork/Join. Please Help!

  • 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-29T06:08:30+00:00Added an answer on May 29, 2026 at 6:08 am

    Fork/Join can paralellize computing. It is: gives you a constant gain (divide the time by 4 by example). And that’s bounded by the real CPUs you have (200 threads will only share the same 4 CPUs by example).

    In contrast factorial (the typical algorithm) is O(N!) what it means it grows very very very fast.

    And if you create a new fork for each step the overhead of forking and joining compensates the gain from parallelizing.

    So the important thing is trying to calculate factorial with another algorith that’s not O(N!). If you apply dynamic programming (resusing intermediate results), you can convert it into O(N).

    I don’t know the algorithm you are trying to imitate by what I should do is mantaining a matrix or something with calculations for pairs in order to reuse them when I need them the second time…


    Looking at your code I can see each factorial method execution provokes two child executions: (p+q)/2,q and p,(p+q)/2+1… or something like that. If each time factorial method finds a result, saves it in a map [Pair -> BigDecimal], you can query this cache at the beginning of the method. Make this map a member of your class (or pass it through arguments) so different method invocations share the map.

    factorial(p,q) {
       if (there is result for (p,q) in the map)
          return it
       else
       {
          normal computation (provokes two child invocations)
          save result into cache
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined
Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement, in Scala, the following Haskell function (from Learn You a Haskell...)
Trying to implement a function in my linkedlist class that will return total amount
Trying to implement a function that will return a list of ints the represent
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Trying to implement a rating system of users and postings. What is the best
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
Am trying to implement a generic way for reading sections from a config file.

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.