Question: What is the last 4 digits of 712 to the power of 729?

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.