Saturday, July 2, 2011

Generating RATS Sequence

In this blog, my effort is to demonstrate the logic of generating the RATS Sequence.
All the numbers which are in the form of S(n)=( n+R(n)) is called RATS number. The sequence of RATS number is called RATS Sequence.
In the above formula : n denotes an Integer, R(n) denotes the Reversed Number, S(n) is an number , which is the sum of  the number n and its Reversal number R(n) , after sequencing the digits of the number in ascending order .
For further information on RATS Sequence, refer to   http://mathworld.wolfram.com/RATSSequence.html
Below code snippet demonstrates the basic logic for generating the RATS Sequence.

CREATE OR REPLACE PROCEDURE GENERATE_RATS_SEQUENCE(IN_FIRST_TERM IN NUMBER, IN_NTH_TERM IN NUMBER) IS
  lvRATSSequence VARCHAR2(32767):=NULL;
  lnPreviousTerm NUMBER:=0;
  lnRatsNumber NUMBER:=0;
  lnFirstTerm NUMBER:=0;
  FUNCTION GENERATE_RATS_NUMBER (IN_NUMBER IN NUMBER) RETURN NUMBER IS
    lnReverseNumber NUMBER:=0;
  BEGIN
    SELECT REVERSE(TO_CHAR(IN_NUMBER)) INTO lnReverseNumber FROM DUAL;
    RETURN SwapDigits((IN_NUMBER+ lnReverseNumber));
  END GENERATE_RATS_NUMBER;
BEGIN
  IF IN_FIRST_TERM IS NULL THEN
     lnFirstTerm:=1;
  ELSE
     lnFirstTerm:=IN_FIRST_TERM;
  END IF;
  lnPreviousTerm:=lnFirstTerm;
  lvRATSSequence:=lnFirstTerm;
  FOR I IN lnFirstTerm..IN_NTH_TERM LOOP   
    lnRatsNumber:= GENERATE_RATS_NUMBER(lnPreviousTerm);
    lnPreviousTerm:=lnRatsNumber;
    lvRATSSequence:=lvRATSSequence||','||lnPreviousTerm;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('RATS Sequence starting with '||IN_FIRST_TERM||' upto '||IN_NTH_TERM||' terms is : '||chr(10)||lvRATSSequence);
END GENERATE_RATS_SEQUENCE;

Friday, July 1, 2011

Sorting Collections in PL/SQL

In this blog, I made an attempt to demonstrate the usage of CAST and MULTISET pseudo-functions.  Pseudo functions are usually used with collections such as VARRAYS, NESTED TABLES and PL/SQL TABLES in Oracle. The below statement creates a user-defined Nested Table of primitive data type NUMBER.
CREATE OR REPLACE TYPE SwapTable IS TABLE OF NUMBER NOT NULL;
/
CAST pseudo function converts one collection type to another collection type.
MULTISET pseudo function returns a collection type records as a table.
The usage of CAST and MULTISET pseudo columns retrieves rows from a table as collection type.
The below function arranges the digits of a given number in ascending order.
CREATE OR REPLACE FUNCTION SwapDigits(IN_NUMBER IN NUMBER) RETURN NUMBER IS
ltSwappingTable SwapTable;
 ltSwappedTable SwapTable;
 lvSwappedNumber VARCHAR2(3000):=NULL;
BEGIN
 ltSwappingTable:= SwapTable ();
 ltSwappingTable.EXTEND(LENGTH(TO_CHAR(IN_NUMBER)));
 FOR I IN 1..LENGTH(TO_CHAR(IN_NUMBER)) LOOP
  ltSwappingTable(I):=SUBSTR(TO_CHAR(IN_NUMBER),I,1);
  DBMS_OUTPUT.PUT_LINE(ltSwappingTable(I));
 END LOOP;
 SELECT CAST(MULTISET(SELECT * FROM TABLE(ltSwappingTable) ORDER BY 1 ASC) AS SwapTable )
   INTO ltSwappedTable
   FROM DUAL;
 FOR J IN ltSwappedTable.FIRST..ltSwappedTable.LAST LOOP
  DBMS_OUTPUT.PUT_LINE(ltSwappedTable(J));
  lvSwappedNumber:=lvSwappedNumber||ltSwappedTable(J);
 END LOOP;
 RETURN TO_NUMBER(lvSwappedNumber);  
END SwapDigits;

Wednesday, June 29, 2011

Program to Generate the Catalan Series


Hi!
I would like to share some interesting programs related to the number theory. Here I am posting a Oracle PL/SQL program to generate the Catalan Series. This procedure takes an input parameter IN_UPPER_BOUND. This denotes the 'n' th term of the Catalan Series, where n is a integer number. By default, the lower bound is taken as 1.

Briefly, Catalan Series is defined as follows "A sequence of natural numbers which are named after the Beligian mathematician Eugène Charles Catalan".
Please follow the following link: http://en.wikipedia.org/wiki/Catalan_number for more information about the Catalan series.

Source Code:

CREATE OR REPLACE PROCEDURE GENERATE_CATALAN_SERIES(IN_UPPER_BOUND IN NUMBER) IS
 lvCatalanSeries VARCHAR2(32767):=NULL;
FUNCTION CATALAN(IN_NUMBER IN NUMBER) RETURN NUMBER IS
lnCatalan NUMBER:=0;
lnCatalanBound NUMBER:=0;
BEGIN
IF
IN_NUMBER=0 OR IN_NUMBER=1 THEN
lnCatalan:=1;
ELSE
lnCatalanBound:=IN_NUMBER-1;
lnCatalan:= ((((lnCatalanBound*2)+1)*2)/(lnCatalanBound+2)) * CATALAN(lnCatalanBound);
IF TO_NUMBER(SUBSTR(TO_CHAR(lnCatalan),INSTR(TO_CHAR(lnCatalan),'.')+1,LENGTH(TO_CHAR(lnCatalan)))) > 50 THEN
lnCatalan:=CEIL(lnCatalan);
ELSE
lnCatalan:=FLOOR(lnCatalan);
END IF;
END IF;
RETURN
lnCatalan;
END CATALAN;

BEGIN
FOR
i IN 0..IN_UPPER_BOUND LOOP
IF
lvCatalanSeries IS NULL THEN
lvCatalanSeries:=CATALAN(I);
ELSE
lvCatalanSeries:=lvCatalanSeries'->' CATALAN(I);
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Catalan Series upto 'IN_UPPER_BOUND' is : 'CHR(10)lvCatalanSeries);
END GENERATE_CATALAN_SERIES;

Here the function CATALAN is a recursive function.