# Backing into numbers with Ruby?

**URL:** <https://www.ruby-forum.com/t/backing-into-numbers-with-ruby/113926>\
**Category:** Ruby\
**Created:** [September 6, 2007, 8:54am UTC](https://www.ruby-forum.com/t/backing-into-numbers-with-ruby/113926 "2007-09-06T08:54:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [September 6, 2007, 8:54am UTC](https://www.ruby-forum.com/t/backing-into-numbers-with-ruby/113926/1 "2007-09-06T08:54:04Z")

</div>

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 = ansitem }  
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]”
