【问题标题】:To print table of factorials and their sum up to n using while loop. i need the sum of all the factorials使用 while 循环打印阶乘表及其总和为 n。我需要所有阶乘的总和
【发布时间】:2021-07-04 08:22:45
【问题描述】:
    //initializing  variables
            **DECLARE**
            i number:=1;
            n number;
            Fact number:=1;
            s number:=0;
    //declaring variables   
            **BEGIN**
            n:=&number;
    //starting the while loop
            while i>n
            loop
            Fact := Fact*i;
            dbms_output.put_line(i || ' factorial = ' || Fact);
            i:=i+1;
            s:=s+Fact; // this doesn't work? why
            **END** loop; // the loop ends
            dbms_output.put_line('Sum of the digits is: ' ||s);
            **END**; // the program ends
            /

【问题讨论】:

    标签: oracle plsql sum factorial digits


    【解决方案1】:

    除了语法错误之外,您使用的while 条件似乎是错误的。不是i > n,而是i <= n

    修复后,结果如下:

    SQL> set serveroutput on
    SQL>
    SQL> declare
      2    i     number := 1;
      3    n     number;
      4    fact  number := 1;
      5    s     number := 0;
      6  begin
      7    n := &par_number;
      8    while i <= n loop
      9      fact := fact * i;
     10      dbms_output.put_line(i
     11                           || ' factorial = '
     12                           || fact);
     13      i := i + 1;
     14      s := s + fact;
     15    end loop;
     16
     17    dbms_output.put_line('Sum of the digits is: ' || s);
     18  end;
     19  /
    Enter value for par_number: 3
    1 factorial = 1
    2 factorial = 2
    3 factorial = 6
    Sum of the digits is: 9
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    【讨论】:

      【解决方案2】:

      由于 pl/sql 的目的是提供简单的 SQL 接口,我提供了一个基于 SQL 的解决方案。除了打印结果外,这可以通过一条 SQL 语句来完成。有了这个要求,下面就严格使用游标循环来提供打印功能。

      set serveroutput on 
      declare
        total_factorial number; 
      begin 
          for factorial in 
              ( with fact (f1, f2, fs, n) as 
                   ( select 1, 1, 1, 0 from dual 
                      union all 
                      select f2 
                           , (n+1)*f2  
                           , fs+ (n+1)*f2 
                           , n+1
                        from fact            
                       where n < &N_FAC
                    )
               select n, f2, fs
                 from fact
             )
          loop
              total_factorial := factorial.fs;
              dbms_output.put_line( to_char(factorial.n,'fm990') ||' factorial is ' ||  
                                    to_char(factorial.f2)
                                  );
          end loop; 
          
          dbms_output.put_line('Sum of the digits is: ' || total_factorial);
      end ;  
      

      结果:
      0 阶乘是 1
      1 阶乘是 1
      2 阶乘是 2
      3 阶乘是 6
      数字总和为:10

      注意:“数字总和”的结果与@Littlefoot 的答案不同。这容纳0!定义为 1。您可以通过更改

      来删除它 select 1, 1, 1, 0 from dual to select 1, 1, 1, 1 from dual

      【讨论】:

        猜你喜欢
        • 2022-01-08
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多