Home » Other » Training & Certification » how can i catch prime numbers with While Loop ? (10g , XP SP2)
icon5.gif  how can i catch prime numbers with While Loop ? [message #319432] Sun, 11 May 2008 02:52 Go to next message
Hany Freedom
Messages: 256
Registered: May 2007
Location: Egypt
Senior Member

I want to make Pl/SQL program using WHILE LOOP , which will accept integer from 1 to 50 and generate the prime numbers as shown in the following output :-

The prime numbers from 1 to 50 are as follows :
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47


How suppose that's done ?!
Re: how can i catch prime numbers with While Loop ? [message #319433 is a reply to message #319432] Sun, 11 May 2008 03:30 Go to previous messageGo to next message
vamsi kasina
Messages: 2096
Registered: October 2003
Location: Riyadh, Saudi Arabia
Senior Member
What you have tried till now?
Paste your code.

By,
Vamsi
Re: how can i catch prime numbers with While Loop ? [message #319448 is a reply to message #319432] Sun, 11 May 2008 06:28 Go to previous messageGo to next message
Michel Cadot
Messages: 59427
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
http://en.wikipedia.org/wiki/Prime_number
http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm

Regards
Michel
Re: how can i catch prime numbers with While Loop ? [message #319454 is a reply to message #319448] Sun, 11 May 2008 09:34 Go to previous messageGo to next message
Hany Freedom
Messages: 256
Registered: May 2007
Location: Egypt
Senior Member

those links are define what Prime numbers means ..... but what I need is in my first post.
Re: how can i catch prime numbers with While Loop ? [message #319455 is a reply to message #319432] Sun, 11 May 2008 09:54 Go to previous messageGo to next message
BlackSwan
Messages: 22911
Registered: January 2009
Senior Member
Are you unwilling or incapable of using GOOGLE to search for "Prime number generator"?
Re: how can i catch prime numbers with While Loop ? [message #319459 is a reply to message #319454] Sun, 11 May 2008 10:06 Go to previous messageGo to next message
Michel Cadot
Messages: 59427
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
The second link gives several algorithms and even code to generate prime numbers... but you are too lazy to read it.

Regards
Michel

[Updated on: Sun, 11 May 2008 10:06]

Report message to a moderator

Re: how can i catch prime numbers with While Loop ? [message #319460 is a reply to message #319432] Sun, 11 May 2008 11:27 Go to previous messageGo to next message
Hany Freedom
Messages: 256
Registered: May 2007
Location: Egypt
Senior Member

for anacedent :-
I searched at Google about what i need and didn't find it .
for Michel Cadot:-
I'm not lazy , but your second link don't related by any kind to oracle ...... look to it by your self.
Re: how can i catch prime numbers with While Loop ? [message #319463 is a reply to message #319460] Sun, 11 May 2008 11:55 Go to previous messageGo to next message
Barbara Boehmer
Messages: 7995
Registered: November 2002
Location: California, USA
Senior Member
You should not expect to find all of the answers to your homework assignments just by searching or asking or expect to learn anything that way. This is a simple task that could be done in various ways and apparently you have been assigned to use a specific method (while loop). If you know what a while loop is (if not, then search the documentation) and you know what a prime number is, then this should be an easy assignment. Use a variable for your number with a starting value of one and create your while loop such that it loops while that variable is less than your maximum number incrementing the variable by 1 after the rest of the processing in the loop. Within the loop, check whether the number is a prime number and output it if it is and don't if it isn't. To check whether it is a prime number, check whether it is divisible by any numbers other than 1 or itself. You may want to use a nested loop to do this.
Re: how can i catch prime numbers with While Loop ? [message #319466 is a reply to message #319460] Sun, 11 May 2008 12:45 Go to previous messageGo to next message
Michel Cadot
Messages: 59427
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
your second link don't related by any kind to oracle

Good grief! The code is in C, PL/SQL (in its most basic form) is close to C, you just have to change the keywords.

Regards
Michel
Re: how can i catch prime numbers with While Loop ? [message #319512 is a reply to message #319432] Mon, 12 May 2008 01:15 Go to previous messageGo to next message
flyboy
Messages: 1776
Registered: November 2006
Senior Member
Even the first link contains reference to Sieve of Eratosthenes algorithm. Unfortunately, it is described in words and illustrated with pseudocode, no PL/SQL too.
But it shall not be difficult for the original poster to rewrite-it to PL/SQL.
Re: how can i catch prime numbers with While Loop ? [message #319646 is a reply to message #319432] Mon, 12 May 2008 08:26 Go to previous messageGo to next message
BlackSwan
Messages: 22911
Registered: January 2009
Senior Member
>But it shall not be difficult for the original poster to rewrite-it to PL/SQL.
Nothing is impossible for the person who does not have to do it.

You can lead some folks to knowledge, but you can't make them think!
Re: how can i catch prime numbers with While Loop ? [message #319692 is a reply to message #319460] Mon, 12 May 2008 12:20 Go to previous message
Barbara Boehmer
Messages: 7995
Registered: November 2002
Location: California, USA
Senior Member
It should only take a few lines of code to do this.

SCOTT@orcl_11g> SET SERVEROUTPUT ON
SCOTT@orcl_11g> DECLARE
  2    ...;
  3    ...;
  4  BEGIN
  5    WHILE ... LOOP
  6  	 ...;
  7  	 WHILE ... LOOP
  8  	   IF ... THEN ...;
  9  	   ELSIF ... THEN ...;
 10  	   END IF;
 11  	   IF ... THEN ...;
 12  	   END IF;
 13  	   ...;
 14  	 END LOOP;
 15  	 ...;
 16    END LOOP;
 17    ...;
 18  END;
 19  /
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47

PL/SQL procedure successfully completed.

SCOTT@orcl_11g> 

Previous Topic: PL/SQL help, I need to write tigger, function, procedure
Next Topic: Updatable views + Key preserved table (merged)
Goto Forum:
  


Current Time: Sat Oct 25 18:40:18 CDT 2014

Total time taken to generate the page: 0.25277 seconds