Re: Re: Mapping output from a SEQUENCE into something non-repeating/colliding but random-looking?

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
Cc: Daniel Verite <daniel(at)manitou-mail(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Re: Mapping output from a SEQUENCE into something non-repeating/colliding but random-looking?
Date: 2009-05-03 11:40:35
Message-ID: 46D3A31F-F2CC-4014-8FE6-9040A077E792@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On May 3, 2009, at 9:00 AM, Craig Ringer wrote:

> CREATE OR REPLACE FUNCTION luhn_verify(int8) RETURNS boolean AS $$
> SELECT
> -- Add the digits, doubling odd-numbered digits (counting left with
> -- least significant as zero), and see if the sum is evenly
> -- divisible by zero.

I think you mean divisible by 10 here, numbers are generally not
divisible by zero ;)

Regardless, thanks for posting these functions, I'm sure they'll come
in handy some time.

>
> MOD(SUM(
> -- Extract digit `n' counting left from least significant as
> zero
> MOD( ( $1::int8 / (10^n)::int8 ), 10::int8)
> -- Double odd-numbered digits
> * (MOD(n,2) + 1)
> ), 10) = 0
> FROM generate_series(0, ceil(log( $1 ))::integer - 1) AS n;
> $$ LANGUAGE 'SQL'
> IMMUTABLE
> STRICT;
>
> COMMENT ON FUNCTION luhn_verify(int8) IS 'Return true iff the last
> digit
> of the input is a correct check digit for the rest of the input
> according to Luhn''s algorithm.'

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,49fd82b6129742129210600!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Paolo Saudin 2009-05-03 12:37:46 Rounding problems
Previous Message garry 2009-05-03 11:32:46 windows service