python处理大数字的方法

本文实例讲述了python处理大数字的方法。分享给大家供大家参考。具体实现方法如下:

def getFactorial(n):
  """returns the factorial of n"""
  if n == 0:
    return 1
  else:
    k = n * getFactorial(n-1)
    return k
for k in range(1, 70):
  print "factorial of", k,"=", getFactorial(k)

运行结果如下:

factorial of 1 = 1
factorial of 2 = 2
factorial of 3 = 6
factorial of 4 = 24
factorial of 5 = 120
factorial of 6 = 720
factorial of 7 = 5040
factorial of 8 = 40320
factorial of 9 = 362880
factorial of 10 = 3628800
factorial of 11 = 39916800
factorial of 12 = 479001600
factorial of 13 = 6227020800
factorial of 14 = 87178291200
factorial of 15 = 1307674368000
factorial of 16 = 20922789888000
factorial of 17 = 355687428096000
factorial of 18 = 6402373705728000
factorial of 19 = 121645100408832000
factorial of 20 = 2432902008176640000
factorial of 21 = 51090942171709440000
factorial of 22 = 1124000727777607680000
factorial of 23 = 25852016738884976640000
factorial of 24 = 620448401733239439360000
factorial of 25 = 15511210043330985984000000
factorial of 26 = 403291461126605635584000000
factorial of 27 = 10888869450418352160768000000
factorial of 28 = 304888344611713860501504000000
factorial of 29 = 8841761993739701954543616000000
factorial of 30 = 265252859812191058636308480000000
factorial of 31 = 8222838654177922817725562880000000
factorial of 32 = 263130836933693530167218012160000000
factorial of 33 = 8683317618811886495518194401280000000
factorial of 34 = 295232799039604140847618609643520000000
factorial of 35 = 10333147966386144929666651337523200000000
factorial of 36 = 371993326789901217467999448150835200000000
factorial of 37 = 13763753091226345046315979581580902400000000
factorial of 38 = 523022617466601111760007224100074291200000000
factorial of 39 = 20397882081197443358640281739902897356800000000
factorial of 40 = 815915283247897734345611269596115894272000000000
factorial of 41 = 33452526613163807108170062053440751665152000000000
factorial of 42 = 1405006117752879898543142606244511569936384000000000
factorial of 43 = 60415263063373835637355132068513997507264512000000000
factorial of 44 = 2658271574788448768043625811014615890319638528000000000
factorial of 45 = 119622220865480194561963161495657715064383733760000000000
factorial of 46 = 5502622159812088949850305428800254892961651752960000000000
factorial of 47 = 258623241511168180642964355153611979969197632389120000000000
factorial of 48 = 12413915592536072670862289047373375038521486354677760000000000
factorial of 49 = 608281864034267560872252163321295376887552831379210240000000000
factorial of 50 = 30414093201713378043612608166064768844377641568960512000000000000
factorial of 51 = 1551118753287382280224243016469303211063259720016986112000000000000
factorial of 52 = 80658175170943878571660636856403766975289505440883277824000000000000
factorial of 53 = 4274883284060025564298013753389399649690343788366813724672000000000000
factorial of 54 = 230843697339241380472092742683027581083278564571807941132288000000000000
factorial of 55 = 12696403353658275925965100847566516959580321051449436762275840000000000000
factorial of 56 = 710998587804863451854045647463724949736497978881168458687447040000000000000
factorial of 57 = 40526919504877216755680601905432322134980384796226602145184481280000000000000
factorial of 58 = 2350561331282878571829474910515074683828862318181142924420699914240000000000000
factorial of 59 = 138683118545689835737939019720389406345902876772687432540821294940160000000000000
factorial of 60 = 8320987112741390144276341183223364380754172606361245952449277696409600000000000000
factorial of 61 = 507580213877224798800856812176625227226004528988036003099405939480985600000000000000
factorial of 62 = 31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
factorial of 63 = 1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
factorial of 64 = 126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
factorial of 65 = 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
factorial of 66 = 544344939077443064003729240247842752644293064388798874532860126869671081148416000000000000000
factorial of 67 = 36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000
factorial of 68 = 2480035542436830599600990418569171581047399201355367672371710738018221445712183296000000000000000
factorial of 69 = 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000

希望本文所述对大家的Python程序设计有所帮助。

(0)

相关推荐

  • python中将阿拉伯数字转换成中文的实现代码

    复制代码 代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass class OutOfRangeError(Exception): pass _MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', ) _P0 = (u'', u'十', u'百', u'千', )

  • python list使用示例 list中找连续的数字

    线上有个需求,格式化,从一堆s1,s100-s199中找出连续的服并且格式化显示出来,如: 神魔:S106-109,s123,s125御剑: s106-109,s123,s125 复制代码 代码如下: #!/usr/bin/env python# -*- coding: utf-8 -*- #  2014/01/15 14:15import sysfrom itertools import *from operator import itemgetterdef parse(filename): 

  • python3实现短网址和数字相互转换的方法

    本文实例讲述了python3实现短网址和数字相互转换的方法.分享给大家供大家参考.具体实现方法如下: import math import decimal def convert_to_code(num): """ 将数字转换为代码 """ def get_num(num, out=''): num = decimal.Decimal(num) codes = "abcdefghjkmnpqrstuvwxyz23456789ABCDEF

  • kNN算法python实现和简单数字识别的方法

    本文实例讲述了kNN算法python实现和简单数字识别的方法.分享给大家供大家参考.具体如下: kNN算法算法优缺点: 优点:精度高.对异常值不敏感.无输入数据假定 缺点:时间复杂度和空间复杂度都很高 适用数据范围:数值型和标称型 算法的思路: KNN算法(全称K最近邻算法),算法的思想很简单,简单的说就是物以类聚,也就是说我们从一堆已知的训练集中找出k个与目标最靠近的,然后看他们中最多的分类是哪个,就以这个为依据分类. 函数解析: 库函数: tile() 如tile(A,n)就是将A重复n次

  • python实现将英文单词表示的数字转换成阿拉伯数字的方法

    本文实例讲述了python实现将英文单词表示的数字转换成阿拉伯数字的方法.分享给大家供大家参考.具体实现方法如下: import re _known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourt

  • python判断字符串是否纯数字的方法

    本文实例讲述了python判断字符串是否纯数字的方法.分享给大家供大家参考.具体如下: 判断的代码如下,通过异常判断不能区分前面带正负号的区别,正则表达式可以根据自己需要比较灵活的写,通过isdigit方法用来判断是否是纯数字,测试代码如下 复制代码 代码如下: #!/usr/bin/python # -*- coding: utf-8 -*- a = "1" b = "1.2" c = "a" #通过抛出异常 def is_num_by_exc

  • Python将阿拉伯数字转换为罗马数字的方法

    本文实例讲述了Python将阿拉伯数字转换为罗马数字的方法.分享给大家供大家参考.具体实现方法如下: def numToRomanNum(Num): """digital will be converted into Roman numerals,Ex: numToRomanNum(3999)""" if Num < 1 or Num > 3999: print 'The Num must in 1-3999' else: NumDi

  • Python实现把数字转换成中文

    周末在家,写了个小程序,用于将阿拉伯数字转换化大写中文.程序没经过任何优化,出没经过详细的测试,挂到网上,方便将来有需要的时候直接拿来用. #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exception): pass class OutOfRangeError(Exception): pass _MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五'

  • Python中列表元素转为数字的方法分析

    本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: 有一个数字字符的列表: numbers = ['1', '5', '10', '8'] 想要把每个元素转换为数字: numbers = [1, 5, 10, 8] 用一个循环来解决: new_numbers = []; for n in numbers: new_numbers.append(int(n)); numbers = new_numbers; 有没有更简单的语句可以做到呢? 1. numbers =

  • python基础教程之数字处理(math)模块详解

    1.math简介 复制代码 代码如下: >>> import math>>>dir(math)          #这句可查看所有函数名列表>>>help(math)         #查看具体定义及函数0原型 2.常用函数 复制代码 代码如下: ceil(x) 取顶floor(x) 取底fabs(x) 取绝对值factorial (x) 阶乘hypot(x,y)  sqrt(x*x+y*y)pow(x,y) x的y次方sqrt(x) 开平方log(x

  • python实现的简单猜数字游戏

    本文实例讲述了python实现的简单猜数字游戏.分享给大家供大家参考.具体如下: 给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好. import random n = random.randint(1, 99) guess = int(raw_input("Enter an integer from 1 to 99: ")) while n != "guess": print

  • Python实现将罗马数字转换成普通阿拉伯数字的方法

    本文实例讲述了Python实现将罗马数字转换成普通阿拉伯数字的方法.分享给大家供大家参考,具体如下: 罗马数字,我们在某些电视中或者现实生活中都曾经看到过,近日,学习Python时,也遇到了罗马数字的解说,于是顺便写了一个小程序来练习罗马数字到我们日常生活普通数字之间的转换的小函数. 首先,咱们了解一下,罗马数字的潜在法则, 在罗马数字中,利用7个不同字母进行重复或者组合来表达各式各样的数字. I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000

随机推荐