Find the GCD of forty-eight and eighteen.
Acceptance criteria
- 1.#out should show gcd 6.
DSA Tutorial · Math for DSA
These 10 tasks go with The Maths You Actually Need chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A handful of number-theory facts appear again and again. None of them is deep, and not knowing one costs an entire question.” .
1.Greatest common divisorexercise · easy · 10 min
Find the GCD of forty-eight and eighteen.
Passes when: #out should show gcd 6.
2.Lowest common multipleexercise · easy · 10 min · practice plan
Find the LCM of four and six.
Passes when: #out should show lcm 12.
3.Primes with a sieveexercise · easy · 10 min · practice plan
List the primes below twelve and join them.
Passes when: #out should show 2-3-5-7-11.
4.Safe moduloexercise · easy · 10 min · practice plan
Wrap minus one into the range zero to four.
Passes when: #out should show wrapped 4.
5.Safe integer limitexercise · medium · 10 min · practice plan
Report whether two to the sixty is safely representable.
Passes when: #out should show unsafe.
6.Fix the GCD looping foreverbug fix · easy · 8 min
Swapping without taking the remainder makes no progress, so a guard stops it.
Passes when: #out should show gcd 6.
7.Fix the LCM multiplying firstbug fix · easy · 8 min · practice plan
Multiplying before dividing can exceed the safe integer range.
Passes when: #out should show lcm 12.
8.Fix the sieve starting too lowbug fix · medium · 8 min · practice plan
Marking from 2i re-crosses numbers that already have a smaller factor, and here it wrongly clears a prime.
Passes when: #out should show 2-3-5-7-11.
9.Fix the negative remainderbug fix · easy · 8 min · practice plan
The percent operator keeps the sign, so a negative index stays negative.
Passes when: #out should show wrapped 4.
10.Fix the safety check using MAX_VALUEbug fix · easy · 8 min · practice plan
MAX_VALUE is far past the safe integer range, so the test always passes.
Passes when: #out should show unsafe.
Between them these tasks cover GCD, Primes and Safe modulo.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.