Introduction

List a set of prime numbers where the sum total of the complete set is 100.

So, I thought why not generate try generating *all* such sets of primes, instead of *any* such set ? And, I sat down to code my own generator ..

About the program

The program conforms to the latest ANSI C++ standards (compiled and tested with MinGW on Windows XP). On my machine (Celeron D 2.6 GHz, 512 MB RAM), the program generates 2473838 different sets of prime numbers, all of which add to 500; in 38.8 seconds (on average).

The main logic of the program is :
(1) Generate primes till some upper limit : call it PMax.
(2) Get the target number from the user : call it UserInput.
(3) Compute the approximate number of primes less than UserInput : call it LastIndex.
(4) Through a loop, "try to" break UserInput into k different prime numbers by the function RPS.
--> Vary k from 1 to sqrt(UserInput), 'coz at max UserInput can be broken into approximately sqrt(UserInput) increasing prime numbers.
( sqrt(UserInput) is a loose upper limit, but the gap is negligible within 0~500, which we are concerned with. )
--> After the RPS func return the lists, sort them if required and remove duplicate lists.

The backbone of the program -- RPS function works as follows :
(1) Accept for arguments : N, K, Index, PIndex.
--> N is the number we are trying to break.
--> K is the number of parts we want.
--> Index is the list position where we store currently.
--> PIndex is the index of the prime number from which we start breaking.
(2) If K is 1, we just need to check if N is prime or not. So, we do a binary search from PIndex to LastIndex and return check if N is prime.
--> If N is prime, then add N to the list and return 0 value.
[ Let me mention here that, the function always returns X-1 where X is the number of elements added to the list. ]
--> If N is not prime, then return -1. ( 'coz 0 values added. )
(3) If K is not equal to one, then ... (the important portion),
--> Loop through all primes starting from index PIndex till we reach N/K ('coz if we break N into K parts, at least one of them will be less than N/K) : call it P, and it's index Pi.
--> Recursively check if N-P can be broken into K-1 parts : RPS(N-1, K, Index, Pi+1).
--> If N-P could be broken, add the lists along with P to the current list and increase Index.

Now, we have some additional thing(s) to take care of :
(*) LastIndex -- The last index of the prime numbers array, till which we conduct the binary search. Calculating this can be omitted, and we can always check till the last index, but that will reduce speed.
To calculate LastIndex approximately, I referred the Wikipedia article on Prime-counting function ( Π(x) ). There, you can find a list that compares Π(x), x/ln x and li(x). From the list, you can see that, for x = 1000, Π(x) - (x/ln x) = 23. So, we can approximately set our LastIndex = 23 + (UserInput/log(UserInput));

The code

So, here is the code ---
Please do give me some credit, if you want to use my code. Just mention my name somewhere

Code: C++

/** Program written by SASWAT PADHI.

This program generates a list of sets of prime numbers which add up to the number entered by the user.

There are no repititions in any sets and no two sets contain all elements identical.The program detects equivalent sets even if the order of elements differ.

Let me explain what their purpose are :
(1) MINI_OUTPUT :: If you un-comment this #define, you can see how many sets are generated of a particular length K. So, verbosity of output increases.
(2) MEGA_OUTPUT :: The most verbose setting. If you un-comment this #define, you can see the whole list of all different sets generated by the program.
(3) ASC_ORDERED :: If you un-comment this #define, the elements inside the sets will be ordered in ascending order.
(4) FILE_OUTPUT :: If you un-comment this #define, the program writes the output to C:\MyFile.txt.
(5) TIME_CALC :: If you un-comment this #define, the program shows the time elapsed to generate the list of sets. (does not count the time you take to input values)

(6) MaxLimit :: Set this to 600 only. Higher values will eat more CPU and memory. Don't try entering 1000 as the target

A plot of number of sets vs UserInput

I tried to find a best fit function for the point-series and found the blue one ( e^(x^(13/30)) ) as a satisfactory best fit till UserInput <= 500.

If you notice closely, there isn't much change in the code.
Actually, what I had done earlier was, I was checking all vectors (which were the prime-sets) for their possible duplicates.
But I just noticed (few days after posting the article here) that the way I generate the primes-sets, all of them must be unique. And they actually were !
So, I just removed the duplicate check :