← DiffPush

All divisors of number

Baseline Bit Manipulation · Advanced Maths O(sqrt(n)) · O(sqrt(n)) for the large-half scratch list

Cutting the Cable Coil Into Every Equal Length

A cable crew must list every equal-length cut possible for a coil of length n, in ascending order. Scanning all lengths up to n wastes a shift; instead the crew checks lengths only up to the square root and records each complementary length — n divided by the small one — on a scratch list, then reads the scratch list backwards so the full catalogue ascends.

Input: An integer n.

Output: All divisors of n in ascending order.

Constraints

Examples

Example 1

Input: {"n":20}
Output: [1,2,4,5,10,20]
Small cuts 1, 2, 4 pair with large cuts 20, 10, 5; merged, the list ascends.

Example 2

Input: {"n":36}
Output: [1,2,3,4,6,9,12,18,36]
The square root 6 is a divisor exactly once — never list it twice.

Solve this in your browser →

Also on LeetCode ↗