Guest User

Untitled

a guest
Oct 9th, 2010
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime(int n){
  8.     if(n == 2 || n == 3){
  9.         return true;
  10.     }
  11.     if(n % 2 == 0){
  12.         return false;
  13.     }
  14.     int limit = (int)sqrt(n);
  15.     for(int div = 3; div <= limit ; div += 2 ){
  16.         if(n % div == 0){
  17.             return false;
  18.         }
  19.     }
  20.     return true;
  21. }
  22.  
  23. int subsetSize = 3;
  24. //int vals[]  = {3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99};
  25. //int numVals = sizeof(vals)/sizeof(vals[0]);
  26. int* vals ;
  27. int numVals = 172;
  28. int* subset;
  29. int numSpecialSubsets = 0 ;
  30.  
  31. void p(string s, int a[], int l){
  32.     cout << s << endl ;
  33.     //int l = sizeof(a) / sizeof(a[0]);
  34.     for(int i=0;i<l;i++){
  35.         cout << a[i] << ",";
  36.     }
  37.     cout << endl;
  38. }
  39.  
  40. void recur(int numRemaining, int subsetIndex, int valIndex) {
  41.     subset[subsetIndex] = vals[valIndex];
  42.  
  43.     int start = valIndex+1;
  44.     int end = numVals - numRemaining + 1;
  45.  
  46.     if(numRemaining == 2){
  47.         for(int j=start; j<=end;j++){
  48.             subset[subsetIndex+1] = vals[j];
  49.             //p("", subset, subsetSize);
  50.             int largest = subset[subsetSize-1];
  51.             int sumOthers = 0;
  52.             for(int k=0;k<subsetSize-1;k++){
  53.                 sumOthers += subset[k];
  54.             }
  55.             if(sumOthers == largest){
  56.                 numSpecialSubsets ++;
  57.             }
  58.         }
  59.     }
  60.     else{
  61.         for(int j=start; j<=end;j++){
  62.             recur(numRemaining-1, subsetIndex+1, j);
  63.         }
  64.    
  65.     }
  66. }
  67.  
  68. void level3(){
  69.     for(subsetSize = 3; subsetSize <= numVals; subsetSize++){
  70.         cout << subsetSize << " " << numSpecialSubsets << endl;
  71.         int e = numVals-subsetSize+1;
  72.         for(int i=0; i <= e; i++){
  73.             subset = new int[subsetSize];
  74.             recur(subsetSize, 0, i);
  75.         }
  76.        
  77.     }
  78.     cout << "level 2 : special subsets count = " << numSpecialSubsets << endl;
  79. }
  80.  
  81.  
  82. int main(){
  83.     int np = 172;
  84.     vals = new int[np];
  85.     vals[0] = 2;
  86.     int cand = 3;
  87.     int i = 1;
  88.     while(i < np){
  89.         if(isPrime(cand)){
  90.             vals[i++] = cand;
  91.         }
  92.         cand += 2;
  93.     }
  94.     numVals = np;
  95.     p("",vals, np);
  96.     level3();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment