NMAX = 1e6.to_i t = [0,0,0,1] freq = nil NMAX.times do |i| t.push (t[-1]+t[-2]+t[-3]+t[-4])%17 if [t[-4],t[-3],t[-2],t[-1]] == [0,0,0,1] freq = i+1 break end end gets.to_i.times do |i| n = (gets.to_i-1)%freq p t[n] end