Problem Solving with Go course

ComplexityCounting Steps

Problem Solving with Go

Display

Theme

Complexity · Test exercise

Counting Steps

Puzzles often come in two parts. Part one is done in a blink. Part two uses the same rules with much bigger numbers, and the same program suddenly never finishes. To see why, count how many times the code runs, not how many seconds it takes.

Big-O in practice

Big-O notation answers one question: if the input size n is multiplied by 10, what does the work get multiplied by?

Code shapeNumber of stepsBig-On × 10 means
one loop up to nnO(n)10× longer
a loop inside a loopabout n²/2O(n²)100× longer
n halved every stepabout log₂ nO(log n)only about 3 more steps
reading a map or a setconstantO(1)no change

Constants like /2 are dropped in Big-O, because what matters is the shape of the growth.

Why it matters in the browser

The Go runtime in Nibble is an interpreter, roughly tens of times slower than real Go. As a rough guide, assume it does a few million simple steps per second. An O(n) loop with n = 1,000,000 still feels quick. An O(n²) loop with the same n means half a trillion steps: it won't finish, in the browser or on your computer.

Counting without running

Look at this nested loop:

for i := 0; i < n; i++ {
	for j := i + 1; j < n; j++ {
		body()
	}
}

When i = 0, the body runs n - 1 times. When i = 1, n - 2 times. And so on down to 0. The total is (n-1) + (n-2) + ... + 1 + 0 = n(n-1)/2. That's the number of distinct pairs among n things, a shape that shows up all the time.

For a loop that halves (i /= 2), the number of steps is how many times n can be halved before it reaches 0: 20 steps for a million, 30 for a billion.

Task

Complete three functions that return how many times body() runs in the loop shown in each comment, for the given n:

  1. countA(n int) int: one loop from 0 to n.
  2. countB(n int) int: the nested loop above. Use the formula; don't run the loop.
  3. countC(n int) int: the loop that halves i. A loop is fine here, since it only runs about log₂ n times.

For an n of 0 or less, the loops don't run at all. Press Run to see the numbers for n up to a billion.

main.go
package main

import "fmt"

// countA is how many times the body runs in:
//
//	for i := 0; i < n; i++ { body() }
func countA(n int) int {
	// TODO: a formula, no loop
	return 0
}

// countB is how many times the body runs in:
//
//	for i := 0; i < n; i++ {
//		for j := i + 1; j < n; j++ { body() }
//	}
func countB(n int) int {
	// TODO: a formula, no loop
	return 0
}

// countC is how many times the body runs in:
//
//	for i := n; i > 0; i /= 2 { body() }
func countC(n int) int {
	// TODO: a loop is fine here: it runs only about log2(n) times
	return 0
}

func main() {
	for _, n := range []int{10, 1_000, 1_000_000, 1_000_000_000} {
		fmt.Printf("n=%d  A=%d  B=%d  C=%d\n", n, countA(n), countB(n), countC(n))
	}
}

Output

Go runtime not loaded yet

Press Run to see the output, or Submit to check your code.

Ctrl/Cmd + Enter: Run · Ctrl/Cmd + Shift + Enter: Submit · Ctrl/Cmd + S: tidy the code (gofmt)