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: Matthias Gresz <GreMa_at_t-online.de>
Date: Wed, 17 Feb 1999 13:06:11 +0100
Message-ID: <36CAB0B3.8FF10B56@t-online.de>

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

Your programm doesn't die (as far as I understood) but just terminates do to your algorithm.
The end of the loop is determined by the condition x <= 2000000000 which is true since you start with x:=2.
Think you want rewrite your condition to x > 2000000000 or use the WHILE-construct:

	WHILE x <= 2000000000 LOOP
		...
	END LOOP;

HTH
Matthias
--
grema_at_t-online.de

Es gibt nichts Neues mehr.
Alles, was man erfinden kann, ist schon erfunden worden. Charles H. Duell, Leiter des US Patentamtes bei seinem Rücktritt 1899 Received on Wed Feb 17 1999 - 06:06:11 CST

Original text of this message

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