Centered triangular number in PL/SQL
Prerequisite
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
Below is the required implementation:
--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 |
Output:
19 235
References:
https://en.wikipedia.org/wiki/Centered_triangular_number