Q: design a program to find the last 4 digits of 712 to the power of 729?
The multiplication of large integers cannot be a simple multiplication. It has long been a problem. Therefore, we need to find another way. The last four digits are discarded each time during the multiplication, without affecting the last four digits.
The source code is as follows:
# Include <iostream> # include <iomanip> using namespace STD; int main () {int value = 1; for (INT I = 1; I <= 729; ++ I) {value * = 712; Value % = 10000;} // set the output format, 4 bits, and fill it with 0. cout <SETW (4) <setfill ('0') <value <Endl; System ("pause"); Return 0 ;}
If we notice 729 = 3 ^ 6, we can reduce the number of cycles.
#include<stdio.h>int main(){ unsigned long long a3 = (712*712*712)%10000; unsigned long long a9 = (a3 *a3*a3)%10000; unsigned long long a27 = (a9 *a9*a9)%10000; unsigned long long a81 = (a27 *a27*a27)%10000; unsigned long long a243 = (a81 *a81*a81)%10000; unsigned long long a729 = (a243 *a243*a243)%10000; printf ("result=%d \n",a729); getchar(); return 0;}
Post reposted from: I love programmers www.52coder.net