2017年3月28日火曜日

170328

Ruby


Product_{k >= 1} (1 - x^k)^m の計算

以下の方法だとm の絶対値が大きくなっても大丈夫。

def s(n)
  s = 0
  (1..n).each{|i| s += i if n % i == 0}
  s
end

def A(k, n)
  ary = [1]
  a = [0] + (1..n).map{|i| s(i)}
  (1..n).each{|i| ary << (1..i).inject(0){|s, j| s -= k * a[j] * ary[-j]} / i}
  ary
end

n = 15
-24.upto(24){|i| p [i, A(i, n)]}

出力結果
[-24, [1, 24, 324, 3200, 25650, 176256, 1073720, 5930496, 30178575, 143184000, 639249300, 2705114880, 10914317934, 42189811200, 156883829400, 563116739584]]
[-23, [1, 23, 299, 2852, 22126, 147407, 871838, 4680845, 23177583, 107100903, 466066181, 1923780950, 7576060505, 28601630657, 103928814438, 364712523658]]
[-22, [1, 22, 275, 2530, 18975, 122430, 702328, 3661900, 17627775, 79264900, 335937954, 1351507830, 5191041625, 19125838600, 67862904725, 232671319474]]
[-21, [1, 21, 252, 2233, 16170, 100926, 560945, 2837418, 13266099, 57994475, 239170239, 937026279, 3507380170, 12601619226, 43628951025, 146036139347]]
[-20, [1, 20, 230, 1960, 13685, 82524, 443870, 2175800, 9869990, 41907380, 168012824, 640438680, 2334121995, 8171039800, 27580783270, 90058003200]]
[-19, [1, 19, 209, 1710, 11495, 66880, 347681, 1649637, 7252300, 29875505, 116319938, 430976031, 1527928814, 5206792965, 17119704425, 54484060983]]
[-18, [1, 18, 189, 1482, 9576, 53676, 269325, 1235286, 5256711, 20985272, 79260723, 285139764, 982349361, 3255488082, 10416507579, 32281134120]]
[-17, [1, 17, 170, 1275, 7905, 42619, 206091, 912475, 3753600, 14503040, 53073898, 185172670, 619237835, 1993524975, 6200890505, 18693654410]]
[-16, [1, 16, 152, 1088, 6460, 33440, 155584, 663936, 2636326, 9845040, 34861152, 117809728, 381946360, 1193074144, 3603543040, 10556065152]]
[-15, [1, 15, 135, 920, 5220, 25893, 115700, 475065, 1817910, 6551390, 22414314, 73265580, 229972855, 696109950, 2039031360, 5796944357]]
[-14, [1, 14, 119, 770, 4165, 19754, 84602, 333608, 1228080, 4263770, 14071827, 44420796, 134793918, 394805110, 1119974875, 3086034350]]
[-13, [1, 13, 104, 637, 3276, 14820, 60697, 229372, 810654, 2706366, 8600501, 26173966, 76654656, 216903064, 594973106, 1586553501]]
[-12, [1, 12, 90, 520, 2535, 10908, 42614, 153960, 521235, 1669720, 5098938, 14931072, 42124380, 114945780, 304351020, 784087848]]
[-11, [1, 11, 77, 418, 1925, 7854, 29183, 100529, 325193, 997150, 2919411, 8207563, 22259237, 58454165, 149104450, 370410700]]
[-10, [1, 10, 65, 330, 1430, 5512, 19415, 63570, 195910, 573430, 1605340, 4322110, 11240645, 28341730, 69488650, 166096270]]
[-9, [1, 9, 54, 255, 1035, 3753, 12483, 38709, 113265, 315445, 841842, 2164185, 5382276, 12994290, 30543210, 70066809]]
[-8, [1, 8, 44, 192, 726, 2464, 7704, 22528, 62337, 164560, 417140, 1020416, 2418710, 5573568, 12520744, 27484160]]
[-7, [1, 7, 35, 140, 490, 1547, 4522, 12405, 32305, 80465, 192899, 447146, 1006145, 2204475, 4715510, 9869132]]
[-6, [1, 6, 27, 98, 315, 918, 2492, 6372, 15525, 36280, 81816, 178794, 380051, 788004, 1597725, 3174210]]
[-5, [1, 5, 20, 65, 190, 506, 1265, 2990, 6765, 14725, 31027, 63505, 126730, 247170, 472295, 885723]]
[-4, [1, 4, 14, 40, 105, 252, 574, 1240, 2580, 5180, 10108, 19208, 35693, 64960, 116090, 203984]]
[-3, [1, 3, 9, 22, 51, 108, 221, 429, 810, 1479, 2640, 4599, 7868, 13209, 21843, 35581]]
[-2, [1, 2, 5, 10, 20, 36, 65, 110, 185, 300, 481, 752, 1165, 1770, 2665, 3956]]
[-1, [1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176]]
[0, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
[1, [1, -1, -1, 0, 0, 1, 0, 1, 0, 0, 0, 0, -1, 0, 0, -1]]
[2, [1, -2, -1, 2, 1, 2, -2, 0, -2, -2, 1, 0, 0, 2, 3, -2]]
[3, [1, -3, 0, 5, 0, 0, -7, 0, 0, 0, 9, 0, 0, 0, 0, -11]]
[4, [1, -4, 2, 8, -5, -4, -10, 8, 9, 0, 14, -16, -10, -4, 0, -8]]
[5, [1, -5, 5, 10, -15, -6, -5, 25, 15, -20, 9, -45, -5, 25, 20, 10]]
[6, [1, -6, 9, 10, -30, 0, 11, 42, 0, -70, 18, -54, 49, 90, 0, -22]]
[7, [1, -7, 14, 7, -49, 21, 35, 41, -49, -133, 98, -21, 126, 112, -176, -105]]
[8, [1, -8, 20, 0, -70, 64, 56, 0, -125, -160, 308, 0, 110, 0, -520, 0]]
[9, [1, -9, 27, -12, -90, 135, 54, -99, -189, -85, 657, -162, -135, -171, -810, 702]]
[10, [1, -10, 35, -30, -105, 238, 0, -260, -165, 140, 1054, -770, -595, 0, -715, 2162]]
[11, [1, -11, 44, -55, -110, 374, -143, -462, 55, 495, 1287, -2069, -902, 1210, -275, 3795]]
[12, [1, -12, 54, -88, -99, 540, -418, -648, 594, 836, 1056, -4104, -209, 4104, -594, 4256]]
[13, [1, -13, 65, -130, -65, 728, -871, -715, 1560, 845, 78, -6513, 2730, 8605, -4355, 2483]]
[14, [1, -14, 77, -182, 0, 924, -1547, -506, 3003, 0, -1729, -8372, 9177, 13090, -15625, 0]]
[15, [1, -15, 90, -245, 105, 1107, -2485, 195, 4860, -2420, -3990, -8190, 19695, 13755, -38475, 3990]]
[16, [1, -16, 104, -320, 260, 1248, -3712, 1664, 6890, -7280, -5568, -4160, 33176, 4640, -74240, 29824]]
[17, [1, -17, 119, -408, 476, 1309, -5236, 4233, 8602, -15470, -4250, 5236, 45815, -21182, -117776, 101065]]
[18, [1, -18, 135, -510, 765, 1242, -7038, 8280, 9180, -27710, 3519, 20196, 50370, -68850, -153765, 244782]]
[19, [1, -19, 152, -627, 1140, 988, -9063, 14212, 7410, -44270, 22781, 38114, 36176, -137256, -154850, 480605]]
[20, [1, -20, 170, -760, 1615, 476, -11210, 22440, 1615, -64600, 60002, 51680, -9520, -213180, -83980, 803528]]
[21, [1, -21, 189, -910, 2205, -378, -13321, 33345, -10395, -86870, 122703, 46683, -98287, -264915, 96390, 1163064]]
[22, [1, -22, 209, -1078, 2926, -1672, -15169, 47234, -31350, -107426, 218680, -266, -234707, -237006, 405878, 1444806]]
[23, [1, -23, 230, -1265, 3795, -3519, -16445, 64285, -64515, -120175, 354706, -123763, -407560, -48530, 817190, 1464341]]
[24, [1, -24, 252, -1472, 4830, -6048, -16744, 84480, -113643, -115920, 534612, -370944, -577738, 401856, 1217160, 987136]]

0 件のコメント:

コメントを投稿