 # PRMSUM - Editorial

Practice

Author: Ashraf Khan
Tester: Ashraf Khan
Editorialist: Ashraf Khan

# DIFFICULTY:

EASY, EASY-MEDIUM

# PREREQUISITES:

Knowledge of Prime Numbers, Basic Mathematics, Basic Programming

# PROBLEM:

Chef has developed interest in prime numbers and is looking out for a challenge to showcase his skills.

He picks a random number N, then finds N prime numbers beginning from 2. For example, if N=5, then the list of prime numbers will be 2,3,5,7,11.

Given the value of N, you have to find the sum of those N prime numbers.

# QUICK EXPLANATION:

Find the first N prime numbers starting from 2 and then print the sum of those numbers.

# EXPLANATION:

The very first line of input contains the number of testcases, T. Then the following T lines contains the values of N.

• So, first take the input for T.
• Then start a loop which will run T times.
• Take the input for N inside this loop.
• Initialize a variable (say count = 1) to keep the count of prime numbers found, .
• Initialize a variable (say sum = 2) to store the summed up prime numbers.
• Initialize another variable (say i = 3) to represent numbers. This variable will keep incrementing by 2 to get the numbers which will be checked for prime number.
• Initialize a while loop as while(count < N). Inside this loop, we will check for the possible prime numbers. If prime number is found then it will be summed up in sum variable and count will be incremented by 1.

# Solutions:

Setter's Solution
``````#Solution code in python

from math import sqrt

T = int(input())
while T>0:
T -= 1
N = int(input())
count = 1
sum = 2
i = 3
while count<N:
found = 0
for j in range(3, int(sqrt(i))+1, 2):
if i % j == 0:
found = 1
break
if found == 0:
count += 1
sum += i
i += 2
print(sum)
``````
Tester's Solution
``````//Solution code in C++

#include <iostream>
#include<math.h>
using namespace std;

int main() {
int T;
cin >> T;

while(T--) {
int N;
cin >> N;

int count=1, sum=2, i=3;

while(count < N) {
int found = 0;
for(int j = 3; j < sqrt(i)+1; j+=2) {
if(i%j == 0) {
found = 1;
break;
}
}
if(found == 0) {
count++;
sum += i;
}
i+=2;
}
cout << sum << endl;
}
return 0;
}
``````
Editorialist's Solution
``````#Solution code in python

from math import sqrt

T = int(input())
while T>0:
T -= 1
N = int(input())
count = 1
sum = 2
i = 3
while count<N:
found = 0
for j in range(3, int(sqrt(i))+1, 2):
if i % j == 0:
found = 1
break
if found == 0:
count += 1
sum += i
i += 2
print(sum)
``````