-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlf.c
56 lines (52 loc) · 894 Bytes
/
dlf.c
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
// Program to implement Discrete Logarithm function
#include <stdio.h>
#include <stdlib.h>
int gcd(int r1,int r2)
{
int q,r;
if(r2==0)
return r1;
else
{
q = r1/r2;
r = r1-q*r2;
r1 = r2;
r2 = r;
gcd(r1,r2);
}
}
int phi(int n)
{
int result = 0;
for(int i=1;i<n;i++)
{
if(gcd(n,i)==1)
{
result++;
}
}
return result;
}
int discretelog(int a, int b, int c)
{
int g=1;
b = b%c;
for(int i = 0;i<phi(c);i++)
{
if(g==b)
return 1;
g = (g*a)%c;
}
return -1;
}
int main()
{
int a,b,c;
printf("\n Enter the values of a,b,c \n");
scanf("%d %d %d",&a,&b,&c);
if(discretelog(a,b,c)==1)
printf("\n Discrete Logarithm Exists");
else
printf("\n Discrete Logarithm Doesn't Exists");
return 0;
}