require 'prime' @n = gets.strip.to_i @list = [2] 3.step(@n, 2) do |i| if i.prime? @list << i end end #p @list @memo = Array.new(@n + 1, 0) @list.each do |p| #p @memo @next_memo = @memo.clone (0..@n).each do |i| #p key, value if i + p > @n break end @next_memo[i + p] = [@memo[i + p], @memo[i] + 1].max end @memo = @next_memo end #p @memo if @memo[@n] != 0 p @memo[@n] else p -1 end