-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarmichael.cs
More file actions
75 lines (70 loc) · 2.28 KB
/
Carmichael.cs
File metadata and controls
75 lines (70 loc) · 2.28 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
//Получаем простые числа и находим среди них делители числа
List<int> spisok = generatePrimes(100);
List<int> deliteli = new List<int>();
//Формируем список делителей
for (int i = 0; i < spisok.Count; i++)
{
if (n % spisok[i] == 0)
deliteli.Add(spisok[i]);
}
//Если делителей больше одного - проверяем является ли числом Кармайкла
if (deliteli.Count > 1)
{
var Kar = deliteli.Where(element => (n - 1) % (element - 1) == 0);
if (Kar.Count() == deliteli.Count)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
else
{
Console.WriteLine("NO");
}
}
//Генератор простых чисел
static List<int> generatePrimes(int toGenerate)
{
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
while (primes.Count < toGenerate)
{
int nextPrime = (primes[primes.Count - 1]) + 2;
while (true)
{
bool isPrime = true;
foreach (int n in primes)
{
if (nextPrime % n == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
break;
}
else
{
nextPrime += 2;
}
}
primes.Add(nextPrime);
}
return primes;
}
}
}