using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace B3CC_Pract1
{
class Program
{
public static int threadcounter;
public static int oplossingen;
public static int modulus;
public static int bovengrens;
public static void Main()
{
// Gedeelde counter voor threads
threadcounter = 0;
oplossingen = 0;
// Start inputer
int locktype = int.Parse(Console.ReadLine()); // 0 = TaS-lock, 1 = TTaS-lock, 2 = Array-lock
int ondergrens = int.Parse(Console.ReadLine()); // 0 - ondergrens
bovengrens = int.Parse(Console.ReadLine()); // ondergrens - 10^9
modulus = int.Parse(Console.ReadLine()); // 0 - 256
int threads = int.Parse(Console.ReadLine()); // 1 - 256
// Einde input
threadcounter = ondergrens;
// Start threading
for (int t = 0; t < threads; t++)
{
Thread thr = new Thread(new ThreadStart(doStuff));
thr.Start();
Console.WriteLine("Thread {0} created.", t);
}
// Einde threading
// REMOVE
Console.WriteLine(oplossingen);
Console.ReadKey(true);
}
public static int Calculate(int toCalc, int modulus)
{
int Total = 0;
for (int t = 1; t < toCalc.ToString().Length; t++)
{
Total += (((int)toCalc / (int)Math.Pow(10, t - 1)) % 10) * t;
}
return (int)Total % modulus;
}
public static void doStuff()
{
// De berekening
// interlocked.Increment zorgt voor een atomaire ophoging van een teller
int getal = (int)Interlocked.Increment(ref threadcounter);
if (getal <= (int)bovengrens)
{
if (Calculate((int)getal, (int)modulus) == 0)
{
// interlocked.Increment zorgt voor een atomaire ophoging van een teller
Interlocked.Increment(ref oplossingen);
}
doStuff();
}
}
}
}