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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started