1

I have a homework to write a code, which find all sums of different prime numbers equals 100. I write this code for 2-elements sums, but i don't have idea how to iterate it for more elements. It has to be written in c++/clr. I would be happy if you could help me.

#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;

int main(array<System::String ^> ^args)
{
List<int> ^primes = gcnew List<int>();
primes->Add(2);
primes->Add(3);
for (int i = 3; i < 100; i++)
{

    double square = Math::Sqrt(i);
    for (int j = 2; j <= square ; j++)
    {
        if(i%j == 0)break;
        else if (j == Math::Floor(square))primes->Add(i);
    }
}
int primesQuantity = primes->Count;
int s = 0;
for (int i = 0; i < primesQuantity; i++)
{
    for (int k = 0; k < primesQuantity; k++)
    {
        if (i != k)
        {
            s = primes[i] + primes[k];

            if (s == 100)
            {
                Console::WriteLine("{0}+{1}=" + s, primes[i], primes[k]);
            }
        }

    }
}
Console::ReadKey();
}
Ziomal
  • 23
  • 4
  • Have you tried something simpler? Like finding all sets of different positive integers that add up to 8? – Beta Jan 17 '16 at 00:07
  • Unfortunately, i have to do this project for sums of prime numbers equals 100. Because this is project for my university... – Ziomal Jan 17 '16 at 00:12
  • 1
    Yes, I know, but trying something simpler first is a *very good way to solve problems.* The exercise I suggest may lead you to the insight that will allow you to complete your project. – Beta Jan 17 '16 at 00:27
  • Also, does the order of the numbers matter? I mean, should the code report both `36+64` and `64+36`, or is one enough? – Beta Jan 17 '16 at 00:31
  • I would recursively sum the primes from a sorted vector starting with the largest and moving towards the smallest. The recursive function would use the index and sum as parameters. The function would abort if the sum would exceed 100 or the end of the vector was reached. Write is a tail-recursive function so the compiler optimises it. You might want to keep track of the numbers making up the sum in another vector parameter or simply output them as they are discovered. – T33C Jan 17 '16 at 00:31
  • @T33C I don't understand what is a sorted vector and how to create it ? – Ziomal Jan 17 '16 at 00:41
  • @Beta Yes, you right that only one sum like that is enough, but for now it is not the main problem. I don't have idea how to iterate over any loop to do this sums. – Ziomal Jan 17 '16 at 00:43

2 Answers2

0

I forgot the name of the algorithm but idea is following:

1) You need to produce all the possible combinations of the elements. This is achieved by using bit masks. You take a number and then take read bits that are set to 1, for example: 5 = 101, so you take only the 0 and 2 bits.

2) You sum them.

Sample of the code:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
    vector<vector<int> > res;
    // Here is a binary number that contains 25 of 1.
    // I used 25 because this is the number of prime number from 1 to 100
    for (auto i = 0; i < 0b1111111111111111111111111; i++)
    {
        vector<int> group;
        auto val = i;
        auto bits = 0;
        for (auto j = val; val; val >>= 1, ++bits)
            if (val & 0x1 == 1) group.push_back(bits);
        auto sum = std::accumulate(group.begin(), group.end(), 0,
        [&primes](int acc, int x){return acc + primes[x];});
        if (sum == 100) res.push_back(group);
    }

    for (auto el : res) {
        for (auto ele : el)
            cout << ele << " ";
        cout << endl;
    }
    return 0;
}

Result:

0 1 2 3 4 5 6 7 8
0 2 4 5 6 8 9
3 4 5 6 8 9
0 1 4 5 7 8 9
2 4 5 7 8 9
0 1 3 6 7 8 9
2 3 6 7 8 9
....
1 24

Here are the indices of the elements from the prime array. Be aware that they start from 0.

Idea for improvement: You might run the body of the cycle in a new thread since the threads don't impact each other

neshkeev
  • 6,280
  • 3
  • 26
  • 47
0

Did you learn about the subset sum problem? It asks the following question:

Given a set of integers A and an integer s, does any non-empty subset sum to s?

In your case, s==100 and A == {2,3,5,7, ... 97}.

You can find C code online that computes all subsets (here's one you might need to adapt) and just feed it your desired input.

Tomer Levinboim
  • 992
  • 12
  • 18