![]() |
Generating prime set which add upto particular number
IntroductionOn 4th August, shabbir posted this question : (at the $1 Daily contest thread) 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 programThe 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. That's all -- a highly recursive function that breaks N into K parts recursively. 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 codeSo, here is the code --- Please do give me some credit, if you want to use my code. Just mention my name somewhere :) Code: C++
Using the codeUsing (modifying) the code is simple. Near the top of the program, you can see some #defines : Code: C++
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 :p A plot of number of sets vs UserInputI 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. So ... we have come to the end of this article then :) Thanx for reading this. I hope this was useful for you. Good bye and, Take Care. |
Re: CODE :: Generating sets of primes which add upto a particular number.
A sample run of the code for UserInput = 75.
Program options : #define MINI_OUTPUT #define MEGA_OUTPUT #define ASC_ORDERED #define TIME_CALC #define MaxLimit 600 Output : Code: OUTPUT
|
Re: Generating prime set which add upto particular number
Nomination for Article of the month - Aug 2009 Started.
|
Re: Generating prime set which add upto particular number
Vote for this article for Article of the month - August 2009
|
Re: Generating prime set which add upto particular number
I had changed (improved the performance of) the above code but had forgotten to post it here.
When I saw this threads' name in the recent threads list, I remembered to post it. :) Here it is, the new code : Code: CPP
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 : Code: C++
Code: C++
And, you get a 20% performance improvement. :D [[ I request shabbir to replace the code in the article with the one above. ]] |
Re: Generating prime set which add upto particular number
Winner of Article of the month - August 2009 Jointly
|
| All times are GMT +5.5. The time now is 21:23. |