problem
Write a query to print all prime numbers less than or equal to . Print your result on a single line, and use the ampersand () character as your separator (instead of a space).
For example, the output for all prime numbers would be:
2&3&5&7
declare @primes as varchar(max); ; with strip as( select * from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) as X(n) --tens ), seed as( select ROW_NUMBER() over(order by (select null)) as n from strip as A cross join strip as B cross join strip as C ) ,cte_prime as( select cte_numerator.n as n1,cte_denominator.n as n2, (case cte_numerator.n % cte_denominator.n when 0 then 0 else 1 end) as mod --1 if has remainder, 0 if its divisable from seed as cte_numerator inner join seed as cte_denominator on cte_numerator.n > cte_denominator.n ) , res as( select n from seed where 1= ALL(select mod from cte_prime where n1=seed.n and n2<>1 ) --always has remainder!! and seed.n > 1 ) SELECT @Primes = COALESCE(@Primes +'&','') +CAST(n as varchar(3)) from res print @primes
No comments:
Post a Comment