// General parallel solution, using void Tasks with shared variable private static long countParallelN1(int range, int taskCount) { int perTask = range / taskCount; LongCounter lc = new LongCounter(); Parallel.For(0, taskCount, t => { int from = perTask * t, to = (t + 1 == taskCount) ? range : perTask * (t + 1); for (int i = from; i < to; i++) { if (isPrime(i)) { lc.increment(); } } }); return(lc.get()); }
// General parallel solution, using void Tasks with shared variable private static long countParallelN1(int range, int taskCount) { int perTask = range / taskCount; LongCounter lc = new LongCounter(); Parallel.For(0, taskCount, t => { int from = perTask * t, to = (t+1 == taskCount) ? range : perTask * (t+1); for (int i=from; i<to; i++) if (isPrime(i)) lc.increment(); }); return lc.get(); }