Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.WriteLine(Program.Calculate(5, 1));
- }
- public static decimal Calculate(int x, byte taskNumber)
- {
- var tasks = new List<Task<decimal>>();
- for (int i = 0; i < x; i += (x / taskNumber))
- {
- int step = i;
- tasks.Add(Task.Run(() =>
- {
- int right = (step + x / taskNumber) > x ? x : (step + x / taskNumber);
- return ChunkE(step + 1, right);
- }));
- }
- Task.WaitAll(tasks.ToArray());
- return tasks.Select(t => t.Result).Aggregate(((i, next) => i + next));
- }
- public static decimal ChunkFactorial(int left, int right)
- {
- //Console.WriteLine("ChunkFactorial Thread ID :" + Thread.CurrentThread.ManagedThreadId);
- if (left == right)
- {
- return left == 0 ? 1 : left;
- }
- else
- {
- return right * ChunkFactorial(left, right - 1);
- }
- }
- public static decimal ChunkE(int left, int right)
- {
- if(left == right)
- {
- return left == 0 ? 1 : left;
- }
- else
- {
- return ((3 * right) * (3 * right) + 1) / ChunkFactorial(left, right) + ChunkE(left, right - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement