X = gets.to_i SPF = Array.new(X + 1, -1) SPF[0] = 0 SPF[1] = 1 2.upto(X) do |x| next if SPF[x] != -1 x.step(X, x) do |n| next if SPF[n] != -1 SPF[n] = x end end def divisor_count(x) res = 1 cur = -1 cnt = 0 while x != 1 f = SPF[x] if cur != f res *= (cnt + 1) cur = f cnt = 1 else cnt += 1 end x /= f end res * (cnt + 1) end min_diff = Float::INFINITY ans = [] 1.upto(X - 1) do |a| b = X - a v1 = a - divisor_count(a) v2 = b - divisor_count(b) diff = (v1 - v2).abs if min_diff > diff min_diff = diff ans = [[a, b]] elsif min_diff == diff ans << [a, b] end end puts ans.map { |nums| nums.join(' ') }