Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Heisenberg Uncertainty Principle Aside,..

Heisenberg Uncertainty Principle Aside,..

From: <cathexis_at_erols.com>
Date: Wed, 17 Feb 1999 03:37:02 GMT
Message-ID: <36ca3581.513330894@news.erols.com>


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:

  1. Is the loop syntactically correct?
  2. Does it generate a divide by zero error? I think yes.
  3. Would this be a valid var. declare/init. X_var number; --- or, X_var number :=2; Y_var number := X_var -1 ; --<<---- What I most want to know here.
  4. Would this be a legal in a While loop: While 1...X_var-1 loop ?

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;

end;
/
 My grateful thanks,

                        Cathexis Received on Tue Feb 16 1999 - 21:37:02 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US