The equation challenge:

4 views
Skip to first unread message

Alan Eliasen

unread,
Jul 22, 2010, 8:00:30 PM7/22/10
to emergi...@googlegroups.com

The challenge from my presentation:

Do you get this result right?

for:
x=77617
y=33096

calculate:
((333 + 3/4) - x^2) y^6 + x^2 (11x^2 y^2 - 121 y^4 - 2) + (5 + 1/2) y^8 + x/(2y)

   Note that the equation simplifies to:
   x / (2 y) - 2

  No fair solving that version, but you can use it to check your answer!

  As stated, I'll buy you a beer if you get the right answer.  If you don't even get the sign right, you have to buy me one!  :)

   Alan Eliasen

  

Ola Bini

unread,
Jul 22, 2010, 8:03:33 PM7/22/10
to emergi...@googlegroups.com, Alan Eliasen
On 2010-07-22 17.00, Alan Eliasen wrote:
>
> The challenge from my presentation:
>
> Do you get this result right?
>
> for:
> x=77617
> y=33096
>
> calculate:
> ((333 + 3/4) - x^2) y^6 + x^2 (11x^2 y^2 - 121 y^4 - 2) + (5 + 1/2) y^8
> + x/(2y)
>
> Note that the equation simplifies to:
> x / (2 y) - 2


iik> f = method(x, y, ((333 + 3/4) - x**2) * y**6 + x**2 * (11 * x**2 *
y**2 - 121 * y**4 - 2) + (5 + 1/2) * y**8 + x/(2 * y))
f = method(x, y, ((333 + 3/4) - x**2) * y**6 + x**2 * (11 * x**2 * y**2
- 121 * y**4 - 2) + (5 + 1/2) * y**8 + x/(2 * y))
+> f:method(x, y, ((333 +(3 /(4))) -(x **(2))) *(y **(6)) +(x **(2) *(11
*(x **(2)) *(y **(2)) -(121 *(y **(4))) -(2))) +((5 +(1 /(2))) *(y
**(8))) +(x /(2 *(y))))

iik> x = 77617
x = 77617
+> 77617

iik> y = 33096
y = 33096
+> 33096

iik> f(x, y)
f(x, y)
+> -54767/66192

Correct?

>
> No fair solving that version, but you can use it to check your answer!
>
> As stated, I'll buy you a beer if you get the right answer. If you
> don't even get the sign right, you have to buy me one! :)
>
> Alan Eliasen
>
>

> --
> Web: http://emerginglangs.com/
> Twitter: http://twitter.com/emerginglangs
> Group: http://groups.google.com/group/emerginglangs?hl=en


--
Ola Bini (http://olabini.com)
Ioke - JRuby - ThoughtWorks

"Yields falsehood when quined" yields falsehood when quined.

Ola Bini

unread,
Jul 22, 2010, 8:26:34 PM7/22/10
to emergi...@googlegroups.com
A Ruby solution that really gets it wrong (but the right sign at least)

def f(x, y)


((333 + 3/4) - x**2) * y**6 + x**2 * (11 * x**2 * y**2 - 121 * y**4 -
2) + (5 + 1/2) * y**8 + x/(2 * y)

end

def f2(x, y)
x / (2 * y) - 2
end

x = 77617
y = 33096

x2 = 77617.0
y2 = 33096.0

puts f(x, y)
puts f2(x, y)

puts f(x2, y2)
puts f2(x2, y2)

Result:
-719737395591900115423969299982909441
-1
-7.19737395591901e+35
-0.827396059946821

Cheers

André Pang

unread,
Jul 22, 2010, 8:36:39 PM7/22/10
to emergi...@googlegroups.com

Haskell has a Rational type that gets it right:

% cat Challenge.hs
x = 77617 :: Rational
y = 33096 :: Rational

f = ((333 + 3/4) - x^2) * y^6 + x^2 * (11*x^2 * y^2 - 121 * y^4 - 2) + (5 + 1/2) * y^8 + x/(2*y)
g = x / (2 * y) - 2

% ghci Challenge.hs
GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( Challenge.hs, interpreted )
Ok, modules loaded: Main.
*Main> x
77617 % 1
*Main> y
33096 % 1
*Main> :t x
x :: Rational
*Main> :t y
y :: Rational
*Main> f
(-54767) % 66192
*Main> g
(-54767) % 66192
*Main> f == g
True

As an aside, I'd love to see all languages use rational types by default for all numeric literals with decimal places, and make such literals floats only if explicitly asked (e.g. with the standard f postfix). Floating-point numbers have killed too many people to let them loose on people who don't understand them.

Charles Oliver Nutter

unread,
Jul 22, 2010, 11:00:17 PM7/22/10
to emergi...@googlegroups.com

Here's the ruby version fixed using the rational library.

require 'rational'

def f(x, y)
  ((333 + 3.rdiv(4)) -x**2) * y**6 + x**2 * (11 * x**2 * y**2 -121 * y**4 -2) + (5 + 1.rdiv(2)) * y**8 + x.rdiv(2 * y)
end

x = 77617
y = 33096

puts f(x,y)

=> -54767/66192

Fixed it with the JRuby-based "Ruboto IRB" app on my Android phone :) JVM languages with interpreters available FTW.

- Charlie (from mobile)

On Jul 22, 2010 5:36 PM, "André Pang" <andre...@gmail.com> wrote:

On Jul 22, 2010, at 5:00 PM, Alan Eliasen wrote:

> The challenge from my presentation:
>

> Do you ...

Group: http://groups.g...

Reply all
Reply to author
Forward
0 new messages