Centered triangular number in PL/SQL

Prerequisite PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

Given n and task is to find nth centered triangular number. A centered triangular number is a centered number that represents a triangle with a dot in the center and all other dots surrounding the center in successive triangular layers.
Examples:

Input: n = 6 
Output: 64

Input: n = 10
Output: 166

The first few centered triangular number series are:
1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 199, 235, 274, 316, 361, 409, 460………………………



Approach
nth Term of centered triangular number is given by:

 CT_{n}=(3n^2+3n+2)/2 

Below is the required implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

--PL/SQL Program to find the nth Centered triangular number 
-- declaration section 
DECLARE 
  x NUMBER; 
  n NUMBER; 
    
  --utility function 
  FUNCTION Centered_triangular_num(n IN NUMBER) 
    RETURN NUMBER 
  IS 
    z NUMBER; 
  BEGIN 
    
    --formula applying 
    z := (3 * n * n + 3 * n + 2) / 2; 
    RETURN z; 
  END
    
  --driver code 
  BEGIN 
    n := 3; 
    x := centered_triangular_num(n); 
    dbms_output.Put_line(x); 
    n := 12; 
    x := centered_trigunal_num(n); 
    dbms_output.Put_line(x); 
  END
  --End of program

chevron_right


Output:

19
235

References:
https://en.wikipedia.org/wiki/Centered_triangular_number



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.