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