
-----------------------------------
Dratino
Sun Apr 17, 2011 8:43 pm

A competition using goto
-----------------------------------
Here's a little something I came up with just to be evil to structured programmers:

Devise a function a(m, n) to output a single integer, that follows the following restrictions:

1. The function must use a return variable, initialized at 0, and you may only have one statement that increments it.
2. The function may not define any other variables.
3. You may only use the functions and statements ++, --, ==, if, and goto. (If coding in a language other than C or C++, the equivalents may be used. ++ means "increase by 1", and -- means "decrease by 1".)
4. You may not have the same statement twice within the same block of code. (So, no using {++n; ++n;})

Make a function that will return the largest value for a(42, 42). My record is 904, and the source code for the solution is below:

[code]long long a(int m, int n){

	long long output = 0;

stop_a:
	
	++output;
	
stop_b:

	--n;
	if(n == 0){
		--m;
		goto stop_c;
	}
	goto stop_a;
	
stop_c:
		
	if(m < 0) goto stop_d;
	
	++n;
	if(n < m) goto stop_c;
	
	goto stop_a;

stop_d:
	
	return output;

}[/code]

-----------------------------------
Insectoid
Sun Apr 17, 2011 8:48 pm

RE:A competition using goto
-----------------------------------
Um, what exactly does a(m, n) do? You seem to have forgotten to tell us that.

-----------------------------------
Dratino
Sun Apr 17, 2011 8:55 pm

RE:A competition using goto
-----------------------------------
a(m, n) can do whatever you want it to, as long as it follows those rules.

It returns a single integer.

-----------------------------------
Tony
Sun Apr 17, 2011 9:10 pm

RE:A competition using goto
-----------------------------------
Your rules don't mention anything regarding the use of integer literals; and your code does use them ( == 0), so... goto loops until 905?

-----------------------------------
A.J
Sun Apr 17, 2011 9:11 pm

Re: A competition using goto
-----------------------------------
Perhaps I am misunderstanding your rules:



#include
using namespace std;
#define arbitrary 0x7FFFFFFF
long long a(int m, int n)
{
    unsigned long long ret = 0;
    a:;
    ret++;
    if (ret==arbitrary) return ret;
    goto a;

}
main()
{
    cout 