-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
66 lines (56 loc) · 1.69 KB
/
main.m
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
% variable inputs
% for (a^x + b^y) % m
disp('Enter inputs for (a^x + b^y) % m');
a = input('a: ');
x = input('x: ');
b = input('b: ');
y = input('y: ');
m = input('m: ');
%---------------------------------------------------------
% Calculating (a^x + b^y) % m by fast modulo exponentiation
start_1 = cputime;
% calculating a^x mod m
ea = fast_modulo_exponentiation(a, x, m);
% calculating b^y mod m
eb = fast_modulo_exponentiation(b, y, m);
% result = (ea + eb) % m
res = modulo(ea + eb, m);
end_1 = cputime - start_1;
fprintf('Result using fast modulo exponentiation: %d\nTime: %f\n', res, end_1);
%---------------------------------------------------------
% Calculating (a^x + b^y) % m by reducing power of x and y
% by using phi(m), and then using fast modulo exponentiation
start_1 = cputime;
% calculate phi of m
pi=phi(m);
% check if a and m are co prime
if (gcd_int(a, m) == 1)
ea = fast_modulo_exponentiation(a, modulo(x, pi), m);
else
ea = fast_modulo_exponentiation(a, x, m);
end
% check if b and m are co prime
if (gcd_int(b, m) == 1)
eb = fast_modulo_exponentiation(b, modulo(y,pi), m);
else
eb = fast_modulo_exponentiation(b, y, m);
end
% result = (ea + eb) % m
res = modulo(ea + eb, m);
end_1 = cputime - start_1;
fprintf('Result using fast modulo exponentiation by reducing power: %d\nTime: %f\n', res, end_1);
%---------------------------------------------------------
% Brute
start_1 = cputime;
% res = mod( mod(power(a,x),m) + mod(power(b,y),m) , m);
ans1 = 1;
for i = 1:x
ans1 = mod((ans1 * a), m);
end
ans2 = 1;
for i = 1:y
ans2 = mod((ans2 * b), m);
end
res = mod((ans1 + ans2), m);
end_1 = cputime - start_1;
fprintf('Result using brute: %d\nTime: %f\n', res, end_1);