FACTORIAL

Dúvidas, dicas e truques de PL/SQL. Aqui também vão assuntos relacionados a pacotes, triggers, funções, Java-Stored Procedures, etc
Post Reply
druffes
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 31
Joined: Fri, 26 Jan 2007 6:23 pm
Location: Campo Mourão - PR

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 ....

Select all

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

druffes
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 31
Joined: Fri, 26 Jan 2007 6:23 pm
Location: Campo Mourão - PR

I tried to use this other command, but tb error ....

Select all

DECLARE 
   VA_FATORIAL   NUMBER(10)  :='&NUMERO'; 
   VA_FINAL      NUMBER(10)  := 1; 
   VA_MULT       NUMBER(10)  := VA_FATORIAL -1; 
   VA_RESULTADO  NUMBER(10); 
BEGIN 
   LOOP 
     EXIT WHEN VA_FATORIAL = VA_FINAL; 
     VA_FATORIAL * VA_MULT := VA_RESULATDO; 
   END LOOP; 
END; 
/ i] [/b]
ARF
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 41
Joined: Thu, 03 Aug 2006 9:30 am
Location: Criciúma - SC

ARF
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 41
Joined: Thu, 03 Aug 2006 9:30 am
Location: Criciúma - SC

Sorry, it looks like the link changed.

But what was on the link is this:

Select all

 
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 
/ Code]
User avatar
dr_gori
Moderador
Moderador
Posts: 5027
Joined: Mon, 03 May 2004 3:08 pm
Location: Portland, OR USA
Contact:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

You're right:
I found the following code that uses analytical functions to generate the factorial:

Select all

select rownum, 
exp(sum(ln(rownum)) over (order by rownum)) Factorial 
from user_objects 
where rownum < 21; 
druffes
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 31
Joined: Fri, 26 Jan 2007 6:23 pm
Location: Campo Mourão - PR

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 .....

Select all

DECLARE 
   VA_NUMERO     NUMBER(10)  :='&NUMERO'; 
   VA_FINAL      NUMBER(10)  := 1; 
   VA_MULT       NUMBER(10)  := VA_NUMERO -1; 
   VA_RESULTADO  NUMBER(10)  := VA_MULT; 
   VA_FATORIAL   NUMBER(10); 
BEGIN 
     EXIT WHEN VA_NUMERO = VA_FINAL LOOP; 
     VA_NUMERO * VA_MULT := VA_RESULTADO; 
     VA_RESULTADO*VA_MULT :=VA_FATORIAL; 
   END LOOP; 
END; 
/
[/i] I can not get the functions fit if anyone knows where I'm wrong and can help me would be very grateful .....
Thanks ....
Trevisolli
Moderador
Moderador
Posts: 2016
Joined: Wed, 12 Jan 2005 3:25 pm
Location: Araraquara - SP
Abraço,

Trevisolli
OCA Oracle PL/SQL Developer Certified Associate
OCP Oracle Forms Developer Certified Professional
Araraquara-SP

Was this brow?

Select all

 
 
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;   
 

Hugs,

: -O
lfarah
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Posts: 3
Joined: Mon, 12 Mar 2007 5:16 pm
Location: são Paulo - SP

To leave more or less as you did:

Select all

DECLARE  
   VA_FATORIAL NUMBER(10) := 5; -- AQUI você ENTRA COM O NUMERO QUE QUER CALCULAR O FATORIAL 
   FAT    	   NUMBER(10) := 1; 
BEGIN  
   WHILE VA_FATORIAL > 1 LOOP 
   FAT := VA_FATORIAL * FAT;  
   VA_FATORIAL := VA_FATORIAL - 1; 
   END LOOP; 
   dbms_output.put_line('FATORIAL -> '||FAT); 
 END; 

- I think it does :-)
druffes
Rank: Programador Pleno
Rank: Programador Pleno
Posts: 31
Joined: Fri, 26 Jan 2007 6:23 pm
Location: Campo Mourão - PR

Many thanks for the help of all who posted their ideas and tips.
I did this and it worked:

Select all

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;
personal vlw .... [/b]]
User avatar
dr_gori
Moderador
Moderador
Posts: 5027
Joined: Mon, 03 May 2004 3:08 pm
Location: Portland, OR USA
Contact:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

For those who like recursiveness (I do not, hehehe), there goes:

Select all

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.
Example:

Select all

SQL> select thomas_factorial ( 3 ) from dual; 
 
THOMAS_FACTORIAL(3) 
------------------- 
           6.0E+000 
 
SQL> select thomas_factorial( 4 ) from dual; 
 
THOMAS_FACTORIAL(4) 
------------------- 
           2.4E+001 
 
SQL> select thomas_factorial ( 10 ) from dual; 
 
THOMAS_FACTORIAL(10) 
-------------------- 
          3.629E+006 
 
SQL> 
Post Reply
  • Information
  • Who is online

    Users browsing this forum: Bing [Bot] and 1 guest