Ejemplo n.º 1
0
/* retorna TRUE cuando encuentra una solucion valida */
int backtrack(int a[], int n, int k) {
    if (n == k) {
        if (is_valid_solution(a, n)) {
            print_solution(a, n);
            return TRUE;
        }
        else
            return FALSE;
    }
    else {
        int nr_candidates;
        int candidates[N];
        find_candidates(a, n, k, candidates, &nr_candidates);
        int found;

        int i;
        for (i = 0; i < nr_candidates; ++i) {
            a[k] = candidates[i];
            found = backtrack(a, n, k + 1);
            if (found)
                return TRUE;
        }
        return FALSE;
    }
}
Ejemplo n.º 2
0
long long min_x_solution(long long d)
{
	number_t num;
	fraction_t x_over_y;
	long long * seq; // expansion sequence for sqrt(d)
	long long seq_size, seq_cap;
	
	// num = a * (sqrt(b) - c) / d
	num.a = 1;
	num.b = d;
	num.c = 0;
	num.d = 1;
	
	seq_size = 0;
	seq_cap = 4;
	seq = (long long *) malloc(seq_cap * sizeof(seq[0]));
	
	do
	{
		if(seq_size >= seq_cap)
		{
			seq_cap *= 2;
			seq = (long long *) realloc(seq, seq_cap * sizeof(seq[0]));
		}
		
		seq[seq_size] = next_num(&num);
		seq_size++;
		
		x_over_y = expanded_sequence(seq, seq_size);
	} while(!is_valid_solution(d, x_over_y.num, x_over_y.denom));
	
	free(seq);
	
	return x_over_y.num;
}
Ejemplo n.º 3
0
int main(int argc, char ** argv)
{
	INT d, x, j, sqrt_d;
	INT max_x, max_d;
	
	max_d = 0;
	max_x = 0;
	for(d = 2; d <= 1000; d++)
	{
		if(is_square(d))
			continue;
		
		for(j = 1; j > 0; j++)
		{
			if(is_valid_solution(d, d * j - 1))
			{
				x = d * j - 1;
				break;
			}
			if(is_valid_solution(d, d * j + 1))
			{
				x = d * j + 1;
				break;
			}
		}
		
		printf("%Ld ^ 2 - %Ld * %Ld ^ 2 == 1\n", x, d, dx_to_y(d, x));
		
		if(max_x < x)
		{
			max_x = x;
			max_d = d;
		}
	}
	
	printf("Largest minimal x for x^2-D*y^2=1 for 1 <= D <= 1000: %Ld\n", max_x);
	
	return 0;
}