Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

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

Posted on 2016-11-07
Medium Priority
66 Views
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
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
• 3

LVL 32

Expert Comment

ID: 41878101
Try this

--

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

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
)

--

if you list of primes use below

--

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

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
)

--

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

LVL 32

Accepted Solution

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

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

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

usage --

SELECT * FROM dbo.Primes(2,50)

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

--
CREATE Function Primesss
(
@StartRange INT
,@EndRange INT
)
RETURNS @Table TABLE
(
)
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
)
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
--

usage --

SELECT * FROM dbo.Primes(2,50)

output

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

15

Hope it helps !!

---------
1

LVL 32

Expert Comment

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

Regards,
Pawan
0

Author Closing Comment

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

Featured Post

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll