+ 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.

10 ответов

+ 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

+ 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

+ 2

Are you talking about the Goldbach Conjecture?
https://artofproblemsolving.com/wiki/index.php?title=Goldbach_Conjecture

+ 1

Gaurav Agrawal will you mind sharing code ?

+ 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.

0

Gaurav Agrawal will you mind sharing code ?

0

Chandu K you code doesn't work

0

12 can be expressed as 2+3+7 but correct ans is 7+5 because its uses two prime number

0