+ 2

[HELP] Sum of primes for n ?

N = 12; The primes in range of 12 are 2, 3, 5, 7, 11 now I want an algorithm to find the sum of primes that equal to N (in this case N = 12). Result would be 2 because 7+5 = 12; ( 2+3+7 is equal to 12 also but there are 3 primes). If you didn't get my question then comment I will try to describe more about it.

1st Apr 2018, 5:48 PM
Raziul Islam
Raziul Islam - avatar
10 Antworten
+ 9
yes , I have done it through 3 different ways [most probably] //go to my profile & U'll surely find it in form of a challenge
1st Apr 2018, 6:45 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
//basic approach , which strikes 1st in mind 1)run loop for 1 to n ... a 2)run a loop inside that loop from 1 to n ... b 3) check condn that if (prime(a)&&prime(b)&&a+b==n) //then print a,b //btw can be done through recursion also , will take 10-15 min for new beginner to think
1st Apr 2018, 6:41 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
1st Apr 2018, 7:42 PM
Emma
+ 1
Gaurav Agrawal will you mind sharing code ?
1st Apr 2018, 6:44 PM
Raziul Islam
Raziul Islam - avatar
+ 1
Code to find primes: https://www.geeksforgeeks.org/program-find-sum-prime-numbers-1-n/ Code to find the list of integers in a list, that add up to a predefined number (in your case N) https://stackoverflow.com/questions/32319836/find-all-numbers-in-a-integer-list-that-add-up-to-a-number You would need to put these two together... i.e. find the primes <= N first, then find the list of integers in that list of primes, that adds up to N.
1st Apr 2018, 6:49 PM
Emma
0
Gaurav Agrawal will you mind sharing code ?
1st Apr 2018, 6:44 PM
Raziul Islam
Raziul Islam - avatar
1st Apr 2018, 6:44 PM
kaliki chandu
kaliki chandu - avatar
0
Chandu K you code doesn't work
1st Apr 2018, 6:45 PM
Raziul Islam
Raziul Islam - avatar
0
12 can be expressed as 2+3+7 but correct ans is 7+5 because its uses two prime number
1st Apr 2018, 6:52 PM
Raziul Islam
Raziul Islam - avatar
0
Gaurav Agrawal Xan Chandu K now you got my question ?
1st Apr 2018, 7:03 PM
Raziul Islam
Raziul Islam - avatar