Your condition for printing is not correct.
(prim);cout<<i<<",";
should be
if(prim) cout<<i<<",";
Note that your logic prints primes <= n, not the first n prime
numbers.

It looks like you're tring to use the trivial (aka slow) method of
computing primes: for a prime candidate p, try dividing all integers
from 2 to floor(sqrt(p)).
eps = 1e9
def isprime(p):
int_p = int(p)
# only integers are prime, accounting for floating point rounding
error
if abs(int_p  p) < eps:
return False
else:
# a number is prime only if it cannot be eve

Try this one:
CREATE SAMPLE DATA
use tempdb
create table temp(
id int,
c1 int,
c2 int,
c3 int
)
insert into temp
select 1, 5, 8, 3 union all
select 2, 7, 4, 7 union all
select 3, 5, 8, 7 union all
select 4, 8, 2, 7
SOLUTION
;with cte as(
select c1 as val, 'c1' as col from temp union all
select c2 as val, 'c2' as col from temp union all
select c3 as val, 'c3' as col
