Divisors of n
O(n)This simple O(n) algorithm tests every integer d from 1 to n to see whether n mod d equals zero. Each divisor is highlighted green and each non-divisor is coloured tan, with the current candidate ringed in orange as the scan advances. While a square-root optimisation exists, the full scan is intentional here: it demonstrates the definition of divisibility directly and makes the symmetry of divisors (if d divides n then n/d divides n) visible in the grid. The done event reports the full divisor list and τ(n).
Numbers
Edit the input and press Play
How it works
- Initialize grid 1..n as candidates
- Test each d: if n mod d = 0 mark green, else tan
- Advance current ring as each d is tested
- Report divisor list and total count
Pseudocode
1divisorsOf(n): # O(n)2 divisors = []3 for d = 1 to n:4 if n mod d == 0:5 divisors.append(d) # d divides n6 return divisors # tau(n) = len(divisors)