Python 模拟竖式大数乘法,python竖式大数乘法,python内置了大数运
Python 模拟竖式大数乘法,python竖式大数乘法,python内置了大数运
python内置了大数运算,非常方便,无聊写了个模拟竖式乘法的小程序,可以计算任意位数乘法.python爱好者,欢迎交流
运行结果及时间
123456709* 1234567890987654321---------------------------- 123456709 246913418 370370127 493826836 617283545 740740254 864196963 987653672 1111110381 000000000 1111110381 987653672 864196963 740740254 617283545 493826836 370370127 246913418 123456709 ----------------------------152415688858406562100289589Elapsed time: 0.10666831 seconds 1234567890987654321* 123456709---------------------------- 11111111018888888889 0000000000000000000 8641975236913580247 7407407345925925926 6172839454938271605 4938271563950617284 3703703672962962963 2469135781975308642 1234567890987654321 ----------------------------152415688858406562100289589Elapsed time: 0.05573613 seconds
[Python]代码
#! /urs/bin/env pythonfrom mytictoc import tic, toc# big intiger multiplicationdef bigmul(a,b): sa = str(a) sb = str(b) resultline = ' '*(len(sa)+len(sb)) # a # * b print print resultline[:-len(sa)]+sa print '*'+resultline[:-len(sb)-1]+sb # --- print '-'*(len(sa)+len(sb)) # ### # ### # ### result_list = [] cursp = 0 for db in sb[::-1]: if int(db)==0: result = '0'*len(sa) else: result = '' carrier = 0 for da in sa[::-1]: #print db,da mr = int(db)*int(da)+carrier carrier = mr/10 result += str(mr%10) if carrier!=0: result += str(carrier) result = result[::-1]+' '*cursp cursp += 1 result_list.append(resultline[:-len(result)]+result) print result_list[-1] # ---- print '-'*(len(sa)+len(sb)) # result print str(sum([int(x.replace(' ','0')) for x in result_list])) # unit testdef main(): a = 1234567890987654321 b = 123456709 tic() bigmul(b,a) toc() tic() bigmul(a,b) toc()if __name__=='__main__': main()
相关内容
- django接入新浪微博OAuth,djangooauth,最近将网站和新浪微
- webpy入门猜数字游戏,webpy入门猜数字,简单的猜数字游
- Python 字符编码实例,python编码实例,[Python]代码#c
- windows平台下如何用Python杀进程,平台python,[Python]代码
- Python脚本实现查找文件,,[Python]代码#c
- Python uuid模块,pythonuuid模块,python模块之uui
- 理解Python元类(Metaclasses),元类metaclasses,“元类的魔
- Python数组功能特点分析,python数组功能特点,实际上Py
- python 使用poplib pop3协议收取邮件,pythonpoplib,[Python]代码
- c,c++,java,python,ruby实现将文件读入数组的,pythonruby,c实现
评论关闭