-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExBoucles3.cs
50 lines (48 loc) · 1.47 KB
/
ExBoucles3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Logique;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exercices
{
public class ExBoucles3 : iCommand
{
public string Name => "03_ExBoucles";
public void Execute()
{
Console.Write("Je dois trouver combien de nombres premiers ? ");
int nb;
bool b = int.TryParse(Console.ReadLine(), out nb);
DateTime start = DateTime.Now;
bool test = false;
int count = 1;
int nbPremier = 2;
if (b && nb > 0)
{
while (count < nb)
{
nbPremier++;
test = false;
if (nbPremier%2 !=0)
{
int sqrt = (int)Math.Sqrt(nbPremier);
for (int i = 3; i <= sqrt && !test; i+=2)
{
if (nbPremier%i == 0)
{
test = true;
}
}
if (!test)
{
count++;
}
}
}
Console.WriteLine($"Le dernier nombre premier que j'ai trouvé est {nbPremier}.");
Console.WriteLine((DateTime.Now - start).TotalSeconds);
}
}
}
}