Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given a positive integer N, calculate the sum of all prime numbers between 1 and N (inclusive).



Input format:

The first line of input contains an integer T denoting the number of test cases.

Each test case contains one line of input containing N.



Output format:

For each test case, in a new line, print the sum of all prime numbers between 1 and N.



Constraints:
1 <= T <= 100
1 <= N <= 100^6


Sample Test Cases
Test Case 1:
Expected Output:
2
5
10
10
17
Test Case 2:
Expected Output:
2
7
17
10
17

What I have tried:

not able to get the above correct output
Posted
Updated 3-Feb-23 15:32pm
Comments
Mike Hankey 3-Feb-23 8:56am    
What have your tried? Please provide what code you wrote and where you are stuck.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
1 <= N <= 100^6

Are you sure about such a requirement?
100^6 = 10^12 is a rather big number, you have to use 64(see k5054 remark) 128-bit integers.

You need a primality test function, see, for instance: Primality test - Wikipedia[^].

Then, consider that computing the sum for the largest input number will, in the proceeding, cover all the test cases.
 
Share this answer
 
v2
Comments
k5054 3-Feb-23 11:20am    
Even 64 bit numbers aren't large enough. If I understand this stackexchange answer correctly :
https://math.stackexchange.com/a/624296
you can approximate the sum of primes using
sum = (x^2)/(2 * log(x))
For a value of 100^6 (10^12) that gives a value of magnitude 10^22 or 2^75
CPallini 3-Feb-23 16:40pm    
I stand corrected, thank you . I suppose the OP actual requirement is 10^6.
Quote:
not able to get the above correct output

Here, we help you fix your code. You will see that understanding why and how your code failed is much more valuable.
But for this, we need to see your code.

It is nice to show us 2 sample outputs, but without the matching inputs, we can't do anything with it.

Update the question with necessary information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900