Thread: how to retrive prime numbers from a Number column using only SQL Query


Permlink Replies: 7 - Pages: 1 - Last Post: May 3, 2007 1:36 AM Last Post By: Satyaki_De
user560002

Posts: 3
Registered: 02/20/07
how to retrive prime numbers from a Number column using only SQL Query
Posted: May 2, 2007 10:40 PM
Click to report abuse...   Click to reply to this thread Reply
Hi all,

Can any one help me in writing a SQL Query.I have a column which is of number type
and have a data of 1 - 100 numbers.Is there any way in which I can retrive prime numbers using a SQL Query only and not from PL/SQL Program.

Thanks in Advance.
Don.
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 2, 2007 11:06 PM   in response to: user560002 in response to: user560002
Click to report abuse...   Click to reply to this thread Reply
SQL> select l prime_number
2 from (select level l from dual connect by level <= 100)
3 , (select level m from dual connect by level <= 100)
4 where m<=l
5 group by l
6 having count(case l/m when trunc(l/m) then 'Y' end) = 2
7 order by l
8 /

PRIME_NUMBER

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

25 rijen zijn geselecteerd.

Regards,
Rob.
user560002

Posts: 3
Registered: 02/20/07
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 2, 2007 11:43 PM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hi Rob,

Thanks for the early reply, But pls, can u explain me clearly step-by-step , as I am new to SQL It's a bit difficult for me to understand.

Tx,
Don.
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 2, 2007 11:46 PM   in response to: user560002 in response to: user560002
Click to report abuse...   Click to reply to this thread Reply
But pls, can u explain me clearly step-by-step , as I am new to SQL

  1  select l prime_number
2 from (select level l from dual connect by level <= 100)
3 , (select level m from dual connect by level <= 100)
4 where m<=l
5 group by l
6 having count(case l/m when trunc(l/m) then 'Y' end) = 2
7 order by l
Sure, line 2 and 3 are both generating the numbers 1 until 100. Without a where clause they would give 10,000 rows, having all combinations of two numbers between 1 and 100.

The next step is to filter out the rows where m>l. After this step you have the pairs (1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1) et cetera. You will have 5,050 rows left at this stage.

Line 5 is grouping by the l column, to have 100 records again. The prime number check is in the having clause at line 6. You divide the l number by the m number and only when it can be divided ( l/m = trunc(l/m) ), you are counting. Prime numbers are the ones that have two m numbers (1 and the number l itself), so only those are displayed.

Hope this helps.

Regards,
Rob.
user560002

Posts: 3
Registered: 02/20/07
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 3, 2007 12:03 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hi Rob,

Thanks a lot. it is really helpful.Once again thankq for your early reply and explanation.

Tx,
Don.
Volder

Posts: 986
Registered: 04/14/07
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 3, 2007 12:17 AM   in response to: user560002 in response to: user560002
Click to report abuse...   Click to reply to this thread Reply
Here's another solution by using model clause in 10g.
With algorithm, described in
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
we can find all the prime numbers by writing such a query:
SQL> with t as (select level l from dual connect by level <= 100)
2 --
3 SELECT l prim_num FROM
4 (select * from t
5 model
6 dimension by (l dim)
7 measures (l,2 temp)
8 rules iterate (1000000) until (power(temp[1],2)>100)
9 (l[DIM>TEMP1]=decode(mod(lCV(),temp[1]),0,null,lCV()),
10 temp[1]=min(l)[dim>temp1])
11 )
12 WHERE l IS NOT NULL
13 /

PRIM_NUM

1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

26 rows selected

SQL>

and it's quicker than a cartesian join given by Rob.
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 3, 2007 12:36 AM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
Splendid!
Satyaki_De

Posts: 6,686
Registered: 12/20/06
Re: how to retrive prime numbers from a Number column using only SQL Query
Posted: May 3, 2007 1:36 AM   in response to: Volder in response to: Volder
Click to report abuse...   Click to reply to this thread Reply
Excellent... Really, refresh my mind.

Regards.

Satyaki De.
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums