Python——发生RecursionError: maximum recursion depth exceeded错误后的探索
RecursionError: maximum recursion depth exceeded
递归错误:超过最大递归深度
通过Python官网开发者指导PEP 611 -- The one million limit文档中查找发现:CPython中执行的硬限制是1000。
在PEP 606 -- Python Compatibility Version文档中发现:有 setrecursionlimit() 这个函数可以使用
利用Python语言试验了一下:发现在递归求994的阶乘时,开始出现Error:
RecursionError: maximum recursion depth exceeded in comparison
阶乘是啥:(来自于百度百科描述)
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
亦即n!=1×2×3×...×(n-1)×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。
下方是Python语言在Python3.7.1 Shell上的测试源码:
1 def func(num):
2 if num == 0 or num == 1:
3 return 1
4 return num * func(num-1)
5
6 num = 994 #在求994的阶乘时出错! 0 ≤ num ≤ 993都是可以计算的
7 print('{}的阶乘为:{}'.format(num,func(num)))
运行过程+结果(很长~):
1 Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
2 Type "help", "copyright", "credits" or "license()" for more information.
3 >>>
4 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
5 1的阶乘为:1
6 >>>
7 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
8 2的阶乘为:2
9 >>>
10 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
11 3的阶乘为:6
12 >>>
13 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
14 4的阶乘为:24
15 >>>
16 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
17 5的阶乘为:120
18 >>>
19 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
20 6的阶乘为:720
21 >>>
22 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
23 7的阶乘为:5040
24 >>>
25 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
26 8的阶乘为:40320
27 >>>
28 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
29 9的阶乘为:362880
30 >>>
31 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
32 10的阶乘为:3628800
33 >>>
34 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
35 100的阶乘为:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
36 >>>
37 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
38 900的阶乘为:67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
39 >>>
40 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
41 903的阶乘为:49555887990073386056452217876952995525488067949329341723790818106936120057782198142178033746726690651828085573411595853034037063665871336496371136272840133091701561011362700727370209232248188330822183654420859169142359589614806591490465798282867244090685145011611618211747486299350090084189007331871113225265825359788157901181186349227137475824487135288022656033753954425399694480985458175791924331557388030023481724140312352276229943162920287672691238274624305476033865125101473037865665705898653480514550106569540310539109507139291123143765829094040496131332081265393178485176781110190772323607463620589899252481955961003218157846569028905254663368083067281423310829240108565979071039671235340803443554404182369492134312151413290136057045737918175010534093078523107590430778418542591494681679318965857860495129899038183346941261126179350963853631550957216819667283267688425655230950168405060571278045024068494663914114428045553131358426286981320708557881574899849247421419700341762744129127855006203125871149585213897501637265808171320915867608203599333181972600080176789802283658480928073148727301793746769368895924964094117827035596396664202732974849587338695517958133297283260541063921047472274596777615582685640246159544273984706813340407031514644742595635098246241361376643079550483484529875422547639660376681753263562554683893536395588888383640587215582570325055056799723232197906376794443716576961550681883173676274383813636892904381923639622011140790976467726133309311755092920085604646721539878384255312324601943933507267015728738131525227803327434914283604480622102114423037819509352948740621462488467641021398020408026049473008953164509641885318762209139980215689398458246194295911640225935812838295114302170881304592866329379789738113437006611220282753600319393624941886235428921627484868305373316875269813793724637177739789574792910700917060196740218354086419451355723363401282373933193782931443214183242070375136958095370798609058770032711352974601298769692903663048608886977043496420419874773790820452496436814455866950004829678206137139200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
42 >>>
43 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
44 904的阶乘为:44798522743026340995032804960765507955041213426193724918306899568670252532235107120528942507040928349252589358364082651142769505553947688192719507190647480314898211154271881457542669145952362251063254023596456688904693069011785158707381081647711988657979371090496902863419727614612481436106862628011486355640306125248494742667792459701332278145336370300372481054513574800561323810810854190915899595727878779141227478622842366457711868619279940056112879400260372150334614073091731626230561798132382746385153296338864440727354994453919175321964309501012608502724201463915433350599810123612458180541147113013268924243688188746909214693298402130350215684747092822406672989633058143645080219862796748086312973181380862020889418184877614282995569347078030209522820142984889261749423690362502711192238104345135505887597428730517745634900058066133271323682922065324004979224073990336792328778952238174756435352701757919176178359442953180030748017363431113920536324943709463719668963409108953520692731580925607625787519225033363341480088290586874107944317816053797196503230472479817981264427266758978126449480821547079509481916167541082515640179142584439270609264026954180748234152500744067529121784626914936235486964486747818782528228023682174959259727956489238847306454128814602190684485343913637070015007381983066252980520304950260549434239756901612355098811090842886643573849771346949801906907364622177119785573241816422389003352042967527751185561258970218298071275042726824424511617826603999757386600636272050059366802341440157315890569382218779270898805934208001162512378450482380311438426188836455065661521802089574747483343810448855548723600093660716716264328161037062542114983216206254559643504122764245974805818783329162476699351951161759329923254547053976543135609254688731836947465156827745151246320948057478455243911669527072008676769775612791273629022417853157392094123184025573920514759266035607179770024665621650831619123810118215201942589128109571063089039574087802384911395942433827247320764059566795506901689056778880268103722804366029098347973836800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
45 >>>
46 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
47 905的阶乘为:40542663082438838600504688489492784699312298150705321051067744109646578541672771944078692968872040156073593369319494799284206402526322657814411154007535969684982881094616052719076115577086887837212244891354793303458747227455665568630179878891179349735471330836899697091394853491224295699676710678350395151854477043349887742114352176029705711721529415121837095354334785194507998048783823042778889134133730295122810868153672341644229241100448345750782155857235636796052825736148017121738658427309806385478563733186672318858256269980796853666377700098416410694965402324843467182292828161869274653389738137277008376440537810815952839297435053927966945194696119004278039055617917619998797598975831057018113240729149680128904923457314240926110990259105617339618152229401324781883228439778064953628975484432347632828275673001118559799584552549850610547933044469118224506197786961254797057544951775548154573994195090916854441415295872627927826955713905158098085374074057064666300411885243602936226922080737674901337704898655193824039479902981121067689607623528686462835423577594235273044306676416875204436780143500106956081134131624679676654362124038917539901383944393533577151908013173381113855215087358017293115702860506775998188046361432368338130053800622761156812340986577214982569459236241841548363581680694674958947370875979985797237986979995959181364424037212812412434334043068989570725751164983070293405943783843862262048033598885612614822932939368047559754503913667776104183014133076619780434873575826205303726956119003342370880965290907995240163419370458241052073702497686554181851775700896991834423677230891065146472426148456214271594858084762948628219216985738541600614059810666660376477371231101642607199265998912892041412913515801392193580545365083848771537726375493302312437455966929109361877920457992018001995740060922000167852476646929576102634265288157107439845181481543144398065857135762224497691872322387594002615307048156984757758043160939161812095580814549461158344813327902613658825291473907949933746028596384886642633869137951256334004916322304000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
48 >>>
49 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
50 906的阶乘为:36731652752689587772057247771480462937576942124539020872267376163339800158755531381335295829798068381402675592603462288151491000688848327979856505530827588534594490271722143763482960712840720380514293871567442732933624988074833005178942970275408490860337025738231125564803737263049211903907099874585458007580156201274998294355603071482913374819705650100384408391027315386224246232198143676757673555525159647381266646547227141529671692437006201250208633206655486937223860116950103512295224535142684585243578742267125120885580180602601949421738196289165268089638654506308181267157302314653562835971102752372969589055127256599253272403476158858738052346394683817875903384389833363718910624672102937658410596100609610196787860652326702279056557174749689309694045919837600252386204966438926847987851788895706955342417759739013415178423604610164653156427338289021111402615194986896846134135726308646628044038740752370670123922258060600902611221876798073236865348911095700587668173168030704260221591405148333460611960638181605604579768792100895687326784506916989935328893761300377157378141848833688935219722810011096902209507523251959787048852084379259291150653853620541420899628659935083289152824869146363667562826791619139054358370003457725714345828743364221608071980933838956774207930068035108442817405002709375512806318013637867132297616203876339018316168177714808045665506643020504551077530555474661685825785068162539209415518440590365029029577243067451089137580545783005150389810804567417521073995459698542005176622243817028188018154553562643687588057949635166393178774462904018088757708785012674601987851571187305022704018090501330130064941424795231457166610589079118690156338188463994301088498335378088202122534995015080189520099645316061327383974100765966987013180096196931895068335106037773081861395934940768309808140495195332152074343842118195948986644351070339340499734422278088824647666565000575394908836324083160166369468185630228190528787103810880601758596217981811809460400875079767974895714075360602639973901908324707298226285438983838238608454188007424000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
51 >>>
52 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
53 907的阶乘为:33315609046689456109255923728732779884382286506956891931146510180149198743991266962871113317626848021932226762491340295353402337624785433477729850516460622800877202676451984393479045366546533385126464541511670558770797864183873535697301274039795501210325682344575630887276989697585635196843739586249010412875201674556423452980531985835002430961473024641048658410661775055305391332603716314819209914861319800174808848418335017367412225040364624533939230318436526652062041126073743885651768653374414918815925919236282484643221223806559968125516544034272898157302259637221520409311673199390781492225790196402283417273000421735522718069952876084875413478179978222813444369641578860893051936577597364456178410663252916448486589611660318967104297357497968203892499649292703428914287904560106651124981572528406208495572908083285167566830209381419340412879595828142148042171981853115439443661103761942491635943137862400197802397488060965018668378242255852425836871462363800433015033063403848764020983404469538448775048298830716283353850294435512388405393547773709871343306641499442081741974656892155864244288588680064890304023323589527526853308840531988177073643045233831068755963194561120543261612156315751846479483899998559122303041593136157222911666670231348998521286706991933794206592571707843357635386337457403590115330438369545488993937896915839489612764537187330897418614525219597627827320213815518149043987056823423062939875225615461081329826559462178137847785555025185671403558399742647691614113881946577598695196375142044566532466180081317824642368560319095918613148437853944406503241868006495864002981375066885655592544408084706427968901872289274931650115804294760651971798736936842831087267990187925999325139240478677731894730378301667623937264509394732057220954347250617228826979941176260185248286112991276856995983429142166261931429864801203725730886426420797781833259121006226563955433574455521883182314545943426270897107644366616968809609903156468705795046769709503311180583593697349553230412666352066594456329030850509519491240893158341282417867948522733568000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
54 >>>
55 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
56 908的阶乘为:30250573014394026147204378745689364135019116148316857873481031243575472459544070402286970892405178003914461900342136988180889322563305173597778704268946245503196500030218401829278973192824252313694829803692596867363884460678957170413149556828134315098975719568874672845647506645407756758734115544314101454890683120497232495306323043138182207313017506374072181836880891750217295330004174413855842602694078378558726434363848195769610300336651079076816821129140366200072333342474959448171805937263968746284860734666544496056044871216356451057969021983119791526830451750597140531654999265046829594941017498333273342883884382935854628007517211485066875438187420226314607487634553605690891158412458406926209996882233648135225823367387569622130702000608155129134389681557774713454173417340576839221483267855792837313980200539622932150681830118328761094894673011953070422292159522628819014844282215843782405436369179059379604576919159356236950887443968314002659879287826330793177650021570694677731052931258340911487743855338290385285296067347445248672097341378528563179722430481493410221712988458077524733814038521498920396053177819290994382804427203045264782867885072318610430414580661497453281543837934702676603371381198691683051161766567630758403793336570064890657328329948675885139586055110721768732930794411322459824720038039547304006495610399582256568390199766096454856101988899394646067206754144490479331940247595668141149406704858838661847482515991657749165789283962868589634431026966324103985615404807492459615238308628976466411479291513836584775270652769739094100738781571381521104943616149898244514707088560732175278030322540913436595762900038661637938305150299642671990393253138653290627239335090636807387226430354639380560415183497914202535036174530416707956626547303560443774897786588044248205443790596079386152352953661086965833738317239492982963644875190084385904599281873653720071533685605613869929541607716631053974573741084888207679125792066073584861902466896229006551969903077193394333214701047676467766346760012262643698046730987773884435424097258642079744000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
57 >>>
58 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
59 990的阶乘为:420968553994851716755941042336061773138320417349747921744064330687849165290658850058183317539527295977244346884077515263571000940056443561946724485943422740497560544110790490107737597696514192626989313274402950735145629649995037631443067122394899999473743675601307052514135495583777342036266502554931810464462535302234781162356697954018211644335510147382538325885822081947113869936878333204706631118889095325118221225776142676572666796561588678484861916879170404815427754543771943860669211386012371911524308983166391503370286778831566895852621781774522032822836488190235307097044436365939557250198604564997403978748797568770986228781783940317292447078843972286987300295269667694986875267217463492445523497215698339307178310922640052867859837039542970208828302088872152359286780153152896627082936566922392609273304631238866872020621167990138402391150661739848045302917868173612803553319189291840337937929206441296014822149179234240643493626462514638582196095921282033804847539048748746613594242878212070639588086027217842260389389532501326677372350065147978282230054364116328102258593483997442928070465249037393659111916419930070813189028446375014634780494728037972888449912323875219038605023159809790504115129047512641962699059257729278548806552739884279560802178179856548962275603878832743127976737197146761013269276343583938043224013182822627517991450767740943381085905198065287394552026151563316282249548875879093433082680366013593905822998490950039693146331749545121105548385669372940283973731007988800094345606904914784082288508559009494252685126250437303525233168376479925327975340112038721091048991036220807678885259949104526782444958057854405796942162823036098892575822327210496280654148523636843904946453083797395622678031935271256616179969657812171046940805849062759597687149092457271689064385695507390497184279146292009985792522820095020092066966434592963937819702054254759611038921707413076423024803141677813236474221641776839591403263365382345862886004650896998449830178906681094638351550927194869199241793266675799136541142947589924667771121400085282203049029921603439809556592806655731273837487358536015933685246426556113586080548528117598910491614882685353331028502089003771942028384846608291251161306660319532797657599686056732476206878294174606679027178094258553193743620505600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
60 >>>
61 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
62 991的阶乘为:417179837008898051305137572955037217180075533593600190448367751711658522803042920407659667681671550313449147762120817626198861931595935569889203965569931935833082499213793375696767959317245564893346409454933324178529318983145082292760079518293345899478479982520895289041508276123523345957940104031937424170282372484514668131895487672432047739536490556056095480952849683209589845107446428205864271438819093467192157234744157392483512795392534380378498159627257871172088904752877996365923188483538260564320590202317893979839954197822082793789948185738551334527430959796523189333171036438646101234946817123912427342940058390652047352722747884854436815055134376536404414592612240685731993389812506321013513785740757054253413706124336292392049098506187083476948847370072302988053199131774520557439190137820091075789844889557717070172435577478227156769630305784189412895191607360050288321339316588213774896487843583324350688749836621132477702183824352006834956331057990495500603911197310007894071894692308162003831793252972881680045885026708814737275998914561646477689983874839281149338266142641465941717831061796057116179909172150700175870327190357639503067470275485631132453863112960342067257577951371502389578092886085028185034767724409715041867293765225321044754958576237840021615123443923248439824946562372440qingyang9852856491682600834997064177223870329527710831274890656132051282699808001057916199246435709302935996181592184936242719471560670591504531489335908014763799215015598450198348583821417967428916900893496496442770551025547911981978408804410960114183367793506069861091606000023562051030372601229550116894820409775292609562586041402953435333716144769683357628774002542639926265601814128261186924112309801935006043219062073929647853815306634349930891861507518338596421194761307964750625156243862806224247823982709620633975381895920390114714164911238363736681627262379324735766466774539571412046358735217579913402712917345953647000848035080633995093904750120030609038925463781707296520964786606386968850115376448617127275716944312272661061615345761181307484514663221588652309008851270583471395829692372949972309191790282079208717108563805823591364540520297190348741185151049245570202737994550129382988816629900854900376657002478681288882221883921016389527035218915933491410226214999927921049600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
63 >>>
64 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
65 992的阶乘为:413842398312826866894696472371396919442634929324851388924780809697965254620618577044398390340218177910941554580023851085189271036143168085330090333845372480346417839220083028691193815642707600374199638179293857585101084431279921634417998882146999132282652142660728126729176209914535159190276583199681924776920113504638550786840323771052591357620198631607646717105226885743913126346586856780217357267308540719454619976866204133343644693029394105335470174350239808202712193514854972394995802975669954479806025480699350828001234564239506131439628600252642923851211512118151003818505668147136932425067242586921127924196537923526830973900965901775601320534693301524113179275871342760246137442694006270445405675454830997819386396475341602052912705718137586809133256591111724564148773538720324392979676616717530347183526130441255333611056092858401339515473263337915897592030074501169886014768602055508064697315940834657755883239837928163417880566353757190780276680409526571536599079907731527830919319534769696707801138906949098626605517946495144219377790923245153305868464003840566900143560013500334214184088413301688659250469898773494574463364572834778387042930513281746083394232208056659330719517327760530370461468142996347959554489582614437321532355415103518476396918907627937301442202456371862452306346989873460642836654033639749140028317087663806079366891489144624691530882994872438209537049452869652464223628512508212139447456752777715788185226772495237421220750645688821295473662596761795150846623689485565686348524471228386617343528686122581533975672433269900851158021302202873152023373554622129620419713715959661846497090268686085353071729807851046415611525890767743810522298806855476999615235097428719411323519525994873309577338210670984784181275131444726615458191887649825203217501032620154993911903774453841390847943668903578840753026993796451591948456826788174244280290137880335040343254840749987865335839274095491214007186017824841250799988923133153512119070364166614060071453638148797068313535873099314453437028190257511208757774479773122422995091857024638545915815943090536780460418803624663054833966372530718255959822575047371695295377002633624196134812825951255669840851605641116090593728347924906096861648061173643746458851838571164108849648258410818937164606023478944405279928497681203200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
66 >>>
67 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
68 993的阶乘为:410945501524637078826433597064797141006536484819577429202307344030079497838274247005087601607836650665564963697963684127592946138890165908732779701508454872983992914345542447490355458933208647171580240712038800582005376840260962182977072889971970138356673577662103029842071976445133413075944647117284151303481672710106080931332441504655223218116857241186393190085490297543705734462160748782755835766437380934418437637028140704410239180178188346598121883129788129545293208160250987588230832354840264798447383302334455372205225922289829588519551200050874423384253031533323946791776128470106973898091771888812680028727162158062143157083659140463172111290950448413444387020940243360924414480595148226552287835726647180834650691700014210838542316778110623701469323794973942492199732123949282122228818880400507634753241447528166546275778700208392530138864950494550486308885863979661696812665221841119508244434729248815151592057159062666273955402389280890444814743646659885535842886348377407136102884298026308830846530934600454936219279320869678209842146386782437232727384755813682931842555093405831874684799794408576838635716609482080112442121020824934938333629999688773860810472582600262715404480706466206657868237865995373523837608155536136260281628927197793847062140475274541740332107039177259415140202560944346418336797455404270896048118868050159436811323248720612318690166813908331142070290106699564896974063112920654654471324555508271777667930185087770759272205391168999546405346958584462584790697323659166726544084799929787911022123985319723463237842726237011545199915153087453039959209939739774713076775719947944213571610636805282755600227699196089090702245209532369603848642715207488660617928451746718375444254889312909196410296843196287890692006205524613529149984544436276426794978525391813908954520448032664501112008063221253788867755804839876430804817629000657024570328106915172695060852056864737950278488399176822775509135715700067362044389000671221437534236871617447761650953462681755488835341121987619252262968992925708630296470058414710566034126214025466076094405231488903022997195871999290413450128607923003228168103817022040093428309363615188826761869136169596880151965644401628277959572249489431754183616524745428240233639875701165960087700720601943204604453781314591794442968998197434777600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
69 >>>
70 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
71 Traceback (most recent call last):
72 File "C:/Users/qingyang/Desktop/递归求阶乘.py", line 8, in <module>
73 print('{}的阶乘为:{}'.format(num,func(num)))
74 File "C:/Users/qingyang/Desktop/递归求阶乘.py", line 5, in func
75 return num * func(num-1)
76 File "C:/Users/qingyang/Desktop/递归求阶乘.py", line 5, in func
77 return num * func(num-1)
78 File "C:/Users/qingyang/Desktop/递归求阶乘.py", line 5, in func
79 return num * func(num-1)
80 [Previous line repeated 990 more times]
81 File "C:/Users/qingyang/Desktop/递归求阶乘.py", line 3, in func
82 if num == 0 or num == 1:
83 RecursionError: maximum recursion depth exceeded in comparison
84 >>>
使用setrecursionlimit() 函数后,求1200的阶乘:
测试源码↓
1 import sys
2 # 设置递归深度100 0000
3 sys.setrecursionlimit(1000000)
4
5 # 递归求阶乘函数
6 def func(num):
7 if num == 0 or num == 1:
8 return 1
9 return num * func(num-1)
10
11 num = 1200 #求1200的阶乘成功!
12 print('{}的阶乘为:{}'.format(num,func(num)))
运行过程及结果
1 Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
2 Type "help", "copyright", "credits" or "license()" for more information.
3 >>>
4 ================== RESTART: C:/Users/qingyang/Desktop/递归求阶乘.py ==================
5 1200的阶乘为:63507890863456767124026223135865363993920361927048401849732641593094118304334664838518489279761202858110442656198961423888337785958374054395875090008179394602121729048755527506222388620438398012556615061940909904115015164483186340282667999681601237310950712039020066375755766076440811303940538840103890741951917564190561275396873204122937083746098640414588850116107225675520412584415359155103495655208534054658149146203739758027326003354866456731097561086620889903717246818807490131678158299401901235057514328881677204150054888001843870188616756694468379677033228639349974219800691760236674808772459147849761319772233349285258185354881462706759016416844508026593468378544952254302304068325633905800617846092173316334920423976973219453332902616477256306327233355305287468380745909208415980513577159243876582648875144838454034618802839634184994308400967856236366107563273185702779295861081100580569890291839403080692152562549750464518941296615589072961901445867101610266912853288908304184351248084449986727837714164724886934713403840928394071581648559037175552279054765268191336562999918827853842984694855922999162907679561931097787771324637893456823082524179018246504955405803121771121360752264414781039718455098118457351815525441962553663917394824907930942411732997508709279776658345871568600666174624864566227235992187598660130953640936345350873339744682415089563501119516442999676532779307108492176925439731860347372511112433604790992759325197681339726349322054878255119874625965850196940660648685990671210212572225564228346159053754713667934756177880507837712524591347066833930606771513277248914447108453842428044821209156242191481316239948456314071437111282270756494813443336707372525017024678559199590515860360962136467183581351799081925904581530470013515629221769984825958416224944982867113744499809912183809641523923237883957287541972186804232110652653006828947123647231393100716154785438044926596489481061404444949958850342025214038700582411675847296195285368313134928856220726124462351334098415059073275239953735718425086123827271757497468773885713767925771130546617686545287963633612898691413349119011210341661317374809835898724194978360650558823413986166888783608735534361048197825992486540004672367499044204531085976637277941222039312514997381320707709813267213844400528265917241905520030567187527121305903232698469502319828288497642071789544559470672876896682816986176852553613912123976135208540633191577134212923764719883260091745561140753211368159375154389149276613870478944559132118639123292629448861220166692668795869998815536697843168702435154762819119055700781916899847331952260368608062686678197902264191722301178373738543720223581116366378070649868986724225640819288848872888236573440404740650405057575066492961346462735749115329710082527101393654163731209311139319445720303577449977552535888706315264756010489543514382828926003274237925093166062460951592960000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
6 >>>
总结:
Python3.7.1版本测试得到—— 0-993可得到递归值,
大于等于994以上数据的递归值需要使用以下两句在源码中进行设置,
1 import sys 2 # 设置递归深度:你想设置的值 3 sys.setrecursionlimit(你想设置的值)
但不建议,
毕竟递归不是一个完美的小可爱(引自CSDN的博文)
1.递归由于是函数调用自身,而函数调用是有时间和空间的消耗的:每一次函数调用,都需要在内存栈中分配空间以保存参数、返回地址以及临时变量,而往栈中压入数据和弹出数据都需要时间。(影响效率)
2.递归中很多计算都是重复的,由于其本质是把一个问题分解成两个或者多个小问题,多个小问题存在相互重叠的部分,则存在重复计算,如fibonacci斐波那契数列的递归实现。(印影响效率)
3.调用栈可能会溢出,其实每一次函数调用会在内存栈中分配空间,而每个进程的栈的容量是有限的,当调用的层次太多时,就会超出栈的容量,从而导致栈溢出。(影响性能)
探索完毕,虽然只有一小部分,暂时没其他的想法了,后续若有其他问题再来补充!欢迎提出您宝贵的建议及问题,互相学习,加油每一天!