oneby7

Foundations Working with digits and divisors

33. All Divisors of a Number

Medium

Given a positive integer n, return all of its divisors in increasing order.

A divisor of n is a positive integer that divides n with no remainder. So 1 and n are always divisors of n, and 4 is a divisor of 12 because 12 = 4 × 3. List each divisor once.

Example 1

Input: n = 36

Output: [1,2,3,4,6,9,12,18,36]

Explanation: Each of these nine numbers divides 36 with no remainder. No other number from 1 to 36 does.

Example 2

Input: n = 1

Output: [1]

Explanation: 1 is the only divisor of 1. Here 1 and n are the same number, so it is listed once.

Example 3

Input: n = 9999999967

Output: [1,9999999967]

Explanation: No number between 1 and n divides this n, so the answer has just two values.

Check your understanding

Work out the answer for this input before you write any code.

n = 16

Constraints

  • 1 ≤ n ≤ 1010

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.