Re: Greatest Common Divisor

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>, Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Chapman Flack <chap(at)anastigmatix(dot)net>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Greatest Common Divisor
Date: 2020-01-04 10:25:17
Message-ID: CAEZATCVL1QTfSDaEO-XVFi7hwbdS=8eK483=nvLPNYqDMVT2uA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, 4 Jan 2020 at 09:37, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> wrote:
>
> Well Vik has now provided a numeric implementation and it doesn't
> appear to be too much code.
>

BTW, I did a bit of research into the efficiency of Euclid's
algorithm. It's actually quite interesting:

It turns out that the worst case is when the inputs are successive
values from the Fibonacci sequence. In that case, since
Fib(n)/Fib(n-1) = 1 remainder Fib(n-2), the algorithm will walk
backwards through the whole sequence before terminating, and the
result will always be 1.

For bigint inputs, the worst case is gcd(7540113804746346429,
4660046610375530309) which requires something like 90 divisions.
Testing that, it's still sub-millisecond though, so I don't think
there's any problem there.

OTOH, for numeric inputs, this could easily end up needing many
thousands of divisions and it's not hard to construct inputs that take
minutes to compute, although this is admittedly with ridiculously
large inputs (~10^130000), and AFAICS, the performance is OK with
"normal" sized inputs. Should we put a limit on the size of the
inputs? I'm not sure exactly how that would work, but I think it would
have to take into account the relative weights of the inputs rather
than just the maximum weight. At the very least, I think we need a
check for interrupts here (c.f. the numeric factorial function).
Perhaps such a check is sufficient. It's not like there aren't lots of
other ways to tie up the server.

There are apparently more efficient algorithms, but I think that
should definitely be kept out of scope for this patch.

Regards,
Dean

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2020-01-04 10:37:30 Re: PATCH: logical_work_mem and logical streaming of large in-progress transactions
Previous Message Dean Rasheed 2020-01-04 09:37:09 Re: Greatest Common Divisor