Queries to print all divisors of number

Given a positive integer ‘n’ and query ‘q’. Print all the divisors of number ‘n’.

Input: 6
Output: 1 2 3 6
Divisors of 6 are: 1, 2, 3, 6

Input: 10
Output: 1 2 5 10

Naive approach is to iterate through 1 to sqrt(n) for every query ‘q’ and print the divisors accordingly. See this to understand more. Time complexity of this approach is q*sqrt(n) which is not sufficient for large number of queries.

Efficient approach is to use factorization by using sieve base approach.