A guide to multiplying large integers in python

Source: Internet
Author: User
For large integer calculations, it is common to use some method of conversion, otherwise it will overflow. But Python has no such concerns. Python supports "infinite precision" integers, generally without the problem of integer overflow, and the Python int type and any precision long integer class can be transformed seamlessly, and the case that exceeds the int range is converted to a long type.

Problem

Multiply large integers

Idea Description

For large integer calculations, it is common to use some method of conversion, otherwise it will overflow. But Python has no such concerns.

Python supports "infinite precision" integers, generally without the problem of integer overflow, and the Python int type and any precision long integer class can be transformed seamlessly, and the case that exceeds the int range is converted to a long type.

For example:

>>> 2899887676637907866*17887789927883482773899435187258157415700236034169791337062588991638l


Note: The preceding "infinite precision" is quoted. In fact there is a limit, for 32-bit machines, the upper limit is: 2^32-1. It's really big enough.

Why does Python do that? Please be interested in inquiring about the source code of Python. This article does not repeat.

In other languages, the multiplication of large integers is usually solved by the "divide-and-conquer" method.

However, here is a very interesting way to calculate the multiplication of two integers as a demonstration of multiplying large integers.

Multiply two integers: Arabic multiplication. For a detailed description of this multiplication, see: http://www.php.cn/

Resolution (Python)

#!/usr/bin/env python#coding:utf-8# Arabic multiplication def arabic_multiplication (num1,num2): Num_lst1 = [Int (i) for i in Str (NUM1)] #将i NT type 123, converted to the list type of [Num_lst2], each element is of type int = [INT (i) for i in Str (num2)] #两个list中整数两两相乘 Int_martix = [[I*j for I I N Num_lst1] for J in Num_lst2] #将上述元素为数字的list转化为元素类型是str, mostly will be 9--> ' Str_martix = [Map (Convert_to_str,int_martix[i] ) for I in Range (len (Int_martix))] #将上述各个list中的两位数字分开: [']-->[0,2,0],[1,9,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)] #计算阿拉伯乘法表的左侧开始各项和 Sum_le FT = Summ_left (Martix) #计算阿拉伯乘法表的底部开始各项和 sum_end = Summ_end (Martix) #将上述两个结果合并后翻转 sum_left.extend (sum_end) SUM_LEFT.R Everse () #取得各个和的个位的数字 (if carry is added) result = Take_digit (sum_left) #翻转结果并合并为一个结果字符串数值 result.reverse () Int_result = "". Join    (result) print "%d*%d="% (num1,num2) print int_result# convert int type to str type,9--> ' the "def convert_to_str (num): If num<10: Return "0" +str (num) Else:    return str (num) #计算阿拉伯乘法表格左侧开始的各项之和def summ_left (LST): Summ = [] x = [i-I in range (len (LST))] y = [J-J in Ra]  Nge (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 Arabic multiplication table Bottom Open    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# gets the single digit of each element if it is greater than 10 to 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)) E  Lse:tmp = lstm/10 mm = lstm-tmp*10 digit_list.append (str (mm)) return digit_listif __name__== "__main__": Arabic_multiplication (469,37) 

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.