Math problem

On Feb 26, 4:28 pm, [email protected] wrote:

I am try to help my daughter:
Problem

father is 4 times older then his daughter.
in 6 years he will be 3 times older what is the answer and how did you
reach it.
Thanks, a mom in CA

0.upto(100) do |father_age|
0.upto(100) do |daughter_age|

   if (father_age  == 4* daughter_age) && (father_age+6  == 3*

(daughter_age+6))
puts “father age is “+father_age.to_s+”, and
daughter_age=”+daughter_age.to_s
end
end
end

father age is 48, and daughter_age=12

On Wed, Feb 27, 2008 at 9:25 PM, Rimantas L. [email protected]
wrote:

=> true

http://rimantas.com/

Yeah, I would probably use Rational or BigDecimal or even Float, but
for this purpose, Integer seems to work.

Todd

On Wed, Feb 27, 2008 at 9:25 PM, Rimantas L. [email protected]
wrote:

=> true

k = Matrix[*[[4, -1], [-3, 1]].map{|r| r.map{|x| Rational(x)}}].inverse
=> Matrix[[Rational(1, 1), Rational(1, 1)], [Rational(3, 1), Rational(4, 1)]]
c = Matrix[[Rational(0)],[Rational(12)]]
=> Matrix[[Rational(0, 1)], [Rational(12, 1)]]
(k * c).to_a.flatten.map{|e| e.to_i}
=> [12, 48]

Opposites attract… But that’s not rational, is it?

Joking aside, I’m actually working on solving nonlinear systems of
equations (you know, upside down pendulum, etc.) with Ruby as an
exercise; and process time doesn’t matter all that much.

Todd

On Wed, Feb 27, 2008 at 4:21 PM, Rick DeNatale [email protected]
wrote:

Well except in the case where some ZERO cleverly seeds the random

I think Rick’s approach is best and makes the most sense. We make a
couple of assumptions, but it’s very quick (compared to mine ).
I don’t think, however, it would scale to more dimensions (i.e.
comparing x and y and z and maybe even w). The speed difference,
though, for this problem, is markedly different (like a factor of
four) .

Todd

On Thu, Feb 28, 2008 at 11:08 AM, Todd B. [email protected]
wrote:

I think Rick’s approach is best and makes the most sense. We make a
couple of assumptions, but it’s very quick (compared to mine ).
I don’t think, however, it would scale to more dimensions (i.e.
comparing x and y and z and maybe even w). The speed difference,
though, for this problem, is markedly different (like a factor of
four) .

I need to qualify that last post. Sorry to beat a dead horse, people.

I really like Rick’s step-up solution, but it fails to find
non-integer solutions (as does mine, but if you change it to floats
you’re doing okay). It also has problems with lop-sided ages (like…
pretend people could be 5000 years old).

Just some noise that might help some newbies.

Todd