Sum 1 to n excluding multiples of 5 or 7

Quick Overview

Return the sum of the integers from 1 to n, skipping every number divisible by 5 or by 7, for n as large as one billion. It tests math reasoning about arithmetic series, avoiding double counting, and keeping large sums exact without a linear loop.

Sum 1 to n excluding multiples of 5 or 7

Company: Bank of America

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a positive integer `n`, return the sum of all integers from `1` to `n` inclusive, excluding any integer that is divisible by `5` or by `7`. ### Function Signature ```python def sum_excluding_multiples(n: int) -> int: ... ``` ### Constraints - `1 <= n <= 10**9` ### Examples ```text Input: n = 10 Output: 33 ``` The excluded numbers are 5, 7 and 10, and `1 + 2 + 3 + 4 + 6 + 8 + 9 = 33`. As a check, `1 + ... + 10 = 55` and `55 - 22 = 33`. ```text Input: n = 20 Output: 139 ``` The excluded numbers are 5, 7, 10, 14, 15 and 20, which add up to 71, and `1 + ... + 20 = 210`, so the result is `210 - 71 = 139`. ```text Input: n = 35 Output: 420 ``` 35 is divisible by both 5 and 7 and is excluded once. The 24 kept numbers pair up as `k` and `35 - k`, each pair adds up to 35, and `12 * 35 = 420`.

Overview: Return the sum of the integers from 1 to n, skipping every number divisible by 5 or by 7, for n as large as one billion. It tests math reasoning about arithmetic series, avoiding double counting, and keeping large sums exact without a linear loop.

Bank of America logo
Bank of America
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a positive integer n, return the sum of all integers from 1 to n inclusive, excluding any integer that is divisible by 5 or by 7.

Function Signature

def sum_excluding_multiples(n: int) -> int:
    ...

Constraints

  • 1 <= n <= 10**9

Examples

Input: n = 10
Output: 33

The excluded numbers are 5, 7 and 10, and 1 + 2 + 3 + 4 + 6 + 8 + 9 = 33. As a check, 1 + ... + 10 = 55 and 55 - 22 = 33.

Input: n = 20
Output: 139

The excluded numbers are 5, 7, 10, 14, 15 and 20, which add up to 71, and 1 + ... + 20 = 210, so the result is 210 - 71 = 139.

Input: n = 35
Output: 420

35 is divisible by both 5 and 7 and is excluded once. The 24 kept numbers pair up as k and 35 - k, each pair adds up to 35, and 12 * 35 = 420.