From: jweirich@... Date: 2001-11-13T13:41:20+09:00 Subject: [ruby-talk:24983] Re: Teach your kid math w/ruby >>>>> "Peter" == Peter J Kernan writes: Peter> I wrote the following program to help my kid learn to do Peter> simple math problems quickly and correctly. My son is in High School and often comes to me with math questions late in the evening. Since I'm usually on the computer by that time, I've discovered the joy of using Ruby to solve math problems. I'll check his answers by whipping out a quick Ruby solution to whatever he is working on. One of my favorites was a 8 line function to calculate synthetic division. I've also done line intersection and quadratic formulas and a few polynomial manipulations. The code is nothing really fancy, but the ability to go from need to code in quick steps is amazing. Here's the synthetic division example. Poly is an array of polynomial coefficients (e.g. x^2 + 3x - 5 => [1, 3, -5]) def synthetic_divide(a, poly) poly = poly.dup result = [poly.shift] while poly.size > 0 result << (a * result[-1]) + poly.shift end result end For example, What's the result of dividing (x^3 + 4x^2 - 11x - 30) by (x + 2)? irb(main):002:0> synthetic_divide(-5, [1, 4, -11, -30]) [1, -1, -6, 0] Answer: (x^2 - x - 6) remainder=0 I wish *I* had Ruby when I was taking math in school. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)