Question: C++ program Can someone help modify this code so that it works for larger numbers. It works for things like 13^11 % 5 = 2

C++ program

Can someone help modify this code so that it works for larger numbers. It works for things like 13^11 % 5 = 2 and 7^13 % 9 = 7 but when entering 40 ^77 % 119 I get 41 when it should 10 or 12^43 % 713 I don't get 48.

#include #include using namespace std;

main() { int i = 0;

while (i < 4) { int a = 0; int n = 0; int p = 0; int r = 0;

long long w = 1; long long x = 0; long long y = 1; long long z = 0;

cout << "This program will calculate the operation a^n mod p" << endl; cout << "Input a, the base" << endl; cin >> a; cout << "Input n, the exponent" << endl; cin >> n; cout << "Input p; the modulus" << endl; cin >> p;

z = a % p;

for(int j = 0; j < 64; j++) { long long x = 1;

if(n & (x << j)) {

for(int c = 0; c < j; c++) {

x = pow(z,2); z = x % p; }

y = y * z; } }

r = y % p;

cout << a << "^" << n << " mod " << p << " = " << r << endl;

i++; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!