2017年5月28日日曜日

170528(2)

Ruby


A186690

二通りで求め、一致することを確認してみた。

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

def A186690_1(n)
  (1..n).map{|i| s(1, i) - 5 * s(2, i) / 2 + s(4, i)}
end

def A186690_2(n)
  ary = []
  (1..n).each{|i|
    j = s(1, i)
    j -= s(1, i / 2) if i % 2 == 0
    ary << (-1) ** (i % 2 + 1) * j
  }
  ary
end

n = 1000
p ary = A186690_1(n)
p ary == A186690_2(n)

出力結果
[1, -2, 4, -4, 6, -8, 8, -8, 13, -12, 12, -16, 14, -16, 24, -16, 18, -26, 20, -24, 32, -24, 24, -32, 31, -28, 40, -32, 30, -48, 32, -32, 48, -36, 48, -52, 38, -40, 56, -48, 42, -64, 44, -48, 78, -48, 48, -64, 57, -62, 72, -56, 54, -80, 72, -64, 80, -60, 60, -96, 62, -64, 104, -64, 84, -96, 68, -72, 96, -96, 72, -104, 74, -76, 124, -80, 96, -112, 80, -96, 121, -84, 84, -128, 108, -88, 120, -96, 90, -156, 112, -96, 128, -96, 120, -128, 98, -114, 156, -124, 102, -144, 104, -112, 192, -108, 108, -160, 110, -144, 152, -128, 114, -160, 144, -120, 182, -120, 144, -192, 133, -124, 168, -128, 156, -208, 128, -128, 176, -168, 132, -192, 160, -136, 240, -144, 138, -192, 140, -192, 192, -144, 168, -208, 180, -148, 228, -152, 150, -248, 152, -160, 234, -192, 192, -224, 158, -160, 216, -192, 192, -242, 164, -168, 288, -168, 168, -256, 183, -216, 260, -176, 174, -240, 248, -192, 240, -180, 180, -312, 182, -224, 248, -192, 228, -256, 216, -192, 320, -240, 192, -256, 194, -196, 336, -228, 198, -312, 200, -248, 272, -204, 240, -288, 252, -208, 312, -224, 240, -384, 212, -216, 288, -216, 264, -320, 256, -220, 296, -288, 252, -304, 224, -256, 403, -228, 228, -320, 230, -288, 384, -240, 234, -364, 288, -240, 320, -288, 240, -384, 242, -266, 364, -248, 342, -336, 280, -256, 336, -312, 252, -416, 288, -256, 432, -256, 258, -352, 304, -336, 390, -264, 264, -384, 324, -320, 360, -272, 270, -480, 272, -288, 448, -276, 372, -384, 278, -280, 416, -384, 282, -384, 284, -288, 480, -336, 336, -416, 307, -360, 392, -296, 294, -456, 360, -304, 480, -300, 336, -496, 352, -304, 408, -320, 372, -468, 308, -384, 416, -384, 312, -448, 314, -316, 624, -320, 318, -432, 360, -384, 432, -384, 360, -484, 434, -328, 440, -336, 384, -576, 332, -336, 494, -336, 408, -512, 338, -366, 456, -432, 384, -520, 400, -352, 576, -348, 348, -480, 350, -496, 560, -384, 354, -480, 432, -360, 576, -360, 360, -624, 381, -364, 532, -448, 444, -496, 368, -384, 546, -456, 432, -512, 374, -432, 624, -384, 420, -640, 380, -480, 512, -384, 384, -512, 576, -388, 572, -392, 390, -672, 432, -456, 528, -396, 480, -624, 398, -400, 640, -496, 402, -544, 448, -408, 726, -480, 456, -576, 410, -504, 552, -416, 480, -624, 504, -448, 560, -480, 420, -768, 422, -424, 624, -432, 558, -576, 496, -432, 672, -528, 432, -640, 434, -512, 720, -440, 480, -592, 440, -576, 741, -504, 444, -608, 540, -448, 600, -512, 450, -806, 504, -456, 608, -456, 672, -640, 458, -460, 720, -576, 462, -768, 464, -480, 768, -468, 468, -728, 544, -576, 632, -480, 528, -640, 620, -576, 702, -480, 480, -768, 532, -484, 768, -532, 588, -728, 488, -496, 656, -684, 492, -672, 540, -560, 936, -512, 576, -672, 500, -624, 672, -504, 504, -832, 612, -576, 732, -512, 510, -864, 592, -512, 800, -516, 624, -704, 576, -608, 696, -672, 522, -780, 524, -528, 992, -528, 576, -768, 553, -648, 780, -640, 588, -720, 648, -544, 720, -540, 684, -960, 542, -544, 728, -576, 660, -896, 548, -552, 806, -744, 600, -768, 640, -556, 912, -560, 558, -832, 616, -768, 864, -564, 564, -768, 684, -568, 968, -576, 570, -960, 572, -672, 768, -672, 744, -832, 578, -614, 776, -720, 672, -784, 648, -592, 1092, -588, 588, -912, 640, -720, 792, -608, 594, -960, 864, -600, 800, -672, 600, -992, 602, -704, 884, -608, 798, -816, 608, -640, 960, -744, 672, -936, 614, -616, 1008, -768, 618, -832, 620, -768, 960, -624, 720, -896, 781, -628, 960, -632, 684, -1248, 632, -640, 848, -636, 768, -864, 798, -720, 936, -768, 642, -864, 644, -768, 1056, -720, 648, -968, 720, -868, 1024, -656, 654, -880, 792, -672, 962, -768, 660, -1152, 662, -664, 1008, -672, 960, -988, 720, -672, 896, -816, 744, -1024, 674, -676, 1240, -732, 678, -912, 784, -864, 912, -768, 684, -1040, 828, -800, 920, -704, 756, -1152, 692, -696, 1248, -696, 840, -960, 756, -700, 936, -992, 702, -1120, 760, -768, 1152, -708, 816, -960, 710, -864, 1040, -720, 768, -1152, 1008, -720, 960, -720, 720, -1248, 832, -762, 968, -728, 930, -1064, 728, -896, 1093, -888, 792, -992, 734, -736, 1368, -768, 816, -1092, 740, -912, 1120, -864, 744, -1024, 900, -748, 1092, -864, 864, -1248, 752, -768, 1008, -840, 912, -1280, 758, -760, 1152, -960, 762, -1024, 880, -768, 1404, -768, 840, -1024, 770, -1152, 1032, -776, 774, -1144, 992, -784, 1216, -780, 840, -1344, 864, -864, 1200, -912, 948, -1056, 788, -792, 1056, -960, 912, -1248, 868, -796, 1296, -800, 798, -1280, 864, -992, 1170, -804, 888, -1088, 1152, -896, 1080, -816, 810, -1452, 812, -960, 1088, -912, 984, -1152, 880, -820, 1456, -1008, 822, -1104, 824, -832, 1488, -960, 828, -1248, 830, -1008, 1112, -896, 1026, -1120, 1008, -960, 1280, -840, 840, -1536, 871, -844, 1128, -848, 1098, -1248, 1064, -864, 1136, -1116, 912, -1152, 854, -992, 1560, -864, 858, -1344, 860, -1056, 1344, -864, 864, -1280, 1044, -868, 1228, -1024, 960, -1440, 952, -880, 1274, -960, 1248, -1184, 878, -880, 1176, -1152, 882, -1482, 884, -1008, 1440, -888, 888, -1216, 1024, -1080, 1452, -896, 960, -1200, 1080, -1024, 1344, -900, 960, -1612, 972, -1008, 1408, -912, 1092, -1216, 908, -912, 1326, -1344, 912, -1280, 1008, -916, 1488, -920, 1056, -1440, 920, -1152, 1232, -924, 1008, -1536, 1178, -928, 1352, -960, 930, -1536, 1140, -936, 1248, -936, 1296, -1456, 938, -1088, 1256, -1152, 942, -1264, 1008, -960, 1920, -1056, 948, -1280, 1036, -1240, 1272, -1152, 954, -1404, 1152, -960, 1440, -960, 1104, -1536, 993, -1064, 1404, -968, 1164, -1536, 968, -1064, 1440, -1176, 972, -1456, 1120, -976, 1736, -992, 978, -1312, 1080, -1368, 1430, -984, 984, -1344, 1188, -1080, 1536, -1120, 1056, -1872, 992, -1024, 1328, -1152, 1200, -1344, 998, -1000, 1520, -1248]
true

0 件のコメント:

コメントを投稿

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