Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 430eff3

Browse files
Merge pull request fnplus#332 from orientor/CompleteModularExpo
Modular Exponentiation in C, Python and Java
2 parents f102d59 + d403562 commit 430eff3

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Iterative C program to compute modular power
2+
#include <stdio.h>
3+
4+
/* Iterative Function to calculate (x^y)%p in O(log y) */
5+
int power(int x, unsigned int y, int p)
6+
{
7+
int res = 1; // Initialize result
8+
9+
x = x % p; // Update x if it is more than or
10+
// equal to p
11+
12+
while (y > 0)
13+
{
14+
// If y is odd, multiply x with result
15+
if (y & 1)
16+
res = (res*x) % p;
17+
18+
// y must be even now
19+
y = y>>1; // y = y/2
20+
x = (x*x) % p;
21+
}
22+
return res;
23+
}
24+
25+
// Driver program to test above functions
26+
int main()
27+
{
28+
int x = 2;
29+
int y = 5;
30+
int p = 13;
31+
printf("Power is %u", power(x, y, p));
32+
return 0;
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Iterative Java program to
2+
// compute modular power
3+
import java.io.*;
4+
5+
class GFG {
6+
7+
/* Iterative Function to calculate
8+
(x^y)%p in O(log y) */
9+
static int power(int x, int y, int p)
10+
{
11+
// Initialize result
12+
int res = 1;
13+
14+
// Update x if it is more
15+
// than or equal to p
16+
x = x % p;
17+
18+
while (y > 0)
19+
{
20+
// If y is odd, multiply x
21+
// with result
22+
if((y & 1)==1)
23+
res = (res * x) % p;
24+
25+
// y must be even now
26+
// y = y / 2
27+
y = y >> 1;
28+
x = (x * x) % p;
29+
}
30+
return res;
31+
}
32+
33+
// Driver Program to test above functions
34+
public static void main(String args[])
35+
{
36+
int x = 2;
37+
int y = 5;
38+
int p = 13;
39+
System.out.println("Power is " + power(x, y, p));
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Iterative Python3 program
2+
# to compute modular power
3+
4+
# Iterative Function to calculate
5+
# (x^y)%p in O(log y)
6+
def power(x, y, p) :
7+
res = 1 # Initialize result
8+
9+
# Update x if it is more
10+
# than or equal to p
11+
x = x % p
12+
13+
while (y > 0) :
14+
15+
# If y is odd, multiply
16+
# x with result
17+
if ((y & 1) == 1) :
18+
res = (res * x) % p
19+
20+
# y must be even now
21+
y = y >> 1 # y = y/2
22+
x = (x * x) % p
23+
24+
return res
25+
26+
27+
# Driver Code
28+
29+
x = 2; y = 5; p = 13
30+
print("Power is ", power(x, y, p))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /