View Single Post
Zurtex
#10
May29-07, 07:53 AM
Sci Advisor
HW Helper
P: 1,123
Here's my quite badly written very naive code in C++

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void mypause() 
{ 
     
  std::cout<<"               Press [Enter] to continue . . .";
  std::cin.get();
} 


int main(int argc, char* argv[])
{
    int prime [] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};
    int bigprime [5];
    int i = 15;
    int prime1;
    int prime2;
    int prime3;
    int prime4;
    int prime5;
    int prime6;
    int sum = 0;
    int prod;
    int prod1 = 0; 
    while(i > -1)
    {
              prime1 = prime[i];
              int j = i - 1;
              while (j > -1)
              {
                    prime2 = prime[j];
                    sum = prime1 + prime2;
                    if (sum < 82)
                    {
                    int k = j - 1;
                    while (k > -1)
                    {
                          prime3 = prime[k];
                          sum = prime1 + prime2 + prime3;
                          if (sum < 89)
                          {
                          int l = k -1;
                          while (l > -1)
                          {
                                prime4 = prime[l];
                                sum = prime1 + prime2 + prime3 + prime4;
                                int m = l - 1;
                                if (sum < 94)
                                {
                                while (m > -1)
                                {
                                      prime5 = prime[m];
                                      sum = prime1 + prime2 + prime3 + prime4 + prime5;
                                      if (sum < 97)
                                      {
                                      int n = m -1;
                                      while (n > -1)
                                      {
                                            prime6 = prime[n];
                                            sum = prime1 + prime2 + prime3 + prime4 + prime5 + prime6;
                                            if (sum == 98)
                                            {
                                                    prod = prime1 * prime2 * prime3 * prime4 * prime5 * prime6;
                                                    cout << " || ";
                                                    cout << prime1 ;
                                                    cout << " ";
                                                    cout << prime2 ;
                                                    cout << " ";
                                                    cout << prime3 ;
                                                    cout << " ";
                                                    cout << prime4 ;
                                                    cout << " ";
                                                    cout << prime5 ;
                                                    cout << " ";
                                                    cout << prime6 ;
                                                    cout << " , ";
                                                    cout << prod ; 
                                                    cout << " || \n";
                                                    if (prod > prod1)
                                                    {
                                                            prod1 = prod;
                                                            bigprime[0] = prime1;
                                                            bigprime[1] = prime2;
                                                            bigprime[2] = prime3;
                                                            bigprime[3] = prime4;
                                                            bigprime[4] = prime5;
                                                            bigprime[5] = prime6;
                                                    }                                                    
                                            }
                                            n--;
                                      }
                                      }
                                      m--;
                                }
                                }
                                l--;
                          }
                          }
                          k--;
                    }
                    }
                    j--;
              }
              i--;
               
    }
    
    cout << "\n";
    cout << bigprime[0];
    cout << " ";
    cout << bigprime[1];
    cout << " ";
    cout << bigprime[2];
    cout << " ";
    cout << bigprime[3];
    cout << " ";
    cout << bigprime[4];
    cout << " ";
    cout << bigprime[5];
    cout << "    ";
    cout << prod1;
    cout << "  \n \n";
    
    mypause();
}
It outputs each combination, followed by a comma, followed by their product.

Then at the end gives the biggest product, with the primes used in the product, here's a screenshot of the output:

http://zurtex.googlepages.com/primes1.jpg