Learn C Programming

Lesson 6 of 9 · Loops and Jump Statements

Module 6 · Loops and Jump Statements

Thinking in Loops: Sums, Digits, Primes

Free with an accountReading

Bob wrote a prime checker in four minutes. He tried it on 97, it said "97 is prime", and he called it done. 97 is a very cooperative number.

Sign in to read

This lesson is free, with an account

An account costs nothing and opens every lesson in this track that is not marked Pro, on every device you read on.

What is inside Thinking in Loops: Sums, Digits, Primes

  1. The accumulator: one box that grows every pass
  2. The digit loop: % takes a digit, / removes it
  3. Reversing and counting digits
  4. Is it prime? Trial division, and where to stop
  5. Euclid's gcd: the remainder does the work
  6. Fibonacci: two variables, and where long long runs out
  7. Zara's rule: test 0, 1 and 2 first