AntikM
AntikM

Reputation: 651

C - Finding out the a^2+b^2 of a number

I have a problem in my hand that I can't figure out because I'm very new to C programming. The problem is finding out all the integers between 100 and 999 that can be expressed in the form of (a^2 + b^2).

Here's what I've tried:

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

int main(void) {
  int n,i,j,soln;

  for (n=100;n<1000;++n) {
    soln=0;

    for (i=0;i<100;++i) {
      for (j=i;j<100;++j) {
        if ((i^2 + j^2)==n) {
          soln=1;
        } else {
          soln=0;
        }
      }
    }

    if (soln==1) printf("%d is a solution.\n",n);
  }

  return EXIT_SUCCESS;
}

Thanks guys!

Upvotes: 1

Views: 90

Answers (4)

kamituel
kamituel

Reputation: 35950

Two issues with your code:

1. Use * to square a number, not a ^

To square a number in C, do not use the ^ operator (this is a bitwise XOR operator). Use * operator instead:

if ((i*i + j*j)==n) {
   soln=1;
} else {
   soln=0;
}

2. Move the if condition inside an inner loop

Another issue with your code is that you overwrite the soln value. You should move the condition into the inner loop:

for (n=100;n<1000;++n) {
  soln=0;

  for (i=0;i<100;++i) {
    for (j=i;j<100;++j) {
      if ((i*i + j*j)==n) {
        soln=1;
      } else {
        soln=0;
      }
      // Condition here. When it was in the outer loop level, 
      // the soln=1 would be overwritten to the 0 in the next iteration
      // and printf() wouldn't be called.
      if (soln==1) {
        printf("%d is a solution.\n",n);
        // To avoid printing multiple times for the same n,
        // break from the loop (loop for 'j').
        // If you would want to print for every i,j pair that meets
        // the criteria, remove this 'if' block, get rid of 'soln'
        // and print in the if above (where you square the numbers).
        break;
      }
    }
    // We need to break the two loops, for i and j. This one is for 
    // the outer loop ('i').
    if (soln == 1) break;
  }
}

As you can see, you can implement this in a two slightly different variations:

  • You only care about the n - if you don't need to know what are the components of the number which is a solution, you can speed up the algorithm a bit by not printing every possible solution, just the n which satisfies it. In that case, once you find a i and j that, when squared, give you n, just break both loops. You can do that, because for that n we know that there exists an i/j pair that satisfies the condition.

  • You care about the n and the exact i/j that solved the equation - in this case remove the soln variable and just print the solution instead of setting soln to 1.

Upvotes: 2

Jeegar Patel
Jeegar Patel

Reputation: 27210

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

int main(void) {
  int n,i,j,soln;

  for (n=100;n<1000;++n) {
    soln=0;

    for (i=0;i<100;++i) {
    for (j=i;j<100;++j) {
        if ((i*i + j*j)==n) {
                printf("%d is a solution for i: %d and j: %d \n",n,i,j);
          soln=1;
        } else {
          soln=0;
        }
      }
    }
//    if (soln==1) printf("%d is a solution.\n",n);
  }

  return EXIT_SUCCESS;
}

Your printf() is at wrong place. I have modified your code. output is

100 is a solution for i: 0 and j: 10 
100 is a solution for i: 6 and j: 8 
101 is a solution for i: 1 and j: 10 
104 is a solution for i: 2 and j: 10 
106 is a solution for i: 5 and j: 9 
109 is a solution for i: 3 and j: 10 
113 is a solution for i: 7 and j: 8 
116 is a solution for i: 4 and j: 10 
117 is a solution for i: 6 and j: 9 
121 is a solution for i: 0 and j: 11 
122 is a solution for i: 1 and j: 11 
125 is a solution for i: 2 and j: 11 
125 is a solution for i: 5 and j: 10 
128 is a solution for i: 8 and j: 8 
130 is a solution for i: 3 and j: 11 
130 is a solution for i: 7 and j: 9 
136 is a solution for i: 6 and j: 10 
137 is a solution for i: 4 and j: 11 
144 is a solution for i: 0 and j: 12 
145 is a solution for i: 1 and j: 12 
145 is a solution for i: 8 and j: 9 
146 is a solution for i: 5 and j: 11 
148 is a solution for i: 2 and j: 12 
149 is a solution for i: 7 and j: 10 
153 is a solution for i: 3 and j: 12 
157 is a solution for i: 6 and j: 11 
160 is a solution for i: 4 and j: 12 
162 is a solution for i: 9 and j: 9 
164 is a solution for i: 8 and j: 10 
169 is a solution for i: 0 and j: 13 
169 is a solution for i: 5 and j: 12 
170 is a solution for i: 1 and j: 13 
170 is a solution for i: 7 and j: 11 
173 is a solution for i: 2 and j: 13 
178 is a solution for i: 3 and j: 13 
180 is a solution for i: 6 and j: 12 
181 is a solution for i: 9 and j: 10 
185 is a solution for i: 4 and j: 13 
185 is a solution for i: 8 and j: 11 
193 is a solution for i: 7 and j: 12 
194 is a solution for i: 5 and j: 13 
196 is a solution for i: 0 and j: 14 
197 is a solution for i: 1 and j: 14 
200 is a solution for i: 2 and j: 14 
200 is a solution for i: 10 and j: 10 
202 is a solution for i: 9 and j: 11 
205 is a solution for i: 3 and j: 14 
205 is a solution for i: 6 and j: 13 
208 is a solution for i: 8 and j: 12 
212 is a solution for i: 4 and j: 14 
218 is a solution for i: 7 and j: 13 
221 is a solution for i: 5 and j: 14 
221 is a solution for i: 10 and j: 11 
225 is a solution for i: 0 and j: 15 
225 is a solution for i: 9 and j: 12 
226 is a solution for i: 1 and j: 15 
229 is a solution for i: 2 and j: 15 
232 is a solution for i: 6 and j: 14 
233 is a solution for i: 8 and j: 13 
234 is a solution for i: 3 and j: 15 
241 is a solution for i: 4 and j: 15 
242 is a solution for i: 11 and j: 11 
244 is a solution for i: 10 and j: 12 
245 is a solution for i: 7 and j: 14 
250 is a solution for i: 5 and j: 15 
250 is a solution for i: 9 and j: 13 
256 is a solution for i: 0 and j: 16 
257 is a solution for i: 1 and j: 16 
260 is a solution for i: 2 and j: 16 
260 is a solution for i: 8 and j: 14 
261 is a solution for i: 6 and j: 15 
265 is a solution for i: 3 and j: 16 
265 is a solution for i: 11 and j: 12 
269 is a solution for i: 10 and j: 13 
272 is a solution for i: 4 and j: 16 
274 is a solution for i: 7 and j: 15 
277 is a solution for i: 9 and j: 14 
281 is a solution for i: 5 and j: 16 
288 is a solution for i: 12 and j: 12 
289 is a solution for i: 0 and j: 17 
289 is a solution for i: 8 and j: 15 
290 is a solution for i: 1 and j: 17 
290 is a solution for i: 11 and j: 13 
292 is a solution for i: 6 and j: 16 
293 is a solution for i: 2 and j: 17 
296 is a solution for i: 10 and j: 14 
298 is a solution for i: 3 and j: 17 
305 is a solution for i: 4 and j: 17 
305 is a solution for i: 7 and j: 16 
306 is a solution for i: 9 and j: 15 
313 is a solution for i: 12 and j: 13 
314 is a solution for i: 5 and j: 17 
317 is a solution for i: 11 and j: 14 
320 is a solution for i: 8 and j: 16 
324 is a solution for i: 0 and j: 18 
325 is a solution for i: 1 and j: 18 
325 is a solution for i: 6 and j: 17 
325 is a solution for i: 10 and j: 15 
328 is a solution for i: 2 and j: 18 
333 is a solution for i: 3 and j: 18 
337 is a solution for i: 9 and j: 16 
338 is a solution for i: 7 and j: 17 
338 is a solution for i: 13 and j: 13 
340 is a solution for i: 4 and j: 18 
340 is a solution for i: 12 and j: 14 
346 is a solution for i: 11 and j: 15 
349 is a solution for i: 5 and j: 18 
353 is a solution for i: 8 and j: 17 
356 is a solution for i: 10 and j: 16 
360 is a solution for i: 6 and j: 18 
361 is a solution for i: 0 and j: 19 
362 is a solution for i: 1 and j: 19 
365 is a solution for i: 2 and j: 19 
365 is a solution for i: 13 and j: 14 
369 is a solution for i: 12 and j: 15 
370 is a solution for i: 3 and j: 19 
370 is a solution for i: 9 and j: 17 
373 is a solution for i: 7 and j: 18 
377 is a solution for i: 4 and j: 19 
377 is a solution for i: 11 and j: 16 
386 is a solution for i: 5 and j: 19 
388 is a solution for i: 8 and j: 18 
389 is a solution for i: 10 and j: 17 
392 is a solution for i: 14 and j: 14 
394 is a solution for i: 13 and j: 15 
397 is a solution for i: 6 and j: 19 
400 is a solution for i: 0 and j: 20 
400 is a solution for i: 12 and j: 16 
401 is a solution for i: 1 and j: 20 
404 is a solution for i: 2 and j: 20 
405 is a solution for i: 9 and j: 18 
409 is a solution for i: 3 and j: 20 
410 is a solution for i: 7 and j: 19 
410 is a solution for i: 11 and j: 17 
416 is a solution for i: 4 and j: 20 
421 is a solution for i: 14 and j: 15 
424 is a solution for i: 10 and j: 18 
425 is a solution for i: 5 and j: 20 
425 is a solution for i: 8 and j: 19 
425 is a solution for i: 13 and j: 16 
433 is a solution for i: 12 and j: 17 
436 is a solution for i: 6 and j: 20 
441 is a solution for i: 0 and j: 21 
442 is a solution for i: 1 and j: 21 
442 is a solution for i: 9 and j: 19 
445 is a solution for i: 2 and j: 21 
445 is a solution for i: 11 and j: 18 
449 is a solution for i: 7 and j: 20 
450 is a solution for i: 3 and j: 21 
450 is a solution for i: 15 and j: 15 
452 is a solution for i: 14 and j: 16 
457 is a solution for i: 4 and j: 21 
458 is a solution for i: 13 and j: 17 
461 is a solution for i: 10 and j: 19 
464 is a solution for i: 8 and j: 20 
466 is a solution for i: 5 and j: 21 
468 is a solution for i: 12 and j: 18 
477 is a solution for i: 6 and j: 21 
481 is a solution for i: 9 and j: 20 
481 is a solution for i: 15 and j: 16 
482 is a solution for i: 11 and j: 19 
484 is a solution for i: 0 and j: 22 
485 is a solution for i: 1 and j: 22 
485 is a solution for i: 14 and j: 17 
488 is a solution for i: 2 and j: 22 
490 is a solution for i: 7 and j: 21 
493 is a solution for i: 3 and j: 22 
493 is a solution for i: 13 and j: 18 
500 is a solution for i: 4 and j: 22 
500 is a solution for i: 10 and j: 20 
505 is a solution for i: 8 and j: 21 
505 is a solution for i: 12 and j: 19 
509 is a solution for i: 5 and j: 22 
512 is a solution for i: 16 and j: 16 
514 is a solution for i: 15 and j: 17 
520 is a solution for i: 6 and j: 22 
520 is a solution for i: 14 and j: 18 
521 is a solution for i: 11 and j: 20 
522 is a solution for i: 9 and j: 21 
529 is a solution for i: 0 and j: 23 
530 is a solution for i: 1 and j: 23 
530 is a solution for i: 13 and j: 19 
533 is a solution for i: 2 and j: 23 
533 is a solution for i: 7 and j: 22 
538 is a solution for i: 3 and j: 23 
541 is a solution for i: 10 and j: 21 
544 is a solution for i: 12 and j: 20 
545 is a solution for i: 4 and j: 23 
545 is a solution for i: 16 and j: 17 
548 is a solution for i: 8 and j: 22 
549 is a solution for i: 15 and j: 18 
554 is a solution for i: 5 and j: 23 
557 is a solution for i: 14 and j: 19 
562 is a solution for i: 11 and j: 21 
565 is a solution for i: 6 and j: 23 
565 is a solution for i: 9 and j: 22 
569 is a solution for i: 13 and j: 20 
576 is a solution for i: 0 and j: 24 
577 is a solution for i: 1 and j: 24 
578 is a solution for i: 7 and j: 23 
578 is a solution for i: 17 and j: 17 
580 is a solution for i: 2 and j: 24 
580 is a solution for i: 16 and j: 18 
584 is a solution for i: 10 and j: 22 
585 is a solution for i: 3 and j: 24 
585 is a solution for i: 12 and j: 21 
586 is a solution for i: 15 and j: 19 
592 is a solution for i: 4 and j: 24 
593 is a solution for i: 8 and j: 23 
596 is a solution for i: 14 and j: 20 
601 is a solution for i: 5 and j: 24 
605 is a solution for i: 11 and j: 22 
610 is a solution for i: 9 and j: 23 
610 is a solution for i: 13 and j: 21 
612 is a solution for i: 6 and j: 24 
613 is a solution for i: 17 and j: 18 
617 is a solution for i: 16 and j: 19 
625 is a solution for i: 0 and j: 25 
625 is a solution for i: 7 and j: 24 
625 is a solution for i: 15 and j: 20 
626 is a solution for i: 1 and j: 25 
628 is a solution for i: 12 and j: 22 
629 is a solution for i: 2 and j: 25 
629 is a solution for i: 10 and j: 23 
634 is a solution for i: 3 and j: 25 
637 is a solution for i: 14 and j: 21 
640 is a solution for i: 8 and j: 24 
641 is a solution for i: 4 and j: 25 
648 is a solution for i: 18 and j: 18 
650 is a solution for i: 5 and j: 25 
650 is a solution for i: 11 and j: 23 
650 is a solution for i: 17 and j: 19 
653 is a solution for i: 13 and j: 22 
656 is a solution for i: 16 and j: 20 
657 is a solution for i: 9 and j: 24 
661 is a solution for i: 6 and j: 25 
666 is a solution for i: 15 and j: 21 
673 is a solution for i: 12 and j: 23 
674 is a solution for i: 7 and j: 25 
676 is a solution for i: 0 and j: 26 
676 is a solution for i: 10 and j: 24 
677 is a solution for i: 1 and j: 26 
680 is a solution for i: 2 and j: 26 
680 is a solution for i: 14 and j: 22 
685 is a solution for i: 3 and j: 26 
685 is a solution for i: 18 and j: 19 
689 is a solution for i: 8 and j: 25 
689 is a solution for i: 17 and j: 20 
692 is a solution for i: 4 and j: 26 
697 is a solution for i: 11 and j: 24 
697 is a solution for i: 16 and j: 21 
698 is a solution for i: 13 and j: 23 
701 is a solution for i: 5 and j: 26 
706 is a solution for i: 9 and j: 25 
709 is a solution for i: 15 and j: 22 
712 is a solution for i: 6 and j: 26 
720 is a solution for i: 12 and j: 24 
722 is a solution for i: 19 and j: 19 
724 is a solution for i: 18 and j: 20 
725 is a solution for i: 7 and j: 26 
725 is a solution for i: 10 and j: 25 
725 is a solution for i: 14 and j: 23 
729 is a solution for i: 0 and j: 27 
730 is a solution for i: 1 and j: 27 
730 is a solution for i: 17 and j: 21 
733 is a solution for i: 2 and j: 27 
738 is a solution for i: 3 and j: 27 
740 is a solution for i: 8 and j: 26 
740 is a solution for i: 16 and j: 22 
745 is a solution for i: 4 and j: 27 
745 is a solution for i: 13 and j: 24 
746 is a solution for i: 11 and j: 25 
754 is a solution for i: 5 and j: 27 
754 is a solution for i: 15 and j: 23 
757 is a solution for i: 9 and j: 26 
761 is a solution for i: 19 and j: 20 
765 is a solution for i: 6 and j: 27 
765 is a solution for i: 18 and j: 21 
769 is a solution for i: 12 and j: 25 
772 is a solution for i: 14 and j: 24 
773 is a solution for i: 17 and j: 22 
776 is a solution for i: 10 and j: 26 
778 is a solution for i: 7 and j: 27 
784 is a solution for i: 0 and j: 28 
785 is a solution for i: 1 and j: 28 
785 is a solution for i: 16 and j: 23 
788 is a solution for i: 2 and j: 28 
793 is a solution for i: 3 and j: 28 
793 is a solution for i: 8 and j: 27 
794 is a solution for i: 13 and j: 25 
797 is a solution for i: 11 and j: 26 
800 is a solution for i: 4 and j: 28 
800 is a solution for i: 20 and j: 20 
801 is a solution for i: 15 and j: 24 
802 is a solution for i: 19 and j: 21 
808 is a solution for i: 18 and j: 22 
809 is a solution for i: 5 and j: 28 
810 is a solution for i: 9 and j: 27 
818 is a solution for i: 17 and j: 23 
820 is a solution for i: 6 and j: 28 
820 is a solution for i: 12 and j: 26 
821 is a solution for i: 14 and j: 25 
829 is a solution for i: 10 and j: 27 
832 is a solution for i: 16 and j: 24 
833 is a solution for i: 7 and j: 28 
841 is a solution for i: 0 and j: 29 
841 is a solution for i: 20 and j: 21 
842 is a solution for i: 1 and j: 29 
845 is a solution for i: 2 and j: 29 
845 is a solution for i: 13 and j: 26 
845 is a solution for i: 19 and j: 22 
848 is a solution for i: 8 and j: 28 
850 is a solution for i: 3 and j: 29 
850 is a solution for i: 11 and j: 27 
850 is a solution for i: 15 and j: 25 
853 is a solution for i: 18 and j: 23 
857 is a solution for i: 4 and j: 29 
865 is a solution for i: 9 and j: 28 
865 is a solution for i: 17 and j: 24 
866 is a solution for i: 5 and j: 29 
872 is a solution for i: 14 and j: 26 
873 is a solution for i: 12 and j: 27 
877 is a solution for i: 6 and j: 29 
881 is a solution for i: 16 and j: 25 
882 is a solution for i: 21 and j: 21 
884 is a solution for i: 10 and j: 28 
884 is a solution for i: 20 and j: 22 
890 is a solution for i: 7 and j: 29 
890 is a solution for i: 19 and j: 23 
898 is a solution for i: 13 and j: 27 
900 is a solution for i: 0 and j: 30 
900 is a solution for i: 18 and j: 24 
901 is a solution for i: 1 and j: 30 
901 is a solution for i: 15 and j: 26 
904 is a solution for i: 2 and j: 30 
905 is a solution for i: 8 and j: 29 
905 is a solution for i: 11 and j: 28 
909 is a solution for i: 3 and j: 30 
914 is a solution for i: 17 and j: 25 
916 is a solution for i: 4 and j: 30 
922 is a solution for i: 9 and j: 29 
925 is a solution for i: 5 and j: 30 
925 is a solution for i: 14 and j: 27 
925 is a solution for i: 21 and j: 22 
928 is a solution for i: 12 and j: 28 
929 is a solution for i: 20 and j: 23 
932 is a solution for i: 16 and j: 26 
936 is a solution for i: 6 and j: 30 
937 is a solution for i: 19 and j: 24 
941 is a solution for i: 10 and j: 29 
949 is a solution for i: 7 and j: 30 
949 is a solution for i: 18 and j: 25 
953 is a solution for i: 13 and j: 28 
954 is a solution for i: 15 and j: 27 
961 is a solution for i: 0 and j: 31 
962 is a solution for i: 1 and j: 31 
962 is a solution for i: 11 and j: 29 
964 is a solution for i: 8 and j: 30 
965 is a solution for i: 2 and j: 31 
965 is a solution for i: 17 and j: 26 
968 is a solution for i: 22 and j: 22 
970 is a solution for i: 3 and j: 31 
970 is a solution for i: 21 and j: 23 
976 is a solution for i: 20 and j: 24 
977 is a solution for i: 4 and j: 31 
980 is a solution for i: 14 and j: 28 
981 is a solution for i: 9 and j: 30 
985 is a solution for i: 12 and j: 29 
985 is a solution for i: 16 and j: 27 
986 is a solution for i: 5 and j: 31 
986 is a solution for i: 19 and j: 25 
997 is a solution for i: 6 and j: 31 

Upvotes: 1

smerlung
smerlung

Reputation: 1519

I think you need to exit the loops if you found a solution:

for (i=0;i<100;++i) {
  for (j=i;j<100;++j) {
    if ((i*i + j*j)==n) {
      soln=1;
      break;
    } else {
      soln=0;
    }
  }

  if (soln=1) {
    break;
  }
}

soln=1 is your flag that you found a solution. But in the next iteration you will overwrite that value and set soln=0 again.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Operator ^ means XOR (eXclusive OR) in C, C++, C#, not raising into power. Use multiplication * instead:

 if ((i * i + j * j) == n) {
   ... 

Upvotes: 1

Related Questions