Python big integer multiplication method guide

Source: Internet
Author: User
For the calculation of large integers, some method is usually used for conversion, otherwise it will overflow. But python has no such concerns. Python supports "infinite precision" Integers. Generally, you do not need to consider integer overflow. In addition, the PythonInt type and the Long integer class with any precision can be seamlessly converted, if the Int range is exceeded, it is converted to the Long type. For the calculation of large integers, some method is usually used for conversion, otherwise it will overflow. But python has no such concerns. Python supports "infinite precision" Integers. Generally, you do not need to consider integer overflow. In addition, the Python Int type and any precision Long integer class can be seamlessly converted, if the Int range is exceeded, it is converted to the Long type.

Problem

Multiply large integers

Ideas

For the calculation of large integers, some method is usually used for conversion, otherwise it will overflow. But python has no such concerns.

Python supports "infinite precision" Integers. Generally, you do not need to consider integer overflow. In addition, the Python Int type and any precision Long integer class can be seamlessly converted, if the Int range is exceeded, it is converted to the Long type.

For example:

>>> 2899887676637907866*17887789927883482773899435187258157415700236034169791337062588991638L


Note: the "infinite precision" in the front is enclosed in quotation marks. In fact, there are limits. for 32-bit machines, the upper limit is 2 ^ 32-1. It's really big enough.

Why can Python do this? If you are interested, go to the Python source code. This article will not go into details.

In other languages, "divide and conquer" is usually used to solve the big integer multiplication problem.

However, here we provide a very interesting method to calculate the multiplication of two integers, which is used as a demonstration of the multiplication of large integers.

Multiply two integers: Arabic multiplication. For a detailed description of this multiplication, see :#

Solution (Python)

#! /Usr/bin/env python # coding: UTF-8 # Arabic multiplication def arabic_multiplication (num1, num2): num_lst1 = [int (I) for I in str (num1)] # convert 123 of the int type to [, 3] of the list type. each element is of the int type num_lst2 = [int (I) for I in str (num2)] # multiply the integers in two lists by int_martix = [[I * j for I in num_lst1] for j in num_lst2] # Convert the list with the above elements as numbers into str with the element type, it mainly includes 9 --> '09' str_martix = [map (convert_to_str, int_martix [I]) for I in range (len (int_martix)] # Separate the double digits in each list: ['01', '29', '03'] --> [, 0, 3] martix = [[int (str_martix [I] [j] [z]) for j in range (len (str_martix [I])] for I in range (len (str_martix) for z in range (2)] # Calculate the Start items on the left of the Arabic multiplication table and sum_left = summ_left (martix) # Calculate the beginning of each item and sum_end = summ_end (martix) at the bottom of the Arabic multiplication table # combine the above two results and flip sum_left.extend (sum_end) sum_left.reverse () # obtain the numbers of individual vertices (if carry is added) result = take_digit (sum_left) # flip the result and merge it into a result string value result. reverse () int_result = "". join (result) print "% d * % d =" % (num1, num2) print int_result # Convert the int type to the str type, 9 --> '09' def convert_to_str (num): if num <10: return "0" + str (num) else: return str (num) # calculate the sum of items starting from the left side of the Arabic multiplication table def summ_left (lst): summ = [] x = [I for I in range (len (lst)] y = [j for j in range (len (lst [0])] sx = [I for I in x if I % 2 = 0] for I in sx: s = 0 j = 0 while I> = 0 and j <= y [-1]: s = s + lst [I] [j] if I % 2 = 1: j = j + 1 else: j = j I = i-1 summ. append (s) return summ # calculate the sum of the items starting at the bottom of the Arabic multiplication table def summ_end (lst ): summ = [] y = [j for j in range (len (lst [0])] ex = len (lst)-1 for m in range (len (y )): s = 0 I = ex j = m while I> = 0 and j <= y [-1]: s = s + lst [I] [j] if I % 2 = 1: j = j + 1 else: j = j I = i-1 (summ. append (s) return summ # obtain the single-digit number of each element. if it is greater than 10, the next carry def take_digit (lst ): tmp = 0 digit_list = [] for m in range (len (lst): lstm = 0 lstm = lst [m] + tmp if lstm <10: tmp = 0 digit_list.append (str (lstm) else: tmp = lstm/10mm = lstm-tmp * 10 digit_list.append (str (mm )) return digit_listif _ name __= = "_ main _": arabic_multiplication (469,37)

The above is the details of the guide to the big integer multiplication method in python. For more information, see other related articles in the first PHP community!

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.