Add the Integers Divisible by Three, Five, or Seven

Quick Overview

Sum integers from 1 through n divisible by three, five or seven, counting each qualifying value only once.

Add the Integers Divisible by Three, Five, or Seven

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem For a positive integer n, sum all integers from 1 through n inclusive that are divisible by at least one of 3, 5, or 7. Count a qualifying integer once even if several divisibility conditions hold. ```cpp class Solution { public: int sumOfMultiples(int n); }; ``` ### Examples **Example 1:** ```text Input: n = 5 Output: 8 Explanation: The qualifying integers are 3 and 5. ``` **Example 2:** ```text Input: n = 15 Output: 81 Explanation: The qualifying integers are 3,5,6,7,9,10,12,14,15, whose sum is 81. Fifteen is counted once. ``` ### Constraints - `1 <= n <= 1000`. ### Clarifying Questions - Is n included? Yes, when it qualifies. - Must a value be divisible by all three numbers? No; any one is enough. - Is a value divisible by several of them added several times? No; count it once. - What if no integer qualifies? Return zero. ```hint Check the boundary Check the value15, which satisfies more than one divisor condition. ```

Overview: Sum integers from 1 through n divisible by three, five or seven, counting each qualifying value only once.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

For a positive integer n, sum all integers from 1 through n inclusive that are divisible by at least one of 3, 5, or 7. Count a qualifying integer once even if several divisibility conditions hold.

class Solution {
public:
    int sumOfMultiples(int n);
};

Examples

Example 1:

Input: n = 5
Output: 8
Explanation: The qualifying integers are 3 and 5.

Example 2:

Input: n = 15
Output: 81
Explanation: The qualifying integers are 3,5,6,7,9,10,12,14,15, whose sum is 81. Fifteen is counted once.

Constraints

  • 1 <= n <= 1000 .

Clarifying Questions Guidance

  • Is n included? Yes, when it qualifies.
  • Must a value be divisible by all three numbers? No; any one is enough.
  • Is a value divisible by several of them added several times? No; count it once.
  • What if no integer qualifies? Return zero.