# Maths in Ruby

Hello to everyone!

I have a mathematic problem I need to solve, and it involves finding the
values of three consecutive numbers, x, y and z. I need to be able to
find
every instance of these from negative one million (-1,000,000) up to one
million (1,000,000). They must follow the following equations:

x + y + z = 5
x + y - z = 7
(x - y)(x - y)(x - y) + (y - z)(y - z)(y - z) = (x - z)(x - z)(x - z)

I’m completely and utterly stumped. I intend on solving this via Ruby,
as
doing it on pen and paper might take a while. If anyone has any
suggestions, I’d love to hear them.

Thanks!

Hi,

2007/3/5, Yannick G. [email protected]:

I have a mathematic problem I need to solve, and it involves finding the
values of three consecutive numbers, x, y and z. I need to be able to find
every instance of these from negative one million (-1,000,000) up to one
million (1,000,000). They must follow the following equations:

x + y + z = 5
x + y - z = 7
(x - y)(x - y)(x - y) + (y - z)(y - z)(y - z) = (x - z)(x - z)(x - z)

You can use “algebra” (http://raa.ruby-lang.org/project/algebra/)

require “algebra”
R = MPolynomial(Rational)
x, y, z = R.vars(“xyz”)
f0 = x + y + z - 5
f1 = x + y - z - 7
f2 = (x - y)(x - y)(x - y) + (y - z)(y - z)(y - z) -
(x - z)(x - z)(x - z)
gs = Groebner.basis([f0, f1, f2])
gs.each do |f|
p f.factorize
end

This shows:
x + y - 6
(y - 3)(y - 7)(y + 1)
z + 1

These mean “z = -1, y = 3, 7, -1, x = -y + 6”, And

p f2.factorize #=> (-3)(x - z)(x - y)(y - z)

This means you can solve this by hand.

Regards,
Shin-ichiro HARA

Yannick G. wrote:

I’m completely and utterly stumped. I intend on solving this via Ruby,
as doing it on pen and paper might take a while. If anyone has any
suggestions, I’d love to hear them.
Well, one of the reasons you might be stumped is that the problem as
posed does not appear to have a solution. I punched it in to Maxima:

(%o1) z+y+x=5
(%o2) -z+y+x=7
(%o4) (y-z)^3+(x-y)^3=(x-z)^3
(%i5) solve([%o1,%o2,%o4], [x,y,z]);
(%o5) [[x=-1,y=7,z=-1],[x=3,y=3,z=-1],[x=7,y=-1,z=-1]]

Since one of the equations is a cubic, there are three solutions, and
they are all smallish integers. But they are not consecutive!

Are you sure about the problem statement?

M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)

If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.

Shin-ichiro HARA schrieb:

You can use “algebra” (http://raa.ruby-lang.org/project/algebra/)
(…)

Thank you for the link, this looks interesting. In your solution, you
forgot:

three consecutive numbers, x, y and z

which makes it even simpler to solve.

Regards,
Pit

Absolutely certain. Unless of course this is a trick question. WHich I
doubt. But thanks for everyone’s help!

“Yannick G.” [email protected] writes:

(x - y)(x - y)(x - y) + (y - z)(y - z)(y - z) = (x - z)(x - z)(x - z)

I’m completely and utterly stumped. I intend on solving this via Ruby,
as doing it on pen and paper might take a while. If anyone has any
suggestions, I’d love to hear them.

This has already been answered by several others, but I’d like to
point out how it could have been solved easily with pencil and paper.

First off, look at the top two equations:

x + y + z = 5
x + y - z = 7

These tell you:

(x+y) + z = 5
(x+y) - z = 7

Although you can add the equations and then divide to get there, it
should be obvious at this point that (x+y) is the average of 5 and 7,
that is that x+y=6. Knowing that, you have that z=-1.

Now, the third equation is:

(x-y)**3 + (y+1)**3 = (x+1)**3

Now, one thing to hold onto is that when you have

an + bn = c**n

For any integer n > 2, the only way to get a solution with a, b, and c
integers is to have one of a, b, or c be 0. (*) Since a person put
this problem together to be solved, and since pencil-and-paper cubics
are a royal pain in the ass to solve in general if at least one
solution isn’t integers, let’s see if we can make it work with one of
those three bits we’re cubing equal to 0.

First off, see if it’ll work with x-y=0 :

Well, if x-y=0, then x=y and then of course
(x-y)**3 + (y+1)**3 = (x+1)**3
0 + (x+1)**3 = (x+1)**3
So, it’ll work if x-y=0.

Since x+y=6 one solution is then x=3,y=3,z=-1.

Now let’s see if it works with y+1=0:

Well, if y+1=0, then y=-1 and then:
(x-y)**3 + (y+1)**3 = (x+1)**3
(x+1)**3 + 0 = (x+1)**3
So, it’ll work if y+1=0.

Since x+y=6, another solution is x=7,y=-1,z=-1

Now let’s see if it works with x+1=0:

Well, if x+1=0, then x=-1 and then:
(x-y)**3 + (y+1)**3 = (x+1)**3
(-1-y)**3 + (y+1)**3 = 0
So, it’ll work if x+1=0.

Since x+y=6, the final solution is x=-1,y=7,z=-1

Since we’re dealing with a cubic and a linear equation once “z” is
solved for with the first two equations, we can only expect to get
at most three solutions, so we’re done. The three possible solutions
are:
x=3, y=3, z=-1
x=7, y=-1,z=-1
x=-1,y=7, z=-1

(*) There’s not room in this post to prove that here.

Daniel M. wrote:

Now, one thing to hold onto is that when you have

an + bn = c**n

For any integer n > 2, the only way to get a solution with a, b, and c
integers is to have one of a, b, or c be 0. (*)

[snip]

(*) There’s not room in this post to prove that here.

I’m not even sure there’s room to discuss whether the proof actually
exists.

M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)

If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.

On Tue, Mar 06, 2007 at 02:00:23AM +0900, Rick DeNatale wrote:

You can tell there’s no solution for consecutive X, Y, Z just from
looking at that last equation, each side boils down to a different
constant:

[ snip simple algebra ]

That’s why I wasn’t sure I understood the question. The OP seems to be
confirming my initial understanding of what was being asked, despite the
fact that the math doesn’t work according to that definition of the
problem.

You don’t need Ruby to solve this. You need any linear algebra program
which can graph a polynomial equation. That means you can solve it
with a \$20 calculator or the Grapher program which ships with OS X,
and probably a number of additional ways as well. Although creating a
general-case polynomial equation grapher would be a pretty interesting
way to solve it, it’s also definitely overkill.

On 3/5/07, M. Edward (Ed) Borasky [email protected] wrote:

(x - y)(x - y)(x - y) + (y - z)(y - z)(y - z) = (x - z)(x - z)(x - z)

I’m completely and utterly stumped.

they are all smallish integers. But they are not consecutive!

Are you sure about the problem statement?

You can tell there’s no solution for consecutive X, Y, Z just from
looking at that last equation, each side boils down to a different
constant:

Since the numbers are consecutive
y = x + 1
z = y + 1

Then:
x - y = x - (x+1) = -1
y - z = y - (y+1) = -1
x - z = x - (y+1) = x - (x+1+2) = -2

so:

(x-y)(x-y)(x-y) + (y-z)(y-z)(y-z) =
(-1)(-1)(-1) + (-1)(-1)(-1) =
-2

but:

(x - z)(x - z)(x - z) =
(-2)(-2)(-2) = -8

Rick DeNatale

My blog on Ruby

Rick DeNatale wrote:

x + y + z = 5
(%o4) (y-z)^3+(x-y)^3=(x-z)^3
constant:
so:

(x-y)(x-y)(x-y) + (y-z)(y-z)(y-z) =
(-1)(-1)(-1) + (-1)(-1)(-1) =
-2

but:

(x - z)(x - z)(x - z) =
(-2)(-2)(-2) = -8

My way is easier – punch the equations into an algebraic solver. The
batteries are dead on my TI-89, and I don’t have a Linux interface to it
anyhow, so I brought in the big guns – Maxima. Yeah, I know – it’s
easy to solve in your head. I still want to know where the “consecutive”
came from.

M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)

If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.

On Mon, Mar 05, 2007 at 05:34:25PM +0900, Pit C. wrote:

I’m completely and utterly stumped. I intend on solving this via Ruby,
as doing it on pen and paper might take a while. If anyone has any
suggestions, I’d love to hear them.

Yannick, you don’t need Ruby to solve this, just some basic Mathematics.

I’m glad you think so. I, for one, am still not sure exactly what the
requirements are for the proposed problem – or, more to the point,
exactly what the problem is.

On Tue, Mar 06, 2007 at 08:01:08AM +0900, Giles B. wrote:

You don’t need Ruby to solve this. You need any linear algebra program
which can graph a polynomial equation. That means you can solve it
with a \$20 calculator or the Grapher program which ships with OS X,
and probably a number of additional ways as well. Although creating a
general-case polynomial equation grapher would be a pretty interesting
way to solve it, it’s also definitely overkill.

I’m not sure why that was a reply to my email.

Yannick G. schrieb:

as doing it on pen and paper might take a while. If anyone has any
suggestions, I’d love to hear them.

Yannick, you don’t need Ruby to solve this, just some basic Mathematics.

Regards,
Pit