Math problem

T

Todd Benson

And then I much prefer the algebraic solution I worked out in a few
seconds on paper when the question was first posed.
<...>

Father: x
Daugher: y
4y - x = 0
(x + 6) / (y+6) = 3 -> 3y + 18 = x + 6 -> 3y - x = -12

Coefficients:
4, -1
3, -1
Constants
0
-12
require 'matrix' => true
require 'rational' => 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]


Regards,
Rimantas

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

Todd
 
T

Todd Benson

And then I much prefer the algebraic solution I worked out in a few
seconds on paper when the question was first posed.
<...>

Father: x
Daugher: y
4y - x = 0
(x + 6) / (y+6) = 3 -> 3y + 18 = x + 6 -> 3y - x = -12

Coefficients:
4, -1
3, -1
Constants
0
-12
require 'matrix' => true
require 'rational' => 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
 
T

Todd Benson

Well except in the case where some ZERO cleverly seeds the random
number generator to produce the right random numbers, yes! A very
slight modification:

def init_counts
zero = 0

@counts = Hash.new(zero)
end

produces:


father: 48 daughter: 12
Father is 48 years old, Daughter is 12.

rick: 12 loops
thomas: 19467 loops


Of course, being random, the thomas count varies, while the rick count
stays at 12.

And then I much prefer the algebraic solution I worked out in a few
seconds on paper when the question was first posed.

I leave to others the pleasure of discovering the details of your trick. <G>

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 <shrug>).
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
 
T

Todd Benson

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 <shrug>).
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
jigyasauniversity

Latest Threads

Top