-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunkcije sa prostim činiocima.cpp
104 lines (99 loc) · 2.53 KB
/
Funkcije sa prostim činiocima.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <vector>
#include <bitset>
#include <math.h>
#define NAJVECA_VELICINA_SITA 10000010
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
vll prosti; //lista svih generisanih prostih brojeva
bitset<NAJVECA_VELICINA_SITA> bs;
ll velicina_sita;
void EratostenovoSito(ll gornja_granica)
{
//dodajemo 1 da bi ukljucili i gornju granicu
velicina_sita = gornja_granica + 1;
bs.set(); //sve elemente postavlja na 1
bs[0] = bs[1] = 0;
for(ll i = 2; i < velicina_sita; i++)
if(bs[i])
{
//precrtavamo sve brojeve deljive sa i pocevsi od i * i
for(ll j = i * i; j < velicina_sita; j += i)
bs[j] = 0;
prosti.push_back((int)i);
}
}
ll brojPF(ll N) //koristimo ll za tip long long kao i pre
{
ll PF_idx = 0 , PF = prosti[PF_idx], rez = 0;
while(N != 1 && (PF * PF <= N))
{
while(N % PF == 0)
{
N /= PF;
rez++;
}
PF = prosti[++PF_idx];
}
if(N != 1)
rez++;
return rez;
}
ll brojDelilaca(ll N)
{
ll PF_idx = 0 , PF = prosti[PF_idx], rez = 1;
while(N != 1 && (PF * PF <= N))
{
int stepen = 0;
while(N % PF == 0)
{
N /= PF; stepen++;
}
rez *= (stepen + 1);
PF = prosti[++PF_idx];
}
if(N != 1) rez *= 2;
return rez;
}
ll zbirDelilaca(ll N)
{
ll PF_idx = 0 , PF = prosti[PF_idx], rez = 1;
while(N != 1 && (PF * PF <= N))
{
int stepen = 0;
while(N % PF == 0)
{
N /= PF; stepen++;
}
rez *= ((ll)pow((double)PF, stepen + 1.0) - 1) / (PF - 1);
PF = prosti[++PF_idx];
}
if(N != 1) rez *= (N + 1);
return rez;
}
ll EulerPhi(ll N)
{
ll PF_idx = 0 , PF = prosti[PF_idx], rez = N;
while(N != 1 && (PF * PF <= N))
{
if(N % PF == 0)
rez -= rez / PF;
while(N % PF == 0)
N /= PF;
PF = prosti[++PF_idx];
}
if(N != 1)
rez -= rez / N;
return rez;
}
int main()
{
EratostenovoSito(10000000);
ll broj = 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 7 * 13 * 23 * 43;
cout << brojPF(broj) << " " << brojDelilaca(broj) << " " << zbirDelilaca(broj) << " " << EulerPhi(broj) << "\n";
ll n = 12;
cout << brojPF(n) << " " << brojDelilaca(n) << " " << zbirDelilaca(n) << " " << EulerPhi(n) << "\n";
return 0;
}