#1
19.02.2015 um 05:17 Uhr4 "Parallele" Threads bringen dir reichlich wenig, um nicht zu sagen nichts, wenn du keine Wartezeiten hast.
C# :: Aufgabe #70 :: Lösung #7
using System;
using System.Collections.Generic;
namespace durch_1_30_teilbar {
class Program {
static decimal checkpoint = 100000000;
static bool fertig = false;
static int? limit = null;
static void Main(string[] args) {
while (!(limit > 1))
try {
if (limit != null) throw new FormatException();
Console.Write("Teilbar durch 1 - ");
limit = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException) {
Console.WriteLine("Bitte Gannzahl größer als 1 eingeben");
limit = null;
Console.ReadKey(false);
Console.Clear();
}
List<Worker> liste = new List<Worker>();
for (int i = 0; i < 4; i++) {
liste.Add(new Worker((decimal)(limit * i + limit), i + 1, limit));
}//2.329.089.562.800 (1-30)
System.Threading.Tasks.Parallel.ForEach(liste, x => x.DoMath());
Console.ReadKey(false);
}
class Worker {
decimal diezahl;
int id;
int? limit;
public Worker(decimal start, int id, int? limit) {
diezahl = start;
this.id = id;
this.limit = limit;
}
public void DoMath() {
bool found = false;
try {
while (!found) {
found = true;
for (int j = 2; j < limit; j++) {
if (diezahl % j != 0) {
found = false;
break;
}
}
if (diezahl > checkpoint) {
Console.WriteLine(">{0:#,0.0} {1}", checkpoint / 1000000000, "Millarden");
checkpoint = diezahl + 100000000;
}
if (found) break;
diezahl += (decimal)(limit * 4);
found = fertig;
}
}
catch (Exception e) {
found = false;
Console.WriteLine(e.Message.ToString());
}
if (!fertig) {
if (found) {
Console.WriteLine("{0} ist durch 1-{1} teilbar", diezahl, limit);
fertig = true;
}
else
Console.WriteLine("Keine Zahl gefunden");
}
}
}
}
}
Kommentare:
Mentalist999
Punkte: 680
21 Lösungen
50 Kommentare
DBqFetti
Punkte: 2480
53 Lösungen
20 Kommentare
Mentalist999
Punkte: 680
21 Lösungen
50 Kommentare