I have to calculate following two mathematical formula in C programming language.
- result = 48^108 // 108 should be 103
- result%143 =9
To calculate this two equation I need some data type whose range is large enough.
I know the end result of equation 2 is 9. However, I did not get this answer after using several datatypes.
Here are some of my implementations.
1. #include <stdio.h>
#include<math.h>
int main()
{
unsigned int a1=48,a2=103,a3, a4;
a3= pow(48,103);
printf("a3=%u",a3);
a4= a3 % 143;
printf("\n a4=%u",a4);
return 0;
}
Answer That I got:
warning: overflow in conversion from ‘double’ to ‘unsigned int’ changes value from ‘1.4717954286441339e+173’ to ‘4294967295’ [-Woverflow]
15 | a3= pow(48,103);
| ^~~
a3=4294967295
a4=47
2. int main()
{
unsigned long a1=48,a2=103,a3, a4;
a3= pow(48,103);
printf("a3=%lu",a3);
a4= a3 % 143;
printf("\n a4=%lu",a4);
return 0;
}
warning: overflow in conversion from ‘double’ to ‘long unsigned int’ changes value from ‘1.4717954286441339e+173’ to ‘18446744073709551615’ [-Woverflow]
15 | a3= pow(48,103); | ^~~ a3=18446744073709551615 a4=16
3. #include <stdio.h>
#include<math.h>
int main()
{
long double a1=48,a2=103,a3, a4;
a3= pow(48,103);
printf("a3=%LF",a3);
a4= fmod(a3,143);
printf("\n a4=%LF",a4);
return 0;
}
a3=147179542864413390695231668723836254417826202083285489645297997883519171141486480221363409432872885235091123842885421688012169987663834748443552551569845821059256315786821632.000000
a4=46.000000
Which data type should I use to handle the situation?
I apologies for my wrong equation on equation 1. It is 103 not 108. and if I use 103 the answer is 9.
Thank you for all your comments and process.
The remainder of 48103 modulo 143 is easily calculated by decomposing the exponent into powers of two and keeping all the intermediate values reduced modulo 143, as illustrated in the following code. (This is not necessarily the method that uses the fewest arithmetic operations.)