Home > Back-end >  To calculate the NTH power of a mod p, a, n, p are positive integers, 32-bit integer variables store
To calculate the NTH power of a mod p, a, n, p are positive integers, 32-bit integer variables store

Time:09-27

Requirements: 1. The use of c language

2. Using recursive method (is it is better to do statement and base of some methods, such as conditional statements, because behind haven't learned, the teacher not to use)

CodePudding user response:

The procedure is as follows,
#include
#include

Int main ()
{
Int a, n, p, num, temp=1;//remainder num, temp for intermediate values calculated
Printf (" please input variable a, n, p corresponding value, input format, for example: 3 and 6 \ n ");
The scanf (" % d, % d, % d ", & amp; A, & amp; N, & amp; P);
// method 1 power function to solve the problem using pow
Num=(int) pow ((double) a, (double) n) % p;

// method using a for loop to solve the problem
/*
for(int i=1; i <=n; I++)
{
Temp=temp * a;
}
*/
Num=temp % p;
Printf (" \ n % d ^ (% d) mod=% d % d ", a, n, p, num);
}

CodePudding user response:

reference dared to 001 on the second floor response:
program is as follows,
#include
#include

Int main ()
{
Int a, n, p, num, temp=1;//remainder num, temp for intermediate values calculated
Printf (" please input variable a, n, p corresponding value, input format, for example: 3 and 6 \ n ");
The scanf (" % d, % d, % d ", & amp; A, & amp; N, & amp; P);
// method 1 power function to solve the problem using pow
Num=(int) pow ((double) a, (double) n) % p;

// method using a for loop to solve the problem
/*
for(int i=1; i <=n; I++)
{
Temp=temp * a;
}
*/
Num=temp % p;
Printf (" \ n % d ^ (% d) mod=% d % d ", a, n, p, num);
}


Thank you thank you , thank you very much