I am trying to create a program that prints the mapping data for found pythagorean triples in C. So far, I have coded the program to be able to find the triples.
#include <stdio.h>
#include <math.h>
int main (int argc, char * argv[]) {
int a, b, c;
int a2, b2, c2;
int limit = 60;
for (a = 1; a <= limit; a++) {
a2 = a * a;
for (b = 1; b <= limit; b++) {
b2 = b * b;
for (c = 0; c <= ((int)sqrt(a2+b2)); c++) {
c2 = c * c;
if (a < b && (c2 == (a2 + b2))) {
printf("triple: %d %d %d\n", a, b, c);
}
}
}
}
}
The intended output is expected in the format:
123456789012345678901234567890123456789012345678901234567890
1\
2 \
3 \
4 *\
5 \
6 \
7 \
8 * \
9 \
0 \
1 \
2 * * \
I am trying to write a loop that does this, but cannot think of how to print in this way. Any suggestions?
UPDATE: I managed to print the x and the y axis (x = a and y = b). The values are correct, now the mapping part is left.
for (int x = 0; x < a; x++) { // x-axis = a
printf("%d ", x);
}
printf("\n");
for (int y = 0; y < b; y++) { // y-axis = b
printf("%d\n", y);
}
UPDATE: Modified the code, the output is printing, but having problems printing spaces. tried manually adding spaces to the " \ " and " * " but that just distorts the whole image.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool is_perfect_square(int num);
int main (int argc, char * argv[]) {
int a, b, c;
int a2, b2, c2;
int limit = 60;
bool flag = false;
for (a = 1; a <= limit; a++) {
a2 = a * a;
for (b = 1; b <= limit; b++) {
b2 = b * b;
for (c = 0; c <= ((int)sqrt(a2+b2)); c++) {
c2 = c * c;
if (a < b && (c2 == (a2 + b2))) {
// printf("triple: %d %d %d\n", a, b, c);
}
}
}
}
for (int x = 0; x < a; x++) {
for (int y = 0; y < b; y++) {
if (x == 0) {
printf("%d ", ((y+1)% 10));
} else if (y == 0) {
printf("%d ", (x % 10));
} else if (x == y) {
printf("\\");
} else if (is_perfect_square((x*x) + (y*y))) {
printf("*");
}
}
printf("\n");
}
}
bool is_perfect_square (int num) {
int root = (int)(sqrt(num));
if (num == root * root) {
return true;
} else {
return false;
}
}
Still working on possible solution.
FINAL UPDATE: finally managed to come up with this answer. a big thanks to Mr. Ravichandra. This is for anyone who might need something like this later. The code is not perfect and can be improved upon. The output is satisfactory and prints out a pattern almost similar to the desired output pattern.
Hint :
Have a nested loop with index i,j;
Algorithm to be used inside the loop:
i == 0
print x axis values by printing(j+1)% 10
[see note at end]j == 0
print y axis values by printingi % 10
i == j
print'\'
(i*i) + (j*j)
) returns 1, print'*'
Specifications for is_perfect_square function: A function that returns 1 if the input is a perfect square and 0 otherwise. For example:
is_perfect_square(25)
shouldreturn 1
is_perfect_square(7)
shouldreturn 0
is_perfect_square(49)
shouldreturn 1
Note:
i == 0
case should printj%10
to start the output with 0 to represent origin. But the output provided in question starts with 1. Hence using(j+1)%10
You might need to handle some corner cases, which should be straight forward once this algorithm is implemented in code.
EASY WAY
If you manage to print all your stuff in the right order, you can easily avoid unnecessary
if
statements and optimize the math calc as a side effect.To find the Pythagorean triples I've modified your first method, so I can avoid the calls to
sqrt
at every position.The output of this program is:
LESS EASY WAY
I'll show you another method to print out what you want.
Consider using an array of strings as a drawing space, stored in a struct. It seems a complication, but you can simplify and maybe generalize the output procedures:
The output of this code is the same as the previous snippet, but, believe it or not, this is faster (at least on my system) due to the less numbers of function calls that print to
stdout
.ADDENDUM
It's way off topic, but I had fun adapting the previous code to actually output an image file (as a grayscaled 512x512 PGM binary formatted file) representing all the triples with side lengths up to 8192.
Every pixel correspond to a 16x16 square block, black colored if there is no match or lighter depending on how many triples the alghorithm found in the block.
The output picture is (once converted to PNG to post here) this. Note the emerging patterns: