2015 Huawei server test-the ending number of the high power, and the ending number of the 2015 Huawei power

Source: Internet
Author: User

2015 Huawei server test-the ending number of the high power, and the ending number of the 2015 Huawei power

Description:


The last three digits (M, N are all greater than 10) of the N power of M ).

 

Interface Description

Prototype:

Int GetLast3DigitsOfMN (int M, int N );

Input parameters:

Int M: M> 10

Int N: N> 10

Return Value:

The last three digits of M's n power


Solution:

Idea 1: first calculate the Npower of M and then M ^ N % 1000 calculate the last three digits. However, the Npower of M is likely to overflow, resulting in an exception or error in the output result.

Train of Thought 2: If train of thought fails, we need to find its corresponding equivalent method. M calculates the remainder for every multiplication of M, and its last three digits are equivalent.


The Code is as follows:

Public static int getLast3DigitsOfMN (int M, int N) {int temp = M; for (int I = 1; I <N; I ++) {temp = M * temp % 1000;} return temp ;}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.