Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine(Program.Calculate(5, 1));
  4. }
  5. public static decimal Calculate(int x, byte taskNumber)
  6. {
  7. var tasks = new List<Task<decimal>>();
  8.  
  9. for (int i = 0; i < x; i += (x / taskNumber))
  10. {
  11. int step = i;
  12. tasks.Add(Task.Run(() =>
  13. {
  14. int right = (step + x / taskNumber) > x ? x : (step + x / taskNumber);
  15. return ChunkE(step + 1, right);
  16. }));
  17. }
  18.  
  19. Task.WaitAll(tasks.ToArray());
  20.  
  21. return tasks.Select(t => t.Result).Aggregate(((i, next) => i + next));
  22. }
  23.  
  24. public static decimal ChunkFactorial(int left, int right)
  25. {
  26. //Console.WriteLine("ChunkFactorial Thread ID :" + Thread.CurrentThread.ManagedThreadId);
  27. if (left == right)
  28. {
  29. return left == 0 ? 1 : left;
  30. }
  31. else
  32. {
  33. return right * ChunkFactorial(left, right - 1);
  34. }
  35. }
  36.  
  37. public static decimal ChunkE(int left, int right)
  38. {
  39. if(left == right)
  40. {
  41. return left == 0 ? 1 : left;
  42. }
  43. else
  44. {
  45. return ((3 * right) * (3 * right) + 1) / ChunkFactorial(left, right) + ChunkE(left, right - 1);
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement