Sum of Primes Up to N
Updated
Problem statement
Add up every prime number p with 0 <= p <= n and return the total. A prime has exactly two positive divisors, 1 and the number itself, so 0 and 1 are not prime.
If n is prime, it is included. If there are no primes in the range, return 0.
Function
sumOfPrimes(n: int) → longExamples
Example 1
n = 12return = 28Five primes sit in the range 0 to 12: 2, 3, 5, 7 and 11. Adding them gives 28; 12 itself is even and not prime.
Example 2
n = 2return = 22 is prime and counts, because the range includes n itself. The answer is 2.
Example 3
n = 1return = 0There is no prime from 0 to 1, so the sum is 0.
Constraints
0 <= n <= 10^6
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEW
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEW
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEW
- Integer BreakAmazon · ONSITE INTERVIEW
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEW
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEW
- Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEW
- Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEW
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEW