Command Palette

Search for a command to run...

Back to the lesson: Topic 2.10 — Putting It Together: Control-Flow Problems
Core Java · Example 2 of 4

Primes up to 50, and how much work they took

Classic small problems (FizzBuzz, primes, GCD, Fibonacci, digit tricks, binary conversion) solved with only the tools from this phase. Each one shows a reusable pattern: test the special case first, peel digits, stop early, and keep a running result.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Print the sum of all multiples of 3 or 5 below 1000.

02

Print all perfect numbers below 10,000 (a perfect number equals the sum of its divisors excluding itself, like 6 = 1 + 2 + 3).

03

Print the first 10 numbers that are both even and have a digit sum of 10.

04

Count how many steps Euclid's algorithm takes for GCD(1071, 462), and print the GCD.

Explain it without notes

01

Why is it enough to test divisors up to √n when checking whether n is prime?

02

In FizzBuzz, why must the 'divisible by both' check come first?

03

How does Euclid's algorithm work, and why does it finish quickly?

04

What start value should an accumulator have for a sum, a product and a maximum, and why?

Primes up to 50, and how much work they took
Sign in to run this example in your browser.

Expected output

Primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
15 primes, 95 divisions