I need to write a program solving all Pythagorean triples where 1<c<500
I have my program written but i dont want it to output the triples already stated in a different order eg "3,4, and 5" and "4,3, and 5". my program is as follows:
#include <stdio.h>
#include <math.h> //needed for power(pow) and square root (sqrt) functions
//This program will find all pythagorean triples where 1<(a,b,c)<500
void main(void)
{
unsigned a;
unsigned b;
unsigned c;
for (a=1; a <= 500; a = a + 1){
for (b=1; b <= 500; b = b + 1){ from 1 to 500
c = sqrt(pow(a,2) + pow(b,2 ));
if (pow(c,2) - pow(a,2) == pow(b,2)){
if (c <= 500) {
printf("%u, %u, and %u is a Pythagorian triple\n", a, b, c);
}}}}}
Is there any way to include "if (b != a previous) {"
also the simplest the solution the better we just learned programming a few days ago.
For example, n=4, and you want to get all combinations for a=[1;4] and b=[1;4] excluding permutations of previous sequences.
If inside the inner for b is initialized to 1, you get:
1,1
1,2
1,3
1,4
2,1
...
4,3
4,4
But if b is initialized to a:
1,1
1,2
1,3
1,4
2,2
2,3
2,4
3,3
3,4
4,4
I suggest you remember this, as it's one of the most useful techniques when iterating combinations of items.
thank you for your time. I actually figured it out a couple of minutes after i posted this. instead of the for loop going til b<= 500 i did b<= a. Thanks anyway! i might be back :D