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 -> Re: Heisenberg Uncertainty Principle Aside,..

Re: Heisenberg Uncertainty Principle Aside,..

From: Connor McDonald <mcdonald.connor.cs_at_bhp.com.au>
Date: Wed, 17 Feb 1999 16:54:36 +0800
Message-ID: <36CA83CC.278D@bhp.com.au>


cathexis_at_erols.com wrote:
>
> 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

How about...

declare

    sum_of_divisors number ;
    upper_limit number := 100;
begin
  for x in 6 .. upper_limit loop
    sum_of_divisors := 1;
    for y in 2 .. trunc(x/2) loop

      if mod(x,y) = 0 then
        sum_of_divisors := sum_of_divisors + (x/y);
      end if;

    end loop;
    if x = sum_of_divisors then
      dbms_output.put_line(x);
    end if;
  end loop;
end;
/

--



Connor McDonald
BHP Information Technology
Perth, Western Australia
"Never wrestle a pig - you both get dirty and the pig likes it..." Received on Wed Feb 17 1999 - 02:54:36 CST

Original text of this message

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