Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

2. Why do economists build models?

Answered: 1 week ago