Program for sum of primes from 1 to n
Last Updated :
20 Mar, 2025
Given a positive integer n, compute and return the sum of all prime numbers between 1 and n (inclusive).
Examples:
Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
[Naive Approach] Trial Division Method - O(n^2) time and O(1) space
This method checks each number from 2 to n
for primality by dividing it by all numbers from 2 to i/2
. If any divisor is found, the flag is set to 0, indicating the number is not prime. If no divisors are found, the number is added to the sum. After completing the loop, the sum of all primes up to n
is returned.
C++
#include <iostream>
using namespace std;
int prime_Sum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag) {
sum += i;
}
}
return sum;
}
int main() {
int n = 10;
int result = prime_Sum(n);
cout << result << endl;
return 0;
}
C
#include <stdio.h>
int primeSum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag) {
sum += i;
}
}
return sum;
}
int main() {
int n = 10;
int result = primeSum(n);
printf("%d\n", result);
return 0;
}
Java
class GfG {
public int primeSum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
GfG gfg = new GfG();
int n = 10;
int result = gfg.primeSum(n);
System.out.println(result);
}
}
Python
class GfG:
def primeSum(self, n):
sum = 0
for i in range(2, n + 1):
flag = 1
for j in range(2, i // 2 + 1):
if i % j == 0:
flag = 0
break
if flag:
sum += i
return sum
gfg = GfG()
n = 10
result = gfg.primeSum(n)
print(result)
C#
using System;
class GfG
{
public int PrimeSum(int n)
{
int sum = 0;
for (int i = 2; i <= n; i++)
{
int flag = 1;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
{
sum += i;
}
}
return sum;
}
static void Main(string[] args)
{
GfG gfg = new GfG();
int n = 10;
int result = gfg.PrimeSum(n);
Console.WriteLine(result);
}
}
JavaScript
function primeSum(n) {
let sum = 0;
for (let i = 2; i <= n; i++) {
let flag = 1;
for (let j = 2; j <= i / 2; j++) {
if (i % j === 0) {
flag = 0;
break;
}
}
if (flag) {
sum += i;
}
}
return sum;
}
let n = 10;
let result = primeSum(n);
console.log(result);
[Better Approach] Square Root Method - O(n*sqrt(n)) time and O(1) space
To optimize the above method, we check whether a number is prime by iterating only up to the square root of the number. This approach checks each number from 2 to n
for primality by testing divisibility up to its square root. If a number is prime (i.e., it has no divisors other than 1 and itself), it is added to the sum. Finally, the sum of all prime numbers up to n
is returned.
C++
#include <iostream>
#include <cmath>
using namespace std;
int prime_Sum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag) {
sum += i;
}
}
return sum;
}
int main() {
int n = 10;
int result = prime_Sum(n);
cout << result << endl;
return 0;
}
C
#include <stdio.h>
#include <math.h>
int primeSum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
sum += i;
}
}
return sum;
}
int main() {
int n = 10;
int result = primeSum(n);
printf("%d\n", result);
return 0;
}
Java
import java.util.*;
public class GfG {
public static int primeSum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
int n = 10;
int result = primeSum(n);
System.out.println(result);
}
}
Python
import math
def prime_sum(n):
sum = 0
for i in range(2, n + 1):
flag = 1
for j in range(2, int(math.sqrt(i)) + 1):
if i % j == 0:
flag = 0
break
if flag:
sum += i
return sum
n = 10
result = prime_sum(n)
print(result)
C#
using System;
class GfG {
public static int PrimeSum(int n) {
int sum = 0;
for (int i = 2; i <= n; i++) {
int flag = 1;
for (int j = 2; j <= Math.Sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
sum += i;
}
}
return sum;
}
static void Main() {
int n = 10;
int result = PrimeSum(n);
Console.WriteLine(result);
}
}
JavaScript
function primeSum(n) {
let sum = 0;
for (let i = 2; i <= n; i++) {
let flag = 1;
for (let j = 2; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
flag = 0;
break;
}
}
if (flag) {
sum += i;
}
}
return sum;
}
let n = 10;
let result = primeSum(n);
console.log(result);
[Expected Approach] Using Sieve of Eratosthenes - O(nloglogn) time and O(n) space
The idea is to use Sieve of Eratosthenes to efficiently check which numbers are prime in the range from 1 to n.
This process marks all prime numbers up to n
by iterating through the numbers and marking their multiples as non-prime. After completing the marking, it sums up all the numbers that remain marked as prime.
C++
#include <iostream>
#include <vector>
using namespace std;
// Returns sum of primes in range from 1 to n.
int sumOfPrimes(int n)
{
vector<bool> prime(n + 1, true);
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is true, it is a prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Return sum of primes
int sum = 0;
for (int i = 2; i <= n; i++)
if (prime[i])
sum += i;
return sum;
}
int main()
{
int n = 10;
cout << sumOfPrimes(n);
return 0;
}
Java
// Returns sum of primes in range from 1 to n.
public class GfG {
public static int sumOfPrimes(int n) {
// Array to store prime numbers
boolean[] prime = new boolean[n + 1];
for (int i = 0; i <= n; i++) prime[i] = true;
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is true, it is a prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Return sum of primes
int sum = 0;
for (int i = 2; i <= n; i++)
if (prime[i])
sum += i;
return sum;
}
public static void main(String[] args) {
int n = 10;
System.out.println(sumOfPrimes(n));
}
}
Python
# Returns sum of primes in range from 1 to n.
def sum_of_primes(n):
# List to store prime numbers
prime = [True] * (n + 1)
# 0 and 1 are not prime numbers
prime[0] = prime[1] = False
for p in range(2, int(n**0.5) + 1):
# If prime[p] is true, it is a prime
if prime[p]:
# Mark all multiples of p as non-prime
for i in range(p * p, n + 1, p):
prime[i] = False
# Return sum of primes
return sum(i for i in range(2, n + 1) if prime[i])
n = 10
print(sum_of_primes(n))
C#
// Returns sum of primes in range from 1 to n.
using System;
using System.Collections.Generic;
class GfG {
public static int SumOfPrimes(int n) {
// List to store prime numbers
bool[] prime = new bool[n + 1];
for (int i = 0; i <= n; i++) prime[i] = true;
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is true, it is a prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Return sum of primes
int sum = 0;
for (int i = 2; i <= n; i++)
if (prime[i])
sum += i;
return sum;
}
static void Main() {
int n = 10;
Console.WriteLine(SumOfPrimes(n));
}
}
JavaScript
// Returns sum of primes in range from 1 to n.
function sumOfPrimes(n) {
// Array to store prime numbers
const prime = new Array(n + 1).fill(true);
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
for (let p = 2; p * p <= n; p++) {
// If prime[p] is true, it is a prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (let i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Return sum of primes
let sum = 0;
for (let i = 2; i <= n; i++)
if (prime[i])
sum += i;
return sum;
}
const n = 10;
console.log(sumOfPrimes(n));
Similar Reads
Program to print prime numbers from 1 to N. Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i
15+ min read
Program to print prime numbers from 1 to N. Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i
15+ min read
Program for Goldbachâs Conjecture (Two Primes with given Sum) Goldbach's conjecture is one of the oldest and best-known unsolved problems in the number theory of mathematics. Every even integer greater than 2 can be expressed as the sum of two primes. Examples: Input : n = 44 Output : 3 + 41 (both are primes) Input : n = 56 Output : 3 + 53 (both are primes) Re
12 min read
Sum of prime numbers without odd prime digits Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosth
8 min read
Sum of prime numbers without odd prime digits Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosth
8 min read
Sum of the first N Prime numbers Given an integer 'n', the task is to find the sum of first 'n' prime numbers. First few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, ...... Examples: Input: N = 4Output: 172, 3, 5, 7 are first 4 prime numbers so their sum is equal to 17Input: N = 40Output: 3087 Approach: Create a sieve which w
7 min read