How about adding rational fraction to Python?

Mensanator mensanator at aol.com
Sun Feb 24 15:19:53 EST 2008


On Feb 24, 1:09�pm, Lie <Lie.1... at gmail.com> wrote:
> On Feb 25, 1:21 am, Mark Dickinson <dicki... at gmail.com> wrote:
>
> > On Feb 24, 1:09 pm, Lie <Lie.1... at gmail.com> wrote:
>
> > > And this limit is much lower than n!. I think it's sum(primes(n)), but
> > > I've got no proof for this one yet.
>
> > It's the least common multiple of the integers 1 through n, or
> > equivalently the product over all primes p <= n of the highest power
> > of p not exceeding n. �So for n = 100, it's:
>
> > 64 * 81 * 25 * 49 * 11 * 13 * 17 * ... rest of primes up to 100.
>
> > For general n, this number is of roughly the same order of magnitude
> > as e**n.
>
> Ah, yes, I meant product(primes(n)), please forgive my honest mistake
> which is partially caused by me not supposed to still be awake at this
> time of the day. And thanks for Mark for noticing the mistake, and
> here is the code I used:
>
> import fraction
> import random
>
> frac = fraction.frac
> ops = (frac.__add__, frac.__sub__)
>
> a = frac(random.randrange(1, 10), random.randrange(1, 10))
> b = frac(random.randrange(1, 10), random.randrange(1, 10))
>
> while True:
> � � o = ops[random.randrange(0, 2)]
> � � a = o(a, b)
> � � b = frac(random.randrange(1, 10), random.randrange(1, 10))
> � � print a
>
> I decided to keep the num/den limit low (10) because higher values
> might obscure the fact that it do have limits. And through further
> observations, I think it is sufficient if the limit is imposed in the
> denominator only (numerator can have any values it wanted to,
> denominator growth is determined only by the limit of denominator
> growth).
>
> I think I'll also post the code for the fraction class I used, if you
> have other fraction class that can automatically simplify, you could
> use that instead as this class suffers from a naive GCD
> implementation:

Out of curiosity, of what use is denominator limits?

The problems where I've had to use rationals have
never afforded me such luxury, so I don't see what
your point is.



More information about the Python-list mailing list