1!+2!+3!+4!+...+100! 多语言版 纯娱乐
需要知道结果 就试了试我在linux下装的这些软件 总结一下C语言 当然简单起见用了浮点数。。。
程序代码:
//gcc -Wall -O3 a.c #include <stdio.h> int main(void) { double sum = 0.0; double f = 1.0; int i = 1; int j = 1; for (i = 1; i <= 100; i++) { f = 1.0; for (j = 1; j <= i; j++) { f *= j; } sum += f; } printf("%lf\n", sum); return 0; }
time ./a.out
94269001683709925420719656762546985486727416355083585338645224398394941811201963496134808913580530755456740099840414271964178383003109887091841526063934996480.000000
real 0m0.002s
user 0m0.000s
sys 0m0.000s
94269001683709925420719656762546985486727416355083585338645224398394941811201963496134808913580530755456740099840414271964178383003109887091841526063934996480.000000
real 0m0.002s
user 0m0.000s
sys 0m0.000s
不用想 这个肯定快 但要是写成大数运算就稍微麻烦点 这里就当成浮点写了。。
python 实际是python 2.7
程序代码:
#!/usr/bin/python2 i = 1 f = 0 sum = 0 while i <= 100: sum += reduce(lambda x, y: x * y, xrange(1, i + 1)) i += 1 print sum
time ./a.py
94269001683709979260859834124473539872070722613982672442938359305624678223479506023400294093599136466986609124347432647622826870038220556442336528920420940313
real 0m0.038s
user 0m0.033s
sys 0m0.007s
94269001683709979260859834124473539872070722613982672442938359305624678223479506023400294093599136466986609124347432647622826870038220556442336528920420940313
real 0m0.038s
user 0m0.033s
sys 0m0.007s
效率也挺好的 关键直接就是大数运算
bc 一个linux下的命令行计算程序 支持类C的语法 大数运算 我非常喜欢的一个小工具 windows下我也用 试试它的表现
程序代码:
i = 1 j = 1 f = 1 sum = 0 for (i = 1; i <= 100; i++) { f = 1 for (j = 1; j <= i; j++) { f *= j } sum += f } sum
time cat a.bc | bc
94269001683709979260859834124473539872070722613982672442938359305624\
67822347950602340029409359913646698660912434743264762282687003822055\
6442336528920420940313
real 0m0.011s
user 0m0.003s
sys 0m0.007s
94269001683709979260859834124473539872070722613982672442938359305624\
67822347950602340029409359913646698660912434743264762282687003822055\
6442336528920420940313
real 0m0.011s
user 0m0.003s
sys 0m0.007s
很好 很好 虽然结果输出比较难看 但结果很好 而且写起来和C的感觉差不多
matlab 我这里用的是开源替代GNU Octave 对我这个没学过matlab的人来说 这玩意就相当于matlab了
sum(factorial(1:100))
神一样的一行代码
time ./a.m
GNU Octave, version 3.6.3
Copyright (C) 2012 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'.
Octave was configured for "x86_64-unknown-linux-gnu".
Additional information about Octave is available at http://www.
Please contribute if you find this software useful.
For more information, visit http://www.
Read http://www. to learn how to submit bug reports.
For information about changes from previous versions, type `news'.
ans = 9.4269e+157
real 0m0.181s
user 0m0.160s
sys 0m0.017s
GNU Octave, version 3.6.3
Copyright (C) 2012 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'.
Octave was configured for "x86_64-unknown-linux-gnu".
Additional information about Octave is available at http://www.
Please contribute if you find this software useful.
For more information, visit http://www.
Read http://www. to learn how to submit bug reports.
For information about changes from previous versions, type `news'.
ans = 9.4269e+157
real 0m0.181s
user 0m0.160s
sys 0m0.017s
我这还有R 不过考虑到它的长处不是在计算上面 不测试了
总结
1 python内置的大数运算不错 直接写就是大数运算了
2 octave或者说是matlab的语法好简洁 一行解决问题 但受限于octave 貌似没法做高精度的运算
3 bc好厉害啊 以后就用它了。。
这里比的不大公平 因为程序启动的时间也统计了 但这个比较也是贴近实际的
以上的几个的几个东东我都是初学 如有不当 欢迎批评
/************************************************************************/
改成到1000! 让python和bc比比速度。。
time ./a.py
402790050531223457680670558292183044930441094551528966419334642920075902370082550849878938011122874840322664395727767319128483846892726398968790560901035018977865543415443376965144616549510538851077440410165819782073475583082079396504111380884916810768467471317168913183948275897490803646698793846190852594417739671786914563472162093180956284720843607311204509397095907530114810381703803860648328656344389023077975647748628886817466804724858587747840936784462436173977429991182753207302348016538494642001301432395279726745538178727708503090870745742249581124437084158031806471602854863650302741825130214652788342621283870175833906572656890104432413095171095723550851047360182700817666447833450097928957386876271765554137407111747666512309980678539886786878262902584539118231519316835523885400137767203426696953126303292828224235349134630280084480064650497764755473824102259453127730077777531063115138764952527597098445380046443997540925880086936317933421768424925084707414812932329166745886234137226923024813461900363225999464524935227947647167947434411637139203517404371214232998239493954562323327076903915931443305369962856388265027346155623332543302943446674383336900484885947609695640139797208838438493998636482572950930777919644406154624084228042985270573774543689565298092301062689807689071120317578510142181892798247199071452022029172707400688758980305702923813291544851703914211657597582497807598793035565731652297925769886549168450867091163868402442018371341022143349040941897422377931540131712376088451926646802255265721820332564453961091792448710886674897251899281432897966817352381951694370184383299109654078410356489144960977274480320655205474842571310858767154579079644215514199003832599232631020684528203955813111140473074686810426051322892063650122841004997271652977702547274337834459757112850838135183696311712170990224739928697694772853140476802033735594574707189941820656793611572800671948131128109558012358679446259496244143021997936801273888393102412918463505832701613704464047394891693567824946365095976820092895872874833067345472399549213736568277881574159120670765215962463587184455433922702506813953637742383768964226648365610070168839324749896910992390804658988479107678562550844858514512762455637677863067821690083222834405613388709119188719479181528190836888908152845729021513491658586509889455613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940313
real 0m0.350s
user 0m0.337s
sys 0m0.010s
402790050531223457680670558292183044930441094551528966419334642920075902370082550849878938011122874840322664395727767319128483846892726398968790560901035018977865543415443376965144616549510538851077440410165819782073475583082079396504111380884916810768467471317168913183948275897490803646698793846190852594417739671786914563472162093180956284720843607311204509397095907530114810381703803860648328656344389023077975647748628886817466804724858587747840936784462436173977429991182753207302348016538494642001301432395279726745538178727708503090870745742249581124437084158031806471602854863650302741825130214652788342621283870175833906572656890104432413095171095723550851047360182700817666447833450097928957386876271765554137407111747666512309980678539886786878262902584539118231519316835523885400137767203426696953126303292828224235349134630280084480064650497764755473824102259453127730077777531063115138764952527597098445380046443997540925880086936317933421768424925084707414812932329166745886234137226923024813461900363225999464524935227947647167947434411637139203517404371214232998239493954562323327076903915931443305369962856388265027346155623332543302943446674383336900484885947609695640139797208838438493998636482572950930777919644406154624084228042985270573774543689565298092301062689807689071120317578510142181892798247199071452022029172707400688758980305702923813291544851703914211657597582497807598793035565731652297925769886549168450867091163868402442018371341022143349040941897422377931540131712376088451926646802255265721820332564453961091792448710886674897251899281432897966817352381951694370184383299109654078410356489144960977274480320655205474842571310858767154579079644215514199003832599232631020684528203955813111140473074686810426051322892063650122841004997271652977702547274337834459757112850838135183696311712170990224739928697694772853140476802033735594574707189941820656793611572800671948131128109558012358679446259496244143021997936801273888393102412918463505832701613704464047394891693567824946365095976820092895872874833067345472399549213736568277881574159120670765215962463587184455433922702506813953637742383768964226648365610070168839324749896910992390804658988479107678562550844858514512762455637677863067821690083222834405613388709119188719479181528190836888908152845729021513491658586509889455613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940313
real 0m0.350s
user 0m0.337s
sys 0m0.010s
time cat a.bc | bc
40279005053122345768067055829218304493044109455152896641933464292007\
59023700825508498789380111228748403226643957277673191284838468927263\
98968790560901035018977865543415443376965144616549510538851077440410\
16581978207347558308207939650411138088491681076846747131716891318394\
82758974908036466987938461908525944177396717869145634721620931809562\
84720843607311204509397095907530114810381703803860648328656344389023\
07797564774862888681746680472485858774784093678446243617397742999118\
27532073023480165384946420013014323952797267455381787277085030908707\
45742249581124437084158031806471602854863650302741825130214652788342\
62128387017583390657265689010443241309517109572355085104736018270081\
76664478334500979289573868762717655541374071117476665123099806785398\
86786878262902584539118231519316835523885400137767203426696953126303\
29282822423534913463028008448006465049776475547382410225945312773007\
77775310631151387649525275970984453800464439975409258800869363179334\
21768424925084707414812932329166745886234137226923024813461900363225\
99946452493522794764716794743441163713920351740437121423299823949395\
45623233270769039159314433053699628563882650273461556233325433029434\
46674383336900484885947609695640139797208838438493998636482572950930\
77791964440615462408422804298527057377454368956529809230106268980768\
90711203175785101421818927982471990714520220291727074006887589803057\
02923813291544851703914211657597582497807598793035565731652297925769\
88654916845086709116386840244201837134102214334904094189742237793154\
01317123760884519266468022552657218203325644539610917924487108866748\
97251899281432897966817352381951694370184383299109654078410356489144\
96097727448032065520547484257131085876715457907964421551419900383259\
92326310206845282039558131111404730746868104260513228920636501228410\
04997271652977702547274337834459757112850838135183696311712170990224\
73992869769477285314047680203373559457470718994182065679361157280067\
19481311281095580123586794462594962441430219979368012738883931024129\
18463505832701613704464047394891693567824946365095976820092895872874\
83306734547239954921373656827788157415912067076521596246358718445543\
39227025068139536377423837689642266483656100701688393247498969109923\
90804658988479107678562550844858514512762455637677863067821690083222\
83440561338870911918871947918152819083688890815284572902151349165858\
65098894556135702731215416389951458945610799992135000761012487815006\
42979091198044736325729107144579768273776743716439557581557253878204\
64739117658285116731218202618879515662005685650334009224747947868473\
8621107994804323593105039052556442336528920420940313
real 0m4.785s
user 0m4.770s
sys 0m0.007s
40279005053122345768067055829218304493044109455152896641933464292007\
59023700825508498789380111228748403226643957277673191284838468927263\
98968790560901035018977865543415443376965144616549510538851077440410\
16581978207347558308207939650411138088491681076846747131716891318394\
82758974908036466987938461908525944177396717869145634721620931809562\
84720843607311204509397095907530114810381703803860648328656344389023\
07797564774862888681746680472485858774784093678446243617397742999118\
27532073023480165384946420013014323952797267455381787277085030908707\
45742249581124437084158031806471602854863650302741825130214652788342\
62128387017583390657265689010443241309517109572355085104736018270081\
76664478334500979289573868762717655541374071117476665123099806785398\
86786878262902584539118231519316835523885400137767203426696953126303\
29282822423534913463028008448006465049776475547382410225945312773007\
77775310631151387649525275970984453800464439975409258800869363179334\
21768424925084707414812932329166745886234137226923024813461900363225\
99946452493522794764716794743441163713920351740437121423299823949395\
45623233270769039159314433053699628563882650273461556233325433029434\
46674383336900484885947609695640139797208838438493998636482572950930\
77791964440615462408422804298527057377454368956529809230106268980768\
90711203175785101421818927982471990714520220291727074006887589803057\
02923813291544851703914211657597582497807598793035565731652297925769\
88654916845086709116386840244201837134102214334904094189742237793154\
01317123760884519266468022552657218203325644539610917924487108866748\
97251899281432897966817352381951694370184383299109654078410356489144\
96097727448032065520547484257131085876715457907964421551419900383259\
92326310206845282039558131111404730746868104260513228920636501228410\
04997271652977702547274337834459757112850838135183696311712170990224\
73992869769477285314047680203373559457470718994182065679361157280067\
19481311281095580123586794462594962441430219979368012738883931024129\
18463505832701613704464047394891693567824946365095976820092895872874\
83306734547239954921373656827788157415912067076521596246358718445543\
39227025068139536377423837689642266483656100701688393247498969109923\
90804658988479107678562550844858514512762455637677863067821690083222\
83440561338870911918871947918152819083688890815284572902151349165858\
65098894556135702731215416389951458945610799992135000761012487815006\
42979091198044736325729107144579768273776743716439557581557253878204\
64739117658285116731218202618879515662005685650334009224747947868473\
8621107994804323593105039052556442336528920420940313
real 0m4.785s
user 0m4.770s
sys 0m0.007s
不行了 bc跟不上了 非常大的时候python的优势体现出来了
/************************************************************************/
改成5000! 哦 py不行了。。。
太长了 具体结果不贴了。。
time ./a.py
real 0m50.277s
user 0m49.937s
sys 0m0.010s
real 0m50.277s
user 0m49.937s
sys 0m0.010s
昨天晚上回去用C写了一个 在这个时候C的优势体现出来了
time ./c.out
real 0m0.404s
user 0m0.303s
sys 0m0.000s
real 0m0.404s
user 0m0.303s
sys 0m0.000s
如果再大 C也不行了 那就得用快速傅里叶变换了罢
[ 本帖最后由 zklhp 于 2013-3-26 08:51 编辑 ]