Backing into numbers with Ruby?

Are there any apps out there that can try and arrive at n given x
criterion?
If not, perhaps there is good reason(complexity maybe?). If there is or
if
anyone has thoughts to share on the topic, I’m very interested. To
exemplify what I’m looking for, here’s a 3am(yawns) demonstration:

include Math

def add(x)
ans = 0
x.first.each {|item| ans += item }
return {:calc => x.join("+"), :ans => ans}
end
def multiply(x)
ans = 1
x.first.each {|item| ans = ans
item }
return {:calc => x.join("
"), :ans => ans}
end

def arrive_at(n, criterion)
criterion[:methods].each {|m|
response = send(m, criterion[:given])
puts response[:calc] if response[:ans].to_s == n.to_s
i=0
criterion[:lambdas].each {|lamb|
if lamb[:for].include? m
lamb[:try].each {|l|
lambda_response = l.call(response)
puts “#{m}lambda[#{i.to_s}]
if
lambda_response.to_s == n.to_s
}
end
i+=1
}
}
end

arrive_at(10, {:given => [2,5], :methods => [‘multiply’], :lambdas =>
[]})
# => yields “2*5”

arrive_at(10, {:given => [7,3], :methods => [‘add’, ‘multiply’],
:lambdas =>
[]})
# => yields “7+3”

arrive_at(12.5663706143592,
{
:given => [2, 2], :methods => [‘add’, ‘multiply’],
:lambdas => [{:for => [‘multiply’], :try => [lambda {|x|
x[:ans]*PI}]}]
}
)
# => “multiplylambda[0]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs