Detailed explanation of the algorithm for implementing the Fast Power modulo in Java and detailed explanation of the java Algorithm

Source: Internet
Author: User

Detailed explanation of the algorithm for implementing the Fast Power modulo in Java and detailed explanation of the java Algorithm

The introduction of the Fast Power modulo algorithm is presented by the limitations of the Simple Algorithm for modulo-obtaining decimals from large numbers, in a simple method, the calculation of a number such as 5 ^ 1003% 31 consumes a lot of our computing resources. The most troublesome part of the computing process is our 5 ^ 1003 process.

Disadvantage 1: In the subsequent exponential calculation process, the numbers calculated do not increase, occupying our computing resources (mainly time and space)

Disadvantage 2: The Middle process of our computation is big, and our existing computers cannot record such long data, therefore, we must find a more efficient solution to this problem.

When we calculate AB % C, the most convenient method is to call the pow method in the Math Function. However, sometimes A's B power number is too large, and even double will overflow, in this case, in order to get the result of AB % C, we will choose to use the Fast Power modulo algorithm to get the desired result in a simple and fast manner.

To prevent numeric overflow and reduce complexity, we need to use the following formula:

AB mod c = (a mod c) B mod c

This formula means that the remainder of the product is equal to the remainder of the product. It is easy to see that this formula is passed, so that we can keep a smaller and smaller by constantly getting the remainder to prevent overflow.

In theory, with this formula, we can write code. By constantly modulo a, we can ensure that the result will not overflow. This can indeed calculate the power of a larger power, however, the complexity of this method is still O (N), not fast.

To quickly calculate the power modulo, we need to rely on the following formula:

AB mod c = (a2) B/2 mod c, B is an even number
AB mod c = (a2) B/2 · a) mod c, B is an odd number

This formula is very simple. The principle is to constantly replace B with the square of a and replace B with the original half. Because we use the first formula to know that the modulo of a number is the same as the modulo of the same power (this is a bit round, that is, the meaning of Formula 1 ). The result of a * a % c is the same as that of.

Therefore, based on the above formula, we can obtain a method for calculating the power of the complexity O (logN:

import java.util.Scanner;public class Main { public static void main(String[] args) {  Scanner in = new Scanner(System.in);  int a = in.nextInt(), b = in.nextInt(), c = in.nextInt();  int res = 1;  a %= c;  for (; b != 0; b /= 2) {   if (b % 2 == 1)    res = (res * a) % c;   a = (a * a) % c;  }  System.out.println(res); }}

This algorithm is probably the case. The first step is to reduce a by a % = c to prevent the number overflow when a * a is first performed in. In a for loop, if the value of B is an odd number, res = res * a is set. In this case, a is multiplied to the result and processed, to prevent numeric overflow, the mod c operation of res * a is directly performed. In this for loop, one day in the morning and evening B will be equal to 1, enter the if branch, and finally calculate the res value mod c Out Of The for loop, to the final result.

Summary

The above is all the details about the Java language implementation of the Fast Power modulo algorithm, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.