Maths in Ruby

Y

Yannick Grams

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!

_________________________________________________________________
Advertisement: Meet Sexy Singles Today @ Lavalife - Click here
http://a.ninemsn.com.au/b.aspx?URL=...754951090&_r=endtext_lavalife_dec_meet&_m=EXT
 
P

Pit Capitain

Yannick said:
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.

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

Regards,
Pit
 
C

Chad Perrin

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.
 
S

Shin-ichiro HARA

Hi,

2007/3/5 said:
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
 
M

M. Edward (Ed) Borasky

Yannick said:
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.
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?
 
D

Daniel Martin

Yannick Grams said:
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.

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

a**n + b**n = 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.
 
M

M. Edward (Ed) Borasky

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

a**n + b**n = 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.

<ducking>
 
R

Rick DeNatale

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?

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
 
C

Chad Perrin

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.
 
G

Giles Bowkett

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.

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.
 
C

Chad Perrin

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.
 
M

M. Edward (Ed) Borasky

Rick said:
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?

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

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

Similar Threads

Ruby .exe Maker 2
File Question 7

Members online

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top