Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Find the Sum of the First N Prime Numbers

Basic Acceptance 93.47% Points 10

You are given a positive integer N. Your task is to return the sum of the first N prime numbers, starting from 2.

A prime number is a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself. Generate the prime numbers in ascending order and calculate their sum until you have found the first N primes.

Examples
Example 1

Input: N = 3

Output: 10

Explanation: The first three prime numbers are 2, 3, and 5. Their sum is 2 + 3 + 5 = 10.

Example 2

Input: N = 5

Output: 28

Explanation: The first five prime numbers are 2, 3, 5, 7, and 11. Their sum is 2 + 3 + 5 + 7 + 11 = 28.

Example 3

Input: N = 1

Output: 2

Explanation: The first prime number is 2, so the sum is 2.

Constraints
  • 1 <= N <= 105
Topics
Introduction Data Structures Basics
Companies
Apple Intuit
Added 24 Nov 2026 93.47% acceptance
Ln 1, Col 1Sign in to save your code
5
28