-
Notifications
You must be signed in to change notification settings - Fork 0
/
prim.cpp
66 lines (56 loc) · 1 KB
/
prim.cpp
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
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
bool isPrime(int number)
{
int n = number;
if (number < 0)
{
n = -1 * number;
}
for (int i = 2; i <= sqrt(number); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int kThPrimeDivider(int number, int k)
{
int dividerCount = 0;
for (int i = 2; i <= number / 2; i++)
{
if (number % i == 0 && isPrime(i))
{
dividerCount++;
}
if (dividerCount == k)
{
return i;
}
}
return 0;
}
int main()
{
cout << kThPrimeDivider(30, 3) << endl;
//TESZT
cout << isPrime(5) << endl;
cout << isPrime(6) << endl;
//PELDA
int primSum = 0;
for (int i = 1; i <= 5; i++)
{
if (isPrime(i))
{
cout << i << (i < 5 ? "..." : "");
primSum += i;
}
}
cout << endl
<< primSum << endl;
return 0;
}