Sum multiples below a limit
Sum all positive integers below a limit that are divisible by 3 or 5, counting shared multiples only once.
Course progress
Problem 70 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
limit = 10; divisors = 3 or 5
Expected output
23
What is happening?
The qualifying positive integers below 10 are 3, 5, 6, and 9. Their sum is 23.
Solution strategy
Build the right mental model
Visit each positive value below the limit and add it when either divisibility test succeeds.
Core concepts
loops · modulo
Complexity
Time O(limit), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for sum-multiples-below-limit
using System;
class Program
{
static int SumMultiples(int limit)
{
int total = 0;
for (int number = 1; number < limit; number++)
if (number % 3 == 0 || number % 5 == 0) total += number;
return total;
}
static void Main()
{
Console.WriteLine(SumMultiples(10));
}
}Knowledge check
Questions students often ask
What is the main idea behind sum multiples below a limit?
Visit each positive value below the limit and add it when either divisibility test succeeds.
What is the time and space complexity of this solution?
Time O(limit), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.