require 'prime' n = gets.strip.to_i primes = [] primes << 2 i = 3 while i < n primes << i if i.prime? i += 2 end # p primes @memo = Hash.new(0) primes.each do |prime| @memo_next = @memo.clone @memo.each do |k, v| next if k + prime > n @memo_next[k + prime] = [@memo[k + prime], v + 1].max end @memo_next[prime] = [1, @memo[prime]].max @memo = @memo_next end if @memo.key?(n) p @memo[n] else puts -1 end