2016年9月18日日曜日

160918(3)

Ruby


Bernoulli number(1)

「ベルヌーイ数とゼータ関数」(荒川恒男・伊吹山知義・金子昌信 著)
の第2章 2.2 に載っているアルゴリズムを用いて計算してみた。
(ちなみに、この本ではB1 = 1 / 2)
計算過程も含めて出力してみたので、第2章 2.2 に載っている逆三角形
と見比べてほしい。

def bernoulli(n)
  ary = []
  a = []
  (0..n).each{|i|
    a << 1r / (i + 1)
    i.downto(1){|j| a[j - 1] = j * (a[j - 1] - a[j])}
    p [i, a]
    ary << a[0] # Bn = a[0]
  }
  p 'Bernoulli number'
  ary
end

p bernoulli(40)

出力結果
[0, [(1/1)]]
[1, [(1/2), (1/2)]]
[2, [(1/6), (1/3), (1/3)]]
[3, [(0/1), (1/6), (1/4), (1/4)]]
[4, [(-1/30), (1/30), (3/20), (1/5), (1/5)]]
[5, [(0/1), (-1/30), (1/20), (2/15), (1/6), (1/6)]]
[6, [(1/42), (-1/42), (-3/140), (2/35), (5/42), (1/7), (1/7)]]
[7, [(0/1), (1/42), (-1/28), (-1/105), (5/84), (3/28), (1/8), (1/8)]]
[8, [(-1/30), (1/30), (1/140), (-4/105), (0/1), (5/84), (7/72), (1/9), (1/9)]]
[9, [(0/1), (-1/30), (1/20), (-1/105), (-1/28), (1/140), (7/120), (4/45), (1/10), (1/10)]]
[10, [(5/66), (-5/66), (1/220), (8/165), (-5/231), (-29/924), (49/3960), (28/495), (9/110), (1/11), (1/11)]]
[11, [(0/1), (5/66), (-5/44), (7/165), (5/132), (-9/308), (-7/264), (8/495), (3/55), (5/66), (1/12), (1/12)]]
[12, [(-691/2730), (691/2730), (-1017/20020), (-44/455), (200/3003), (295/12012), (-343/10296), (-28/1287), (27/1430), (15/286), (11/156), (1/13), (1/13)]]
[13, [(0/1), (-691/2730), (691/1820), (-2663/15015), (-629/12012), (1543/20020), (67/5720), (-1576/45045), (-87/5005), (125/6006), (55/1092), (6/91), (1/14), (1/14)]]
[14, [(7/6), (-7/6), (601/1820), (368/1365), (-35/143), (-41/12012), (3997/51480), (4/6435), (-27/770), (-27/2002), (121/5460), (22/455), (13/210), (1/15), (1/15)]]
[15, [(0/1), (7/6), (-7/4), (1247/1365), (15/364), (-1013/4004), (133/3432), (464/6435), (-6/715), (-205/6006), (-11/1092), (3/130), (13/280), (7/120), (1/16), (1/16)]]
[16, [(-3617/510), (3617/510), (-809/340), (-244/255), (5350/4641), (-3515/18564), (-38759/175032), (140/1989), (1539/24310), (-75/4862), (-605/18564), (-11/1547), (169/7140), (91/2040), (15/272), (1/17), (1/17)]]
[17, [(0/1), (-3617/510), (3617/340), (-1511/255), (107/204), (32421/30940), (-9653/26520), (-18536/109395), (1113/12155), (775/14586), (-55/2652), (-95/3094), (-13/2856), (49/2040), (35/816), (8/153), (1/18), (1/18)]]
[18, [(43867/798), (-43867/798), (922191/45220), (43416/11305), (-46705/6783), (3035/1596), (1104901/1511640), (-88508/188955), (-51219/461890), (9597/92378), (10769/251940), (-517/20995), (-3887/135660), (-91/38760), (125/5168), (40/969), (17/342), (1/19), (1/19)]]
[19, [(0/1), (43867/798), (-43867/532), (1623817/33915), (-220565/27132), (-47569/9044), (7179/2584), (441824/1322685), (-74976/146965), (-105155/1939938), (38555/352716), (9643/293930), (-7423/271320), (-343/12920), (-7/15504), (352/14535), (34/855), (9/190), (1/20), (1/20)]]
[20, [(-174611/330), (174611/330), (-6132631/29260), (-276356/21945), (181300/3553), (-5472055/298452), (-563801/255816), (98908/31977), (-170073/3233230), (-326115/646646), (-1331/352716), (3223/29393), (169/7140), (-377/12920), (-885/36176), (8/6783), (289/11970), (51/1330), (19/420), (1/21), (1/21)]]
[21, [(0/1), (-174611/330), (174611/220), (-10405289/21945), (619991/5852), (14842153/497420), (-9937529/426360), (179992/159885), (52449/17765), (-738425/1939938), (-164455/352716), (3567/92378), (21177/198968), (6601/426360), (-5165/170544), (-5024/223839), (34/13167), (351/14630), (57/1540), (10/231), (1/22), (1/22)]]
[22, [(854513/138), (-854513/138), (12988703/5060), (-21376/345), (-46300675/100947), (79819919/403788), (-91434539/29418840), (-84078428/3677355), (3255471/817190), (410187/163438), (-25619891/40562340), (-1381937/3380195), (2494609/34321980), (988897/9806280), (10825/1307504), (-2520/81719), (-12427/605682), (255/67298), (361/15180), (190/5313), (21/506), (1/23), (1/23)]]
[23, [(0/1), (854513/138), (-854513/92), (21491081/3795), (-4486325/3036), (-21955485/134596), (25942189/115368), (-129558928/3677355), (-7542618/408595), (155735/25806), (169939/89148), (-5441531/6760390), (-2132689/6240360), (970543/9806280), (1967/20976), (7456/3677355), (-6698/216315), (-6291/336490), (171/35420), (125/5313), (35/1012), (11/276), (1/24), (1/24)]]
[24, [(-236364091/2730), (236364091/2730), (-1552922421/41860), (32209348/10465), (337992250/69069), (-678649375/276276), (1108107805/4499352), (4638284/24453), (-626165937/10623470), (-25306815/2124694), (8374289/1158924), (120659/96577), (-2836327/3120180), (-242333/891480), (8147/68816), (105272/1225785), (-289/86526), (-1479/48070), (-361/21252), (152/26565), (147/6325), (77/2300), (23/600), (1/25), (1/25)]]
[25, [(0/1), (-236364091/2730), (236364091/1820), (-2523785339/31395), (582059503/25116), (119063489/460460), (-328836059/131560), (11876842408/19684665), (249912591/2187185), (-3196365475/44618574), (-38510285/8112468), (609029/79534), (762863/1248072), (-194383/203320), (-943565/4635696), (1261664/9561123), (218314/2812095), (-4941/624910), (-399/13156), (-1063/69069), (427/65780), (2057/89700), (253/7800), (12/325), (1/26), (1/26)]]
[26, [(8553103/6), (-8553103/6), (1139644561/1820), (-107638072/1365), (-18141585/299), (886838131/25116), (-6661850629/1184040), (-22816268/13455), (274348161/336490), (900387/38038), (-3001800791/40562340), (6694963/3380195), (23377679/3120180), (10369/297160), (-10368135/10816624), (-283208/2028117), (1107737/7873866), (303399/4374370), (-1083/92092), (-684/23023), (-686/49335), (77/10764), (529/23400), (92/2925), (25/702), (1/27), (1/27)]]
[27, [(0/1), (8553103/6), (-8553103/4), (1827648887/1365), (-150546745/364), (184550647/8372), (227017945/7176), (-1501623488/148005), (-7030668/16445), (2264592835/2624622), (-29878651/477204), (-65974293/965770), (693979/90440), (361949/52440), (-163361/356592), (-1485856/1601145), (-146098/1789515), (636417/4374370), (513/8372), (-15/1001), (-381/13156), (-121/9660), (253/32760), (152/6825), (25/819), (13/378), (1/28), (1/28)]]
[28, [(-23749461029/870), (23749461029/870), (-7089687053/580), (842271812/435), (6768026800/7917), (-18511988255/31668), (74587309657/624312), (1136903908/78039), (-11414065671/953810), (172090155/190762), (10692144551/13838916), (-153207835/1153243), (-225197401/3934140), (13574561/1124040), (20818639/3447056), (-927208/1077205), (-90734729/103791870), (-348483/11532430), (842213/5722860), (536104/10015005), (-126343/7153575), (-219439/7803900), (-53429/4750200), (4876/593775), (3125/142506), (325/10962), (27/812), (1/29), (1/29)]]
[29, [(0/1), (-23749461029/870), (23749461029/580), (-11254630547/435), (2924743559/348), (-43597042623/52780), (-20217548561/45240), (71527976584/390195), (-361830837/43355), (-6317754175/572286), (208735615/104052), (1361390201/2306486), (-387567557/2129064), (-1117854983/25852920), (156824675/10341168), (9750176/1938969), (-670106/570285), (-9284517/11532430), (17613/1213940), (293245/2003001), (88151/1907620), (-51667/2601300), (-18469/678600), (-664/65975), (205/23751), (169/7830), (117/4060), (14/435), (1/30), (1/30)]]
[30, [(8615841276005/14322), (-8615841276005/14322), (378639019356093/1384460), (-17380760743952/346115), (-2765846511545/207669), (9194040084569/830676), (-123601607588137/46280520), (-378147458164/5785065), (5661825323859/29568110), (-6040416093/203918), (-130271599469/16128060), (3683074417/1344005), (59711076113/165002460), (-9894184391/47143560), (-3018551695/106858736), (341579704/20036013), (28024619/7071534), (-5532123/3928630), (-3567763/4908540), (297844/5644821), (2125424/14784055), (126973/3225612), (-151823/7012200), (-69092/2629575), (-39625/4417686), (3055/339822), (2673/125860), (126/4495), (29/930), (1/31), (1/31)]]
[31, [(0/1), (8615841276005/14322), (-8615841276005/9548), (596303510772251/1038345), (-160974527939935/830676), (7043839847269/276892), (540201920089/79112), (-147653672046128/40495455), (1756630755402/4499495), (18392665376975/124186062), (-1003250897879/22579284), (-835776444953/206976770), (587853689813/191055480), (21641566527/172859720), (-45389402737/207431664), (-1963487072/143736615), (3481362578/194467185), (125753553/43214930), (-310479/197780), (-1739215/2699697), (1005025/11827244), (2253383/16128060), (15433/467480), (-20236/876525), (-1775/70122), (-1352/169911), (117/12586), (1127/53940), (203/7440), (15/496), (1/32), (1/32)]]
[32, [(-7709321041217/510), (7709321041217/510), (-5645818769488043/811580), (78381346456972/55335), (37011879077950/168113), (-3358366543488455/14121492), (27161485402723/417384), (-3734407504708/1513017), (-510603999545313/152982830), (23291844682185/30596566), (27629378745727/383847828), (-1630585580675/31987319), (341000734763/1623971580), (473383384539/154663960), (-768718294563/8228122672), (-1639873520872/7713865005), (-1017074521/2722540590), (1084420089/60500902), (2650101/1384460), (-578208/346115), (-174391/311025), (27016591/241920900), (31156513/231402600), (785588/28925325), (-168125/6942078), (-6500/267003), (-486/69223), (189/19778), (841/40920), (145/5456), (31/1056), (1/33), (1/33)]]
[33, [(0/1), (-7709321041217/510), (7709321041217/340), (-8834901408462277/608685), (818912043504603/162316), (-18566786190720487/23535820), (-2145268463452009/20173560), (607227019180712/7565085), (-10508379435681/840565), (-178860729240925/91789698), (15956655812845/16689036), (-136536617949/9139234), (-978930707963/19684504), (1872413032063/463991880), (46778413295/16872432), (-1287864273824/4628319003), (-265715469238/1361270295), (57126741291/5142576670), (9368631549/541323860), (27155181/27066193), (-133067499/77331980), (-4594256843/9596195700), (334040339/2503355400), (147827776/1147371225), (3000260/137684547), (-7971899/317733570), (-550251/23535820), (-62083/10086780), (13601/1391280), (1875/92752), (155/5984), (16/561), (1/34), (1/34)]]
[34, [(2577687858367/6), (-2577687858367/6), (67894941959587/340), (-11191744708984/255), (-431245550675215/121737), (164807798297293/28644), (-105779053462256573/60520680), (1084435995174364/7565085), (104816115507537/1681130), (-6532598785551/336226), (-473438541257/83445180), (6652193244301/6953765), (-13974856491401/147633780), (-1790568956813/42181080), (437238485465/61865584), (8898226024/3866599), (-229830918617/544508118), (-51537067719/302504510), (4772961139/231995940), (1317371764/81198579), (11140682/57998985), (-21890429/12654324), (-1001660971/2503355400), (47198852/312919425), (101244125/826107282), (97565/5776974), (-605799/23535820), (-37701/1681130), (-841/157080), (6467/649264), (12493/628320), (496/19635), (33/1190), (1/35), (1/35)]]
[35, [(0/1), (2577687858367/6), (-2577687858367/4), (105697073459989/255), (-30092810459185/204), (4213775170171353/162316), (198527138848087/139128), (-14764512136334176/7565085), (325555556912904/840565), (19482261716135/1008678), (-3917458641503/183396), (26927816029243/13907530), (10209601679537/12837720), (-6573264916547/42181080), (-528420013681/16872432), (1593021999776/173996955), (17697429182/10235115), (-158451923169/302504510), (-4498335063/31842580), (2274260315/81198579), (229268333/15466396), (-10836397/21090540), (-552299/323640), (-11332228/34768825), (4115875/25033554), (526435/4539051), (14625/1176791), (-264271/10086780), (-29899/1391280), (-2995/649264), (1271/125664), (128/6545), (44/1785), (17/630), (1/36), (1/36)]]
[36, [(-26315271553053477373/1919190), (26315271553053477373/1919190), (-8222082433140118821/1279460), (479045504390869788/319865), (130785739894457300/3262623), (-2029755955421007845/13050492), (197903031977441655193/3814472376), (-259418672105285140/43346277), (-18491395258437068817/15363847070), (1601020788482746725/3072769414), (-54955972131102955/1676056044), (-2567138088186001/139671337), (15648566238296693/4512458580), (681411178820827/1289273880), (-765395527824139/3953773232), (-68250051266536/3706662405), (148312964303501/14390571690), (1795365934671/1598952410), (-2071869164443/3534526380), (-97567361624/883631595), (119921527537/3576604075), (51608457901/3901749900), (-126103549/113094200), (-210954436/127230975), (-713703125/2778724494), (18669625/106874019), (9514665/87082534), (104265/12440362), (-682051/25738680), (-70615/3431824), (-18259/4649568), (496/48433), (121/6290), (187/7770), (35/1332), (1/37), (1/37)]]
[37, [(0/1), (-26315271553053477373/1919190), (26315271553053477373/1279460), (-12745379713118458459/959595), (3698785153161779183/767676), (-20087877601953700037/21750820), (-9984854869351353/6214520), (869656204850562154264/16688316645), (-23175912707428338687/1854257405), (11949669049953887225/64528157694), (5895721755116780455/11732392308), (-153444418321389515/1955398718), (-21371906191835281/1804983432), (24462964435947163/5586853480), (1446512606787115/6704224176), (-6012827171665312/28911966759), (-46043735779382/8503519635), (44170101043425/4157276266), (105935738169/198912740), (-26811141956723/43651400793), (-3313591015609/41572762660), (35971260904733/963732225300), (966281025017/83802802200), (-16931868536/10475350275), (-666523495/419014011), (-3923492677/20306063610), (273935727/1504152860), (726935531/7091006340), (4616423/978069840), (-1738475/65204656), (-248465/12620256), (-9088/2760681), (2596/250971), (8381/442890), (595/25308), (18/703), (1/38), (1/38)]]
[38, [(2929993913841559/6), (-2929993913841559/6), (294857320048274438263/1279460), (-53977876347278501536/959595), (7134184511439685/9139), (3578930853369592475/767676), (-95112988481654565863/55930680), (1687213261158668404/6991335), (6260409368416144773/285270370), (-11078927789402666187/741702962), (98487582561597348229/58661961540), (1710432681589587703/4888496795), (-25561802555592199/237497820), (-1530495990317911/429757960), (72475589995478625/15643189744), (-273095609474120/2933098077), (-4813184163035299/23809854978), (2763548745453/426699910), (157221014825487/15316280980), (-255953076/33296263), (-19138831594708/31179571995), (-343378980791/6802815708), (321310631971/8109948600), (308221527908/31426050825), (-412788125/203844654), (-875884345/580173246), (-2642772501/19553987180), (261360351/1396713370), (609248153/6357453960), (109069/77060048), (-21909839/820316640), (-482608/25634895), (-29403/10875410), (20009/1919190), (6125/329004), (210/9139), (37/1482), (1/39), (1/39)]]
[39, [(0/1), (2929993913841559/6), (-2929993913841559/4), (455443615848938396081/959595), (-44757008082536826815/255892), (9151158782827676543/255892), (-284756725155498799/219336), (-10592463472445174416/6991335), (334585688357146134/776815), (-57656278674155113685/2225108886), (-4994755545598634035/404565252), (3912560524435871787/1396713370), (350319504797410317/3008305720), (-150313065679722827/1289273880), (4985930691763/1046064), (189857358823248416/43996471155), (-938971709915578/2588027715), (-2391685981711659/13227697210), (23004363635523/1392389180), (7195134951825/765814049), (-208939239051/437608028), (-3608657740853/6105091020), (-488791159351/20704221720), (2980450385348/73327451925), (4924122475/606847878), (-4771079638/2030606361), (-213488307/150415286), (-692085359/8380280220), (7087339/37287120), (75677265/847660528), (-85343/54687776), (-35968/1349205), (-41932/2330445), (-12427/5757570), (3451/329004), (837/45695), (111/4940), (19/780), (1/40), (1/40)]]
[40, [(-261082718496449122051/13530), (261082718496449122051/13530), (-82622815315007896987/9020), (15700351621967437588/6765), (-9138915566031900914350/86555469), (-53245099482846805949215/346221876), (18219172354548840589327/296761608), (-373501844616010173476/37095201), (-179720872672814570763/700687130), (64353055350136153035/140137426), (-1191507514351676949757/16587175332), (-8038842702373537879/1382264611), (607927801147902631513/185010801780), (-248463324791491537/1822766520), (-753088221725787125/7048030544), (5237463779536728/440501909), (758080979491529851/212218272630), (-435801716869881/760638970), (-3644956467105211/24466267020), (1043103856394128/42815967285), (2750929195968289/336411171525), (-318129456436253/366994005300), (-2341563606057757/4244365452600), (200615704988/530545681575), (87948790705625/2164626380826), (540245893850/83254860801), (-8014553829/3083513363), (-1165621419/881003818), (-5860717909/165871753320), (4230682835/22116233776), (3903508003/47086175136), (-366544/86555469), (-11827871/445891810), (-450857/26228930), (-1715/1037628), (3948/374699), (2738/151905), (703/31980), (39/1640), (1/41), (1/41)]]
"Bernoulli number"
[(1/1), (1/2), (1/6), (0/1), (-1/30), (0/1), (1/42), (0/1), (-1/30), (0/1), (5/66), (0/1), (-691/2730), (0/1), (7/6), (0/1), (-3617/510), (0/1), (43867/798), (0/1), (-174611/330), (0/1), (854513/138), (0/1), (-236364091/2730), (0/1), (8553103/6), (0/1), (-23749461029/870), (0/1), (8615841276005/14322), (0/1), (-7709321041217/510), (0/1), (2577687858367/6), (0/1), (-26315271553053477373/1919190), (0/1), (2929993913841559/6), (0/1), (-261082718496449122051/13530)]

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。