Last active
July 21, 2017 13:23
-
-
Save awls99/597e64cb6b005d6feac021840b68dded to your computer and use it in GitHub Desktop.
Some math 4 4's thinggy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
math_methods = Math.methods(false).each do |m| | |
self.class.send(:define_method, m, Proc.new do |x| Math.method(m).(x); end ) | |
end | |
math_methods.delete_if{|m| %I[atan2 hypot acos asin atan atanh frexp lgamma ldexp].include? m} | |
def sqrt x | |
Math.sqrt x | |
end | |
ops = [' + 4.0 ', ' - 4.0 ', ' * 4.0', ' / 4.0 ',] | |
mixer = %w[* - + /] | |
math_methods.each do |math_method| | |
ops.concat( mixer.map{|m| "#{m} #{math_method}(4.0) "}) | |
end | |
solutions = {} | |
start = Time.now | |
while true do | |
string = '4.0 ' | |
3.times do | |
string += ops.sample | |
end | |
# puts string | |
result = eval( string) | |
if result > 0 and result.to_i.to_f == result and !solutions[result] | |
puts string | |
puts result | |
solutions[result] = string | |
end | |
break if Time.now - start >= 2*60*60 #two hours timeout because I went for lunch | |
end | |
# at_exit do | |
puts '=======================================' | |
solutions.keys.sort.each do |k| | |
puts "#{k} = #{solutions[k]}" | |
end | |
puts '=======================================' | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment