#PostgreSQL, 176 bytes #
PostgreSQL, 176 bytes
WITH c AS(SELECT * FROM(SELECT 6v)t,generate_series(1,sqrt(v)::int)s(r)WHERE v%r=0)
SELECT string_agg(r::text,',' ORDER BY r)
FROM(SELECT r FROM c UNION SELECT v/r FROM c)s
Input: (SELECT ...v)
How it works:
(SELECT ...v)
- inputgenerate_series(1, sqrt(v)::int)
- numbers from 1 to sqrt(n)WHERE v%r=0
-filter divisors- wrap with common table expression to refer twice
SELECT r FROM c UNION SELECT v/r FROM c
generete rest of divisors and combineSELECT string_agg(r::text,',' ORDER BY r)
produce final comma separated result
Input as table:
WITH c AS(SELECT * FROM i,generate_series(1,sqrt(v)::int)s(r)WHERE v%r=0)
SELECT v,string_agg(r::text,',' ORDER BY r)
FROM(SELECT v,r FROM c UNION SELECT v,v/r FROM c)s
GROUP BY v
Output:
╔═════╦════════════════╗
║ v ║ string_agg ║
╠═════╬════════════════╣
║ 1 ║ 1 ║
║ 2 ║ 1,2 ║
║ 6 ║ 1,2,3,6 ║
║ 9 ║ 1,3,9 ║
║ 99 ║ 1,3,9,11,33,99 ║
╚═════╩════════════════╝