Solved

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

Posted on 2016-11-07
4
27 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 17

Expert Comment

by:Pawan Kumar Khowal
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 17

Accepted Solution

by:
Pawan Kumar Khowal 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 17

Expert Comment

by:Pawan Kumar Khowal
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

11 Experts available now in Live!

Get 1:1 Help Now