Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Heisenberg Uncertainty Principle Aside,..
Greetings Ng,
What follows is some failed code. Just for kicks, and to practice some PL/SQL, I've been trying to write a program that would compute *perfect numbers*. A perfect number is any integer x, such that the sum of the proper divisors of x are equal to x, but not including x itself. Example: The first perfect number is 6 because---- 1+2+3=6.
Well, my code compiles okay, but when I try to run it it SQL*PLUS died faster than Elvis on the crapper. I don't care too much about the exit condition because I want it to go on and on. Of course, I am limited by the size of the datatype ( and my hard-drive!).
Any thoughts would be welcome, though I know I could do better. I'd enjoy some feedback. Some questions I could ask include:
Also, please excuse my obscure var. names, it's not a comm'l app. just fun.
-------------Program Perfect Number--------- declare
q number;
x number :=2;
t number ;
s number ;
begin
loop q:= x/x-1; ---loop decrements from value x if mod(x,x-1) =0 then s :=s +(x/x-1); ----sum of quotients if mod(x,x-1) =0 then t := t+(x-1); ----sum of proper divisors if s/2 = x ----s/2,i.e.,if x=6 then (1+2+3+6)/2 =x and t = x then dbms_output.put_line( x || ' is a perfect number.'); x :=x+1; ---increment loop to next int end if; end if; end if; exit when x <= 2000000000; --- or type number(-1) end loop;
Cathexis Received on Tue Feb 16 1999 - 21:37:02 CST