Solved

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

Posted on 2016-11-07
4
39 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
  • 3
4 Comments
 
LVL 24

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 24

Accepted Solution

by:
Pawan Kumar earned 500 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 24

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now