Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created December 19, 2021 02:52
Show Gist options
  • Save emasaka/25fd35460f972f0ac7cc8c1e4abe4a09 to your computer and use it in GitHub Desktop.
Save emasaka/25fd35460f972f0ac7cc8c1e4abe4a09 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
nums = [2, 3, 7, 9]
nums.permutation do |ns|
%i(+ - * /).repeated_permutation(ns.size - 1) do |ops|
begin
x, *xs = ns
z = ops.zip(xs).inject(Rational(x)) {|r, v| r.send(*v) }
p ns.zip(ops).flatten.compact if z == Rational(10)
rescue ZeroDivisionError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment