Gustavo Rotondo
Gustavo Rotondo

Reputation: 59

Euler number with threads

I have two classes to calculate Euler numbers.

The formula to calculate Euler numbers is a sum of 1/n!. n = 0 to infinity

This class calculates the factorial

public class E 
{
    public static double factorial(double number)
    {
          double m = number;
          if (number == 0 || number == 1)
          {
               return 1;
          } 
          else
          {
              return m*factorial(m-1);
          }
    }

}

And this class calculates the Euler number

public class Teste 
{
    static double e = 0;
    public static void euller (int n)
    {
        for (int i = 0; i <= n; i++)
        {
            double j = (double)(E.factorial(i));
            e += (1 / j);
        }
    }
    public static void main(String[] args)
    {
        euller (100); // euller of numbr 100
        System.out.println (e);  
    }
}

I'd like to use threads. Each thread should calculate the factorial of a number.

For example, n = 10, threads number = 5

thread 1, calculate factorial 0 1, thread 2 calculate 2 3 ...

How should I include threads in this code?

Upvotes: 0

Views: 533

Answers (1)

Adi
Adi

Reputation: 2094

Create a pool thread, in your case with 5 threads and just execute them when you call factorial(i). there isn't much more then that.

Upvotes: 1

Related Questions