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

  1. Initialize grid 1..n as candidates
  2. Test each d: if n mod d = 0 mark green, else tan
  3. Advance current ring as each d is tested
  4. 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)