Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize large integer multiplication and divide-and-conquer algorithm

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to realize large integer multiplication and divide and conquer algorithm". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve large integer multiplication and divide and conquer algorithm"!

General multiplier operation

There is a relatively simple and easy to understand method for multiplier operation, and we can use the column vertical calculation method learned in primary school to carry out multiplication.

Column vertical multiplication

With reference to the column vertical calculation method in the figure above, we implement it in code.

# include # include std::string multiply (std::string a, std::string b) {std::string result = ""; int row = b.size (); int col = a.size () + 1; int tmp [row] [col]; memset (tmp,0, sizeof (int) * row*col); reverse (a.begin (), a.end ()) Reverse (b.begin (), b.end ()); for (int I = 0; I

< b.size(); i++) { for(int j = 0; j < a.size(); j++) { std::string bit_a = std::string(1, a.at(j)); std::string bit_b = std::string(1, b.at(i)); tmp[i][j] += std::stoi(bit_a) * std::stoi(bit_b); tmp[i][j+1] = tmp[i][j] / 10; tmp[i][j] %= 10; } } int N = a.size() + b.size(); int sum[N]; memset(sum, 0, sizeof(int)*N); for(int n = 0; n < N; n++) { int i = 0; int j = n; while (i = 0 ) { if(i < row && j < col) { sum[n] += tmp[i][j]; } i++; j--; } if( n+1 < N ) { sum[n+1] = sum[n] / 10; sum[n] %= 10; } } bool zeroStartFlag = true; for (int i = N-1; i >

= 0; return result; -) {if (zeroStartFlag [I] = = 0 & & zeroStartFlag) {continue;} zeroStartFlag = false; result.append (std::to_string (Sumi));} return result;} int main () {std::string a = "3456"; std::string b = "1234" Std::string result = multiply (a, b); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report