2010-11-28 45 views
1

我已經在PLSQL中編寫了代碼。 在我需要的地方檢查數字立方體的總和=數字本身。檢查數字立方體的總和=數字本身

我試着遵守算法,仍然有一些errors.Please幫助。 我是PLSQL的新手。

以下是我的代碼:

set serveroutput on; 



Declare 

    I number(4); 
    Sum number(4):=0; 
    C number(15):=10; 

Begin 
    for I in 1..999 
    loop 

    --  dbms_output.put_line(I); 



     Sum:=power(mod(I,C),3); 

     while mod(I,C) 
     loop 

      Sum:=power(mod(mod(I,C),C),3); 


      C:=C*10; 

     end loop;  


     if Sum=I then 

      dbms_output.put_line(I);   

     end if; 

    end loop; 

End; 

/
+0

數據庫可以做到這一點?太棒了。 – Yehonatan 2010-11-28 15:04:39

回答

3

sum是PLSQL一個關鍵的詞,你不應該使用,作爲一個變量名。

這裏是你的問題的解決方案:

SET serveroutput ON format wraped; 
DECLARE 
    i INTEGER := 153; 
    j INTEGER; 
    summ INTEGER := 0; 
BEGIN 
    j  := i; 
    WHILE i > 0 
    LOOP 
    summ := summ + MOD(i,10) ** 3; 
    i := FLOOR (i/10); 
    END LOOP; 
    IF summ = j THEN 
    dbms_output.put_line('Sum of cubes of digits is EQUAL to the number'); 
    ELSE 
    dbms_output.put_line('Sum of cubes of digits is NOT EQUAL to the number'); 
    END IF; 
END; 

的解決方案適用於任何整數i,這是NUMBER(38)。