Exponential operation of the computing model of the Algorithm

Source: Internet
Author: User

Module-based exponential Computation

Long live family, long live family.
Introduction
Oh, after the holiday, I went home, and the village was quite well-built. In the evening, Aunt Auntie came out to dance. I am still in coding. It's really nice to have a coding day. I like rural areas, I'm a farmer, not yet a village cadre!
Modulo exponential operation
The functionality of the original model is so powerful that I didn't care too much about it before. It's really embarrassing. The module gives the Organization great meaning, so that big data can be processed by us. This is my understanding of the module. Definition of the problem: given x and y, calculate the value of x's y Power modulo N, where N has hundreds of digits. The algorithm is as follows (recursive algorithm)
The principle is as follows:

Lab


As long as the algorithm is recursive, I will convert it into a non-recursive algorithm and give it to you. Test1.cpp
#include
 
  #include
  
   using namespace std;// function modexp in recursionint modexp_re(int x,int y,int N);// function modexp without recursionint modexp_nonre(int x,int y,int N);int const N = 10000 ;// function mainint main(){int a,b,i = 0;while(i < 100){a = rand()%10000;b = rand()%10000;cout<<"a="< * s = new stack
   
    ;while(y >= 1){s->push(y);y = y/2;}result = x % N;while(s->size()>1){z = s->top();s->pop();result = ( (result%N) * (result%N) )%N;if(s->empty())break;else if(z*2 != s->top()){result = (result * (x%N) )%N;}}return result;}}
   
  
 



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.