Search for all combinations a ^ 2 + b ^ 2 in C

I am trying to find all combinations of x, where x = a ^ 2 + b ^ 2, and x is between 100 and 999.

So far I have:

#include <stdio.h>
#include <stdlib.h>

#define MAX 31 // given that 31^2 = 961

int main(){
    int i = 0;
    int poss_n[] = {0};

    for (int a=0; a <= MAX; a++){
        for (int b=0; b <= MAX; b++){
            if (a*a + b*b >= 100 && a*a + b*b <= 999){ 
                poss_n[i] = a*a + b*b;
                printf("%i\n", poss_n[i]);
                i++;
            }
        }
    }
}

However, it only gives a partially correct output, and also ends prematurely with a segmentation error of 11:

100 1380405074 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 +784 +841 900 +961 101 122 145 170 197 226 257 290 325 362 401 442 485 530 577 626 677 730 785 +842 901 +962 104 125 148 173 200 229 260 293 328 365 404 445 488 533 580 629 680 733 +788 845 904 965 109 130 153 178 205 234 265 298 333 370 409 450 493 538 585 634 685 +738 +793 850 909 970 116 137 160 185 212 241 272 305 340 377 416 457 500 545 592 641 692 745 800 +857 916 +977 106 125 146 169 194 221 250 281 314 349 386 425 466 509 554 601 650 701 754 809 +866 925 +986 100 117 136 157 180 205 232 261 292 325 360 397 436 477 520 : 11

?

UPDATE

, - ? , 100 , , , a ^ 2 + b ^ 2, b = 0.

2

, a = 10, b = 0, 100.

+5
6

Try:

int poss_n[(MAX + 1) * (MAX + 1)] = {0};

.

+5

, .

:

#include <stdio.h>
#include <stdlib.h>

#define MAX 31 // given that 31^2 = 961

int main(){
    int i = 0;
    int poss_n[(MAX + 1) * (MAX + 1)] = {0}; //<<- Give it a size
    int result; //<<- Using this to reduce calculation of the value multiple times.

    for (int a=0; a <= MAX; a++){
        for (int b=0; b <= MAX; b++){
            result = a*a + b*b;
            if (result  >= 100 && result  <= 999){ 
                poss_n[i] = result ;
                printf("%i\n", result);
                i++;
            }
        }
    }
}
+1
int poss_n[] = {0};  

, , Segfault , > 1

0

, . . , , , , . Segmentation fault.

malloc (3) , , . realloc. , .

un * ces , alloca (3). , some_type array[some_value], some_value , .

++ STL , vector list, . .

: . (a + 1) ^ 2 - a ^ 2 = 2a + 1. , (a < 1) + 1 , 0. b. , 999. . b (int) sqrt (100-a * a) ( temp b), .

0

,

#include <stdio.h>
#include <stdlib.h>

#define MAX 31 // given that 31^2 = 961

int main(){
    int i = 0;
    int poss_n[301];
int a,b;
    for (a=0; a <= MAX; a++){
        for (b=0; b <= MAX; b++){
            if (a*a + b*b >= 100 && a*a + b*b <= 999){ 
                poss_n[i] = a*a + b*b;
                printf("%i\t i = %d , a = %d , b = %d\n", poss_n[i],i,a,b);
                i++;
            }
        }
    }
}
0

, .

Ff , , ( ):

for (int a=0; a <= MAX; a++){
    for (int b=a; b <= MAX; b++){

poss_n : (32+(32-1)+(32-2)+..+1)=1/2*32*(32+1)

Therefore, it should be defined as:

int elements = MAX+1;
int *poss_n = (int*)malloc ((1/2)*(elements)*(elements+1));

It will have more elements due to the rule 100 <value <999, but without this rule it will contain the exact number of elements.

0
source

All Articles