using System.Threading.Tasks;
using System.Diagnostics;
static int[] numbers = { 3, 4, 5, 1, 3, 4, 5, 1, 3, 4, 5, 1, 3, 4, 5, 1, 3, 4 };
public static int f(int n) {
while (n > 1) result *= n--;
public static int GetSumNormal() {
foreach (int n in numbers) sum += f(n);
public static int GetSumThreaded(int threadCount) {
var tasks = new Task[threadCount];
for (int i = 0; i < threadCount; i++)
tasks[i] = Task.Run(() => {
while (_t_index < numbers.Length) {
int index = Interlocked.Increment(ref _t_index) - 1;
Interlocked.Add(ref _t_sum, f(numbers[index]));
public static void Main()
Stopwatch stopwatch = Stopwatch.StartNew();
int sum = GetSumNormal();
Console.WriteLine($"Normal sum = {sum}, time = {stopwatch.Elapsed} ns");
Console.WriteLine($"Threaded sum = {sum}, time = {stopwatch.Elapsed} ns");