Recursive Function help please

This is a recursive solver to try to solve Euler#60. http://projecteuler.net/problem=60 The solver runs through, but fails to find a solution for the last array member, so backtracks (like I think it's supposed to) but when I get back to the first array member, the loop runs out all the way. Can anybody spot for me why it doesn't stop at the next prime?

I've posted just the solver function below; the other function (Concat_Check) works properly and returns true for a partially filled array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int Solver (int primes[5])
{
    int i=1;
    int x=0;

    while (primes[x]!=0) {++x;} //work on the next one

    if ((x>5) && Concat_Check(primes)) {return 1;} //solved array

    for (i=3; i<=SIZE; i++) //try each value, if successful, return true
    {
      if (Is_Prime(i)) {primes[x]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
      if ((Concat_Check (primes)) && Solver (primes)) {return 1;}
    }
    primes[x-1] = 0;
    return 0;
}
Line 6 can overflow primes.
Line 15 should set primes[x]. It can also underflow primes.
I tried setting 15 to primes[x] but now it stalls and keeps getting the same result for primes[4] == 28219.

Can you please explain why it's primes[x] I want and not [x-1]? If I incremented in line 6, and I don't find a solution, isn't the problem before this (so x-1, rather than x)?

Also I don't understand your "overflow" comment about line 6 - I thought I took care of this with line 8?

Thanks helios.
A recursive call that uses a shared resource is supposed to clean up the space it used. The function uses primes[x], so that's the element it should clean up. This will decrement the value returned by the loop on line 8 by 1. If you clean up primes[x-1] you're decrementing this value by 2.
Personally, I'd rather do this, instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
int Solver (int primes[5], int x = 0)
{
    int i=1;

    if ((x>5) && Concat_Check(primes)) {return 1;} //solved array

    for (i=3; i<=SIZE; i++) //try each value, if successful, return true
    {
      if (Is_Prime(i)) {primes[x]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
      if ((Concat_Check (primes)) && Solver (primes, x+1)) {return 1;}
    }
    return 0;
}


Also I don't understand your "overflow" comment about line 6 - I thought I took care of this with line 8?
This is outside the loop. x could very well equal 220 at this point, which would mean you already overflowed primes by several megabytes.
Also I don't understand your "overflow" comment about line 6 - I thought I took care of this with line 8?


If, on line 8, x is greater than or equal to 5, you invoked undefined behavior on line 6 by accessing memory that is outside the primes array.
I made the changes you suggest, but it still doesn't work; now I just get the largest prime under SIZE for each array value. I know SIZE is big enough, because you can find solutions for this problem online.

I don't understand why it wouldn't immediately pick the next prime for primes[0] when Concat_Check will be true, and then execute the next recursive loop.

Thanks for your explanation above.

Topic archived. No new replies allowed.