Algorithms
Logic
Mathematics
Numbers
Recursion based implementations are not a great idea to count factorials. :)
def factorial(x): result = 1 for i in range(2, x + 1): result *= i return result
- def factorial(x):
return x * factorial(x-1) if x > 0 else 1- result = 1
- for i in range(2, x + 1):
- result *= i
- return result
test.assert_equals(factorial(0), 1, "Wrong calculation!") test.assert_equals(factorial(6), 720, "Wrong calculation!") test.assert_equals(factorial(10), 3628800, "Wrong calculation!") test.assert_equals(factorial(575), 7390441777119141443401630031209136886962206496218349477250383820672630038291650027199276041379604977613822300356137582483671094754462433862060420213421726875538872643113190344171733528305637229616079272474914553779262327068944082289063739293899147664296334411304178909628922640478036480043123897889808217914505951696824443963735666011997300563726162488291847603301888156744760938159790313291832262355081128621083112904233051689362142319752603260694085989673295856537174842402003481382867705729452057147119996957618169629160344929741174366305002153417530459036888195944818660236990764932793659814680008188556982327000036545187924637634043397381072990487999060488419647054718909159182929734848951186267933465552241358089972644314912779875596613356687641502422935454290075104768112364758258035567841598815922483405788774534926446224281594855206880491390490360997335630051293435175879442130205423487684712608957346067905111895580651990754324329721972918043847654333624842456491357161085321228947774542348951852384127552202491794571382429220625253786820865579682842442921219893791367295048327627070500582137100660494127735634830649073758502862713646164779092559240920618025632380925462715374385034690560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!") test.assert_equals(factorial(1500), 48119977967797748601669900935813797818348080406726138081308559411630575189001095591292230585206733851868464009619343585194052091124618166270271481881393331431627962810299844149333789044689395510487167879769325303699470467829234399263326545652860748605075746366928323606645492277541120083438086727369377887676000211405318480244354207419604864176969950581435222198851194568984095705945549589054568321792338919149442985919957734792959402499096845643020401869381175603964424333222114125974374817804242633309769804293952870034619354125014210045647664063240162007560108665290568646128342557147350985358724154623253371867470765120422073867963935775258692109753041762094343569050497470353531764481503174750911858230906998361066084787758316110585736013365377431860738572261325738233656835271947352695180865573043834027955539012765489372645042504406597752357481931532872356635411224578334040522294746402829585458478708778346379431862368824819009177091444034885941394319343910223168655869761799669075059527608502465593181398566214786801211651657222004123456498258513120359126022843038535083709796101565934859483203933443308601475813108363074118562404412420191947127585482919172173045961122122701434297870691932154082986945954748251105782181586397275820342101470457300633590139512919549474113721711616912519714191760699935509810254849967087635936181176363954224186031346682928878492872249485456690138831610135377916327940503701400290125509132140782614640495733518048670983360134097860364762638658894873174499870133559364805443430831459505987809215393353387232078177562975021460595422358573128085417162336030235138652735438053034531962620811566019896879275257163988352090874930346115518331202927263708446729394381879888839549731876978682249320628599631628662375508826209854754631984276392670919216923002770077734756077549035942976209159416211581439461484509549370357486770276807687544580164314647595031368948490282897173328013518435758700056425922638411889496527975846052717958044813737086806600171993703579485864029383208714528950303253881360812631162134750100307772634337467012820470715650810714689905121432259528505483053930402217400686061612471659630192434864094539828085677465383026128353771071152304197549798870706139893609140045659756285435787771636258253666592102151236142132724425850991205720020493660580896600891888594659612927724357866265934517615841298789154462249169688860092640284756382431746120357767933119589280468687348061788072986362788582227019465263474828590646048451070702923434422714349595857654843699542321849363652767771978314681013589442955219879702008068934096624650625769705233333462826013860098698155180331145365652453482955497979915586438474687345677874451117702250441711504844638414485210092261397271970571029038581873069951161330495772310508760528249706514238384269808639507080418298318311361373628512041716415196868334254119137139589149597210032153545941114666530498906529240798164804007394775927836045668573993316428972539932745757171947402454257142633700815922407278403640595355142075599446056337986717212316223257763412164180899532722039383244462511410346646148863397237096276822656157561194665545757017429842404840309758925618650507921043007241637877939825811059339138925526124514467627126548126795078784022672860886251974581362141782786407402896309678008909663263987018538107050886193489012497405005820727271232733728141775132722013860591169620692789290456794698409808557447756701311883266010859016027592252397754508251628808293537776536569608111330584797160694847898923196743970244451842702266403326317319092117151143971679500042590269255093130215984418097418435474300467281949798227102529873732749027992079700287275900856241172902880909546551703263202853584498085358955307673717177961902081098618729046348849060249600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")
- test.assert_equals(factorial(0), 1, "Wrong calculation!")
- test.assert_equals(factorial(6), 720, "Wrong calculation!")
- test.assert_equals(factorial(10), 3628800, "Wrong calculation!")
test.assert_equals(factorial(575), 7390441777119141443401630031209136886962206496218349477250383820672630038291650027199276041379604977613822300356137582483671094754462433862060420213421726875538872643113190344171733528305637229616079272474914553779262327068944082289063739293899147664296334411304178909628922640478036480043123897889808217914505951696824443963735666011997300563726162488291847603301888156744760938159790313291832262355081128621083112904233051689362142319752603260694085989673295856537174842402003481382867705729452057147119996957618169629160344929741174366305002153417530459036888195944818660236990764932793659814680008188556982327000036545187924637634043397381072990487999060488419647054718909159182929734848951186267933465552241358089972644314912779875596613356687641502422935454290075104768112364758258035567841598815922483405788774534926446224281594855206880491390490360997335630051293435175879442130205423487684712608957346067905111895580651990754324329721972918043847654333624842456491357161085321228947774542348951852384127552202491794571382429220625253786820865579682842442921219893791367295048327627070500582137100660494127735634830649073758502862713646164779092559240920618025632380925462715374385034690560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")- test.assert_equals(factorial(575), 7390441777119141443401630031209136886962206496218349477250383820672630038291650027199276041379604977613822300356137582483671094754462433862060420213421726875538872643113190344171733528305637229616079272474914553779262327068944082289063739293899147664296334411304178909628922640478036480043123897889808217914505951696824443963735666011997300563726162488291847603301888156744760938159790313291832262355081128621083112904233051689362142319752603260694085989673295856537174842402003481382867705729452057147119996957618169629160344929741174366305002153417530459036888195944818660236990764932793659814680008188556982327000036545187924637634043397381072990487999060488419647054718909159182929734848951186267933465552241358089972644314912779875596613356687641502422935454290075104768112364758258035567841598815922483405788774534926446224281594855206880491390490360997335630051293435175879442130205423487684712608957346067905111895580651990754324329721972918043847654333624842456491357161085321228947774542348951852384127552202491794571382429220625253786820865579682842442921219893791367295048327627070500582137100660494127735634830649073758502862713646164779092559240920618025632380925462715374385034690560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")
- test.assert_equals(factorial(1500), 48119977967797748601669900935813797818348080406726138081308559411630575189001095591292230585206733851868464009619343585194052091124618166270271481881393331431627962810299844149333789044689395510487167879769325303699470467829234399263326545652860748605075746366928323606645492277541120083438086727369377887676000211405318480244354207419604864176969950581435222198851194568984095705945549589054568321792338919149442985919957734792959402499096845643020401869381175603964424333222114125974374817804242633309769804293952870034619354125014210045647664063240162007560108665290568646128342557147350985358724154623253371867470765120422073867963935775258692109753041762094343569050497470353531764481503174750911858230906998361066084787758316110585736013365377431860738572261325738233656835271947352695180865573043834027955539012765489372645042504406597752357481931532872356635411224578334040522294746402829585458478708778346379431862368824819009177091444034885941394319343910223168655869761799669075059527608502465593181398566214786801211651657222004123456498258513120359126022843038535083709796101565934859483203933443308601475813108363074118562404412420191947127585482919172173045961122122701434297870691932154082986945954748251105782181586397275820342101470457300633590139512919549474113721711616912519714191760699935509810254849967087635936181176363954224186031346682928878492872249485456690138831610135377916327940503701400290125509132140782614640495733518048670983360134097860364762638658894873174499870133559364805443430831459505987809215393353387232078177562975021460595422358573128085417162336030235138652735438053034531962620811566019896879275257163988352090874930346115518331202927263708446729394381879888839549731876978682249320628599631628662375508826209854754631984276392670919216923002770077734756077549035942976209159416211581439461484509549370357486770276807687544580164314647595031368948490282897173328013518435758700056425922638411889496527975846052717958044813737086806600171993703579485864029383208714528950303253881360812631162134750100307772634337467012820470715650810714689905121432259528505483053930402217400686061612471659630192434864094539828085677465383026128353771071152304197549798870706139893609140045659756285435787771636258253666592102151236142132724425850991205720020493660580896600891888594659612927724357866265934517615841298789154462249169688860092640284756382431746120357767933119589280468687348061788072986362788582227019465263474828590646048451070702923434422714349595857654843699542321849363652767771978314681013589442955219879702008068934096624650625769705233333462826013860098698155180331145365652453482955497979915586438474687345677874451117702250441711504844638414485210092261397271970571029038581873069951161330495772310508760528249706514238384269808639507080418298318311361373628512041716415196868334254119137139589149597210032153545941114666530498906529240798164804007394775927836045668573993316428972539932745757171947402454257142633700815922407278403640595355142075599446056337986717212316223257763412164180899532722039383244462511410346646148863397237096276822656157561194665545757017429842404840309758925618650507921043007241637877939825811059339138925526124514467627126548126795078784022672860886251974581362141782786407402896309678008909663263987018538107050886193489012497405005820727271232733728141775132722013860591169620692789290456794698409808557447756701311883266010859016027592252397754508251628808293537776536569608111330584797160694847898923196743970244451842702266403326317319092117151143971679500042590269255093130215984418097418435474300467281949798227102529873732749027992079700287275900856241172902880909546551703263202853584498085358955307673717177961902081098618729046348849060249600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")
Algorithms
Logic
Mathematics
Numbers
The variable x can be from 0 to +∞.
def factorial(x): result = 1 for i in range(2, x+1): result *= i return result
- def factorial(x):
return 1 if x == 1 else x*factorial(x-1)- result = 1
- for i in range(2, x+1):
- result *= i
- return result
test.assert_equals(factorial(0), 1, "Wrong calculation!") test.assert_equals(factorial(6), 720, "Wrong calculation!") test.assert_equals(factorial(10), 3628800, "Wrong calculation!") test.assert_equals(factorial(575), 7390441777119141443401630031209136886962206496218349477250383820672630038291650027199276041379604977613822300356137582483671094754462433862060420213421726875538872643113190344171733528305637229616079272474914553779262327068944082289063739293899147664296334411304178909628922640478036480043123897889808217914505951696824443963735666011997300563726162488291847603301888156744760938159790313291832262355081128621083112904233051689362142319752603260694085989673295856537174842402003481382867705729452057147119996957618169629160344929741174366305002153417530459036888195944818660236990764932793659814680008188556982327000036545187924637634043397381072990487999060488419647054718909159182929734848951186267933465552241358089972644314912779875596613356687641502422935454290075104768112364758258035567841598815922483405788774534926446224281594855206880491390490360997335630051293435175879442130205423487684712608957346067905111895580651990754324329721972918043847654333624842456491357161085321228947774542348951852384127552202491794571382429220625253786820865579682842442921219893791367295048327627070500582137100660494127735634830649073758502862713646164779092559240920618025632380925462715374385034690560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")
- test.assert_equals(factorial(0), 1, "Wrong calculation!")
- test.assert_equals(factorial(6), 720, "Wrong calculation!")
test.assert_equals(factorial(10), 3628800, "Wrong calculation!")- test.assert_equals(factorial(10), 3628800, "Wrong calculation!")
- test.assert_equals(factorial(575), 7390441777119141443401630031209136886962206496218349477250383820672630038291650027199276041379604977613822300356137582483671094754462433862060420213421726875538872643113190344171733528305637229616079272474914553779262327068944082289063739293899147664296334411304178909628922640478036480043123897889808217914505951696824443963735666011997300563726162488291847603301888156744760938159790313291832262355081128621083112904233051689362142319752603260694085989673295856537174842402003481382867705729452057147119996957618169629160344929741174366305002153417530459036888195944818660236990764932793659814680008188556982327000036545187924637634043397381072990487999060488419647054718909159182929734848951186267933465552241358089972644314912779875596613356687641502422935454290075104768112364758258035567841598815922483405788774534926446224281594855206880491390490360997335630051293435175879442130205423487684712608957346067905111895580651990754324329721972918043847654333624842456491357161085321228947774542348951852384127552202491794571382429220625253786820865579682842442921219893791367295048327627070500582137100660494127735634830649073758502862713646164779092559240920618025632380925462715374385034690560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, "Wrong calculation!")
# caesar cipher shifted of 13 characters with maketrans for python 2.7 from string import maketrans as mktr, ascii_uppercase as asuc def caesar(s): return s.translate(mktr(asuc, asuc[13:] + asuc[:13]))
- # caesar cipher shifted of 13 characters with maketrans for python 2.7
from string import maketransimport string- from string import maketrans as mktr, ascii_uppercase as asuc
- def caesar(s):
print(s)s=s.translate(maketrans(string.ascii_uppercase, string.ascii_uppercase[13:]+string.ascii_uppercase[:13]))return s- return s.translate(mktr(asuc, asuc[13:] + asuc[:13]))