CodeIQ 中学入試から：数列の和をRubyで解く

問題はこちら
http://nabetani.hatenablog.com/entry/codeiq_sumseq_q1012

```rb
def solve(q, st, ed)
  ns = q.split(',').map(&:to_i)
  (1..ns.size).each{|sz|
    ps = ns.each_slice(sz).to_a
    ds = ps.zip([[]]+ps).drop(1).map{|x,y|x.zip(y).map{|a,b|a-b}}.uniq
    next if ds[2] || (ds[1] && ds[0].zip(ds[1]).select{|x,y| x && y && x != y}.size > 0)
    d = ds.max
    return (0..ed/d.size).map{|i| ns[0...d.size].zip(d.map{|e|e*i}).map{|x,y|x+y}}.flatten[st..ed].inject(&:+)
  }
  0
end

DATA.readlines.each do |line|
  no,q,st,ed,a = line.strip.split(/\s+/)
  ans = solve(q, st.to_i-1, ed.to_i-1)
  print no + "\t" + ans.to_s
  puts ans == a.to_i ? ' o' : ' x'
end
__END__
T01 1,2,100,1,2,101,1,2,102,1,2,103,1,2,104,1,2,105,1,2,106,1,2,107,1,2,108,1,2,109,1,2,110,1,2,111,1,2,112 100 110 414
T02 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 1 1 1
T03 1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7,1,1,1,7 100 100 1
T04 1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1,1,2 58 171 199
T05 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 40 192 459
T06 3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3 7 178 344
T07 3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4 15 106 247
T08 3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4,1,3,1,4 98 192 213
T09 3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3,1,4,1,5,3 70 192 345
T10 3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4,1,5,9,3,1,4 54 158 404
T11 3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1,4,1,5,9,2,3,1 12 198 670
T12 3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,3,1,4 84 162 306
T13 3,1,4,1,5,9,2,6,5,3,4,1,5,9,2,6,7,5,4,1,5,9,2,6,9,7,4,1,5,9,2,6,11,9,4,1,5,9,2,6,13,11,4,1,5,9,2,6,15 11 122 910
T14 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 47 116 5705
T15 10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76 44 141 18914
T16 1,8,15,22,29,36,43,50,57,64,71,78,85,92,99,106,113,120,127,134,141,148,155,162,169,176,183,190,197,204 37 125 49929
T17 1,100,1,101,1,102,1,103,1,104,1,105,1,106,1,107,1,108,1,109,1,110,1,111,1,112,1,113,1,114,1,115,1,116 74 180 8828
T18 1,2,7,1,2,14,1,2,21,1,2,28,1,2,35,1,2,42,1,2,49,1,2,56,1,2,63,1,2,70,1,2,77,1,2,84,1,2,91,1,2,98,1,2 11 161 10127
T19 1,1,7,1,1,8,1,1,10,1,1,7,1,1,8,1,1,10,1,1,7,1,1,8,1,1,10,1,1,7,1,1,8,1,1,10,1,1,7,1,1,8,1,1,10,1,1,7 42 129 308
T20 1,1,7,1,1,8,1,1,9,1,1,10,1,1,11,1,1,12,1,1,13,1,1,14,1,1,15,1,1,16,1,1,17,1,1,18,1,1,19,1,1,20,1,1,21 1 178 2243
T21 1,1,7,1,1,8,1,1,9,1,1,8,1,1,11,1,1,8,1,1,13,1,1,8,1,1,15,1,1,8,1,1,17,1,1,8,1,1,19,1,1,8,1,1,21,1,1,8 68 181 1121
T22 1,2,3,1,1,2,3,2,1,2,3,3,1,2,3,4,1,2,3,5,1,2,3,6,1,2,3,7,1,2,3,8,1,2,3,9,1,2,3,10,1,2,3,11,1,2,3,12,1 76 115 295
T23 1,2,3,4,100,2,3,4,5,100,3,4,5,6,100,4,5,6,7,100,5,6,7,8,100,6,7,8,9,100,7,8,9,10,100,8,9,10,11,100,9 29 125 3339
T24 1,2,3,4,5,2,4,6,8,10,3,6,9,12,15,4,8,12,16,20,5,10,15,20,25,6,12,18,24,30,7,14,21,28,35,8,16,24,32,40 46 130 4590
T25 1,2,3,4,5,6,6,6,6,6,6,6,11,10,9,8,7,6,16,14,12,10,8,6,21,18,15,12,9,6,26,22,18,14,10,6,31,26,21,16,11 10 125 3546
T26 4,3,2,1,6,6,6,6,8,9,10,11,10,12,14,16,12,15,18,21,14,18,22,26,16,21,26,31,18,24,30,36,20,27,34,41,22 99 166 7871
T27 1,1,1,1,1,1,1,1,1,1,1,1,1,8,1,1,1,1,1,1,15,1,1,1,1,1,1,22,1,1,1,1,1,1,29,1,1,1,1,1,1,36,1,1,1,1,1,1,43 4 141 1468
T28 1,1,2,1,1,2,1,1,2,1,1,3,1,1,4,1,1,5,1,1,4,1,1,6,1,1,8,1,1,5,1,1,8,1,1,11,1,1,6,1,1,10,1,1,14,1,1,7,1 64 196 1374
T29 1,2,3,1,2,3,1,2,3,1,2,4,1,2,5,1,2,6,1,2,5,1,2,7,1,2,9,1,2,6,1,2,9,1,2,12,1,2,7,1,2,11,1,2,15,1,2,8,1 17 156 1111
T30 35,31,7,30,35,12,35,31,7,30,35,12,35,31,7,30,35,12,35,31,7,30,35,12,35,31,7,30,35,12,35,31,7,30,35,12 65 148 2100
```