In TSQL determine if a given number is a prime number. If you’re not familiar wi
ID: 3706387 • Letter: I
Question
In TSQL determine if a given number is a prime number. If you’re not familiar with what prime numbers are, a refresher can be found at:
https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-prime-numbers/v/prime-numbers
A list of prime numbers can be found at:
http://www.primos.mat.br/indexen.html
For this assignment, you will declare a single variable and set it to the number you want to check. Your output should look like the following:
“The number <#your number here> is/is not prime.”
Make sure you understand the rules (for example – 1 is not prime, but 2 is). Do not hard code the list of prime numbers. Your algorithm does not need to be super-efficient. I will check numbers up to around 5k.
Explanation / Answer
Create Proc Prime_Or_NotPrime
@Number Int
As
Begin
Declare @Counter Int
Declare @Checker Int
Set @Checker = 0
Set @Counter = 2
While(@Counter<=(@Number/2))
Begin
If((@Number % @Counter)=0)
Set @Checker = @Checker + 1
Set @Counter = @Counter + 1
End
If(@Checker = 0)
Print 'The number '+@Number+' is prime'
ELSE
Print 'The number '+@Number+' is not prime'
End
--to test the number
EXEC Prime_Or_NotPrime (@Prime_Or_NotPrime=99)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.