I'm trying to set up a logic where I bid a number and this logic provides me the factorial of this number ....
I'm trying to use this command but it's not working, if someone can help me I thank you very much ....
DECLARE
VA_FATORIAL NUMBER(10) :='&NUMERO';
VA_FINAL NUMBER(1) := 1;
VA_MULT NUMBER(10) := VA_FATORIAL -1;
BEGIN
LOOP
EXIT WHEN VA_FATORIAL=VA_FINAL
OR VA_FATORIAL * VA_MULT;
VA_MULT := VA_FATORIAL-1;
END LOOP;
END;
[99] You must have noted that I opened some topics is that I'm taking a course and I started to see pl / sql to two lessons, so so many doubts I hope I'm not bothering and who knows but forward can contribute the forum by taking the doubt of others who need .. ..
please a lot for the help you are giving me .... Thank you
Create an object to do the cumulative multiplication
create or replace type cumulative_mult_type as object ( total varchar2(4000), static function ODCIAggregateInitialize(sctx IN OUT cumulative_mult_type ) return number, member function ODCIAggregateIterate(self IN OUT cumulative_mult_type , value IN NUMBER) return number, member function ODCIAggregateTerminate(self IN cumulative_mult_type , returnValue OUT NUMBER, flags IN number) return number, member function ODCIAggregateMerge(self IN OUT cumulative_mult_type, ctx2 IN cumulative_mult_type ) return number );
Define the body
create or replace type body cumulative_mult_type is static function ODCIAggregateInitialize(sctx IN OUT cumulative_mult_type) return number is begin sctx := cumulative_mult_type( 1 ); return ODCIConst.Success; end; member function ODCIAggregateIterate(self IN OUT cumulative_mult_type, value IN NUMBER ) return number is begin self.total := self.total * value; return ODCIConst.Success; end; member function ODCIAggregateTerminate(self IN cumulative_mult_type, returnValue OUT NUMBER, flags IN number) return number is begin returnValue := self.total; return ODCIConst.Success; end; member function ODCIAggregateMerge(self IN OUT cumulative_mult_type, ctx2 IN cumulative_mult_type) return number is begin self.total := self.total * ctx2.total; return ODCIConst.Success; end;end;
Create the aggregate function
CREATE or replace FUNCTION factorial(input NUMBER ) RETURN NUMBER PARALLEL_ENABLE AGGREGATE USING cumulative_mult_type;
Populate the table
SQL> create table a ( col1 NUMBER );Table created.SQL> INSERT INTO a VALUES (1);1 row created.SQL> INSERT INTO a VALUES (2);1 row created.SQL> INSERT INTO a VALUES (3);1 row created.SQL> INSERT INTO a VALUES (4);1 row created.
Now, run the factorial function
SQL> SELECT factorial( col1 ) from a;FACTORIAL(COL1)--------------- 24
I understood more ow less what you sent, the problem is that I need to, get a logic that you put a number and I inserting this number it provides me the factor of the same ......
I think it's more ow less by this path .....
DECLARE
v_teste NUMBER := 0;
FUNCTION fun_fatorial (p_nro IN NUMBER)
RETURN NUMBER
IS
v_nro NUMBER := 1;
BEGIN
FOR x IN 1..p_nro
LOOP
v_nro := v_nro * x;
END LOOP;
RETURN v_nro;
END;
BEGIN
v_teste := fun_fatorial(5);
dbms_output.put_line('Fatorial: '||v_teste);
END;
DECLARE
VA_NUM NUMBER(3) :='&NUMERO';
VA_FAT NUMBER := VA_NUM;
BEGIN
FOR VA_MULT IN 2..VA_NUM-1 LOOP
VA_FAT := VA_FAT * VA_MULT;
END LOOP;
DBMS_OUTPUT.PUT_LINE(' O FATORIAL DE '||VA_NUM||' É: '||VA_FAT);
END;
SQL> create or replace function thomas_factorial( n binary_double) return binary_double is
2 begin
3 if n<=1 then
4 return 1;
5 else
6 return n * thomas_factorial (n-1);
7 end if;
8 end;
9 /
Function created.