Recently while at my local bookstore I found myself browsing through this book. and one of the exercises that caught my eye was to write a program to compute this sum:
30 29 1 ---- + ---- + ... + ---- 1 2 30
I immediately thought it would be interesting to compute this exactly as a fraction and also to generalize the problem by replacing 30 with an arbitrary integer n. I knew perl had support for arbitrary precision integers, and I was curious to find out if the numerators or denominators formed any recognizable pattern.

So when I got home I consulted the documentation for bigrat, and naively coded up this loop:

use strict; use warnings; use bigrat; for my $n (1..30) { my $s = 0; for my $k (1..$n-1) { $s += $k / ($n-$k); } print "n = $n, s = $s\n"; }
Needless to say, it didn't work as I expected - I got output like this:
n = 1, sum = 0 n = 2, sum = 1 n = 3, sum = 2.5 n = 4, sum = 4.333333333333333
I expected to get 5/2 for n = 3 and 13/3 for n = 4, etc. After reconsulting the docs and some more experimentation, I finally came up with this solution:
for my $n (1..30) { my $s = 0; for my $k (1..$n-1) { $s += 1/1 * $k / ($n-$k); } print "n = $n, s = $s\n"; }
which gave what I wanted:
n = 1, sum = 0 n = 2, sum = 1 n = 3, sum = 5/2 n = 4, sum = 13/3
Having tackled that problem, I went on to extract the numerator and denominator sequences so I could plug them into The On-Line Encyclopedia of Integer Sequences. Again, my naive approach was to simply call ->numerator and ->denominator on each sum:
for my $n (1..30) { ... push(@nums, $s->numerator); push(@denoms, $s->denominator); }
Alas, I ran into problems on the very first term:
Can't locate object method "numerator" via package "Math::BigInt" at . +/frac line ...
Indeed, this program illustrates the problem:
use bigrat; my $x = 1; print $x->numerator, "\n";
So I guess my questions are:
  1. Why didn't bigrat figure out that the expression $k/($n-$k) should produce a bigrat and not a floating point number?
  2. Why can't I call numerator and denominator on a rational that's actually an integer? Should those methods should be added to Math::BigInt or somehow enabled when bigrat is used?
I'm using 5.8.8 if that's of any consequence.

Update 1: fixed typo with expected results.


In reply to issues using bigrat by pc88mxer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.