Finding Divisors of a Number

hebashakeel

hebaShakeel

Posted on June 18, 2022

Finding Divisors of a Number

Ok so now your interviewer asks you to write a program to find all the divisors of a number. What would be your solution.
Example:
if NUM = 6
Divisors of 6 are 1 2 3 6.

NAIVE SOLUTION:
Image description

Time Complexity : Θ(n)
Space Complexity: O(1)

EFFICIENT SOLUTION:
Image description

Time Complexity : Θ(√n)
This solution does not print the divisors in sorted order.

To print in sorted order:
Image description

Time Complexity : Θ(√n)
Space Complexity : O(1)

Do you have a better solution in mind?
Do share in the comments!

💖 💪 🙅 🚩
hebashakeel
hebaShakeel

Posted on June 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Check if the Kth bit it set!
computerscience Check if the Kth bit it set!

June 19, 2022

Finding Divisors of a Number
computerscience Finding Divisors of a Number

June 18, 2022

Computing power
computerscience Computing power

June 17, 2022

Best Code to check if number is Prime
computerscience Best Code to check if number is Prime

June 16, 2022