9 (A)
9 (A)
DATE:
AIM:
To implement a *multi-threaded function* that calculates the *Fibonacci series* up to the *n-
th term* using *multiple threads*, where each thread computes a portion of the series
iteratively and merges the results.
BACKGROUND THEORY:
Multi-threading is a technique that allows multiple computations to be performed
concurrently, utilizing CPU cores efficiently. Fibonacci sequence follows the recurrence
relation:
F(n) = F(n-1) + F(n-2)
ALGORITHM:
1. Read an integer n from the user, where 1 ≤ n ≤ 10⁶.
- fib[0] = 0
5. Divide the Fibonacci calculation across threads by assigning chunks of indices to different
threads.
6. Start each thread, where each computes Fibonacci numbers for its assigned range.
import java.util.Scanner;
this.fibArray = fibArray;
this.start = start;
this.end = end;
scanner.close();
if (n < 1) {
System.out.println("Invalid input");
return;
fibArray[0] = 0;
if (n > 1) {
fibArray[1] = 1;
int start = 2;
threads[i].start();
start = end;
}
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}]
System.out.println(num);
}
OUTPUT:
RESULT:
Thus, the multi-threaded Java program efficiently finds all prime numbers in the given range
by distributing the task across multiple threads.