require 'prime' S = gets.chomp N = S.size nums = S.chars.map(&:to_i) def dfs(i, base, sum, nums) cnt = 0 if i == 1 cnt += dfs(i + 1, base * 10, sum + base * nums[-i], nums) elsif i > N return sum.prime? ? 1 : 0 else cnt += dfs(i + 1, base * 10, sum + base * nums[-i], nums) cnt += dfs(i + 1, 10, sum + nums[-i], nums) end cnt end puts dfs(1, 1, 0, nums)