?
Solved

Writing a function to counts the number of primes in the range [1-N].

Posted on 2016-11-07
4
Medium Priority
?
62 Views
Last Modified: 2016-11-09
Hi everyone,
How can I  write a function that counts the number of primes in the range [1-N].
(A natural number is called a prime, a prime number or just prime if it has exactly two distinct divisors. Therefore, 1 is not prime number, since it has only one divisor, namely 1.)
0
Comment
Question by:BehrangDBA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41878101
Try this

--

DECLARE @StartRange AS INT = 2
DECLARE @EndRange AS INT = 50

SELECT COUNT (DISTINCT Number) PrimeNumbers
FROM
    MASTER..SPT_VALUES num
WHERE
    Number >= @StartRange AND Number <= @EndRange AND
        NOT EXISTS 
( 
        SELECT 1 FROM MASTER..SPT_VALUES AS num1 WHERE num1.Number > 1
    AND  num1.Number < num.Number
    AND  num.Number % num1.Number = 0
)

--

Open in new window


if you list of primes use below

--

DECLARE @StartRange AS INT = 2
DECLARE @EndRange AS INT = 50

SELECT DISTINCT Number PrimeNumbers
FROM
    MASTER..SPT_VALUES num
WHERE
    Number >= @StartRange AND Number <= @EndRange AND
        NOT EXISTS 
( 
        SELECT 1 FROM MASTER..SPT_VALUES AS num1 WHERE num1.Number > 1
    AND  num1.Number < num.Number
    AND  num.Number % num1.Number = 0
)

--

Open in new window


Output
------------------------
PrimeNumbers
-------------------------
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41878103
Function with usage

CREATE Function Primes
(
	 @StartRange INT
	,@EndRange INT
)
RETURNS @Table TABLE 
(
   PrimeNumbers INT
)
AS
BEGIN

INSERT INTO @Table(PrimeNumbers)
SELECT COUNT (DISTINCT Number) PrimeNumbers
FROM
    MASTER..SPT_VALUES num
WHERE
    Number >= @StartRange AND Number <= @EndRange AND
        NOT EXISTS 
( 
        SELECT 1 FROM MASTER..SPT_VALUES AS num1 WHERE num1.Number > 1
    AND  num1.Number < num.Number
    AND  num.Number % num1.Number = 0
)	

RETURN

END

Open in new window



usage --

SELECT * FROM dbo.Primes(2,50)

Open in new window


If you dont want to Use Master..Spt_Values (System Table) use below

--
CREATE Function Primesss
(
	 @StartRange INT
	,@EndRange INT
)
RETURNS @Table TABLE 
(
   PrimeNumbers INT
)
AS
BEGIN

WITH SingleDigits(Number) AS
(
    SELECT Number
    FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8),
    (9), (0)) AS X(Number)
)
,Series AS
(
    SELECT (d1.Number+1) + (10*d2.Number) + (100*d3.Number) Number
    from
    SingleDigits as d1,
    SingleDigits as d2,
    SingleDigits as d3   
)
INSERT INTO @Table(PrimeNumbers)
SELECT COUNT (DISTINCT Number) PrimeNumbers
FROM
    Series num
WHERE
    Number >= @StartRange AND Number <= @EndRange AND
        NOT EXISTS 
( 
        SELECT 1 FROM Series AS num1 WHERE num1.Number > 1
    AND  num1.Number < num.Number
    AND  num.Number % num1.Number = 0
)	

RETURN

END
--

Open in new window



usage --

SELECT * FROM dbo.Primes(2,50)

Open in new window


output

---------------

PrimeNumbers
15


Hope it helps !!

---------
1
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41879909
Hi BehrangDBA,
Did you get a chance to check this?

Regards,
Pawan
0
 

Author Closing Comment

by:BehrangDBA
ID: 41881454
Thank you very much for your time Pawan.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question