# Developers Planet

Soul February 2016

### Print a representation of the Pythagorean Triple in C

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        ``````
``` ```
``` ```
``` Answers Ravichandra Sutrave February 2016 Hint : Have a nested loop with index i,j; for i 0.. limit { for j 0 .. limit { /*Implement the below algorithm here!!*/ } printf("\n") } Algorithm to be used inside the loop: if i == 0 print x axis values by printing (j+1)% 10 [see note at end] else if j == 0 print y axis values by printing i % 10 else if i == j print '\' else if is_perfect_square((i*i) + (j*j)) returns 1, print '*' else print a space. 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) should return 1 is_perfect_square(7) should return 0 is_perfect_square(49) should return 1 Note: i == 0 case should print j%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. Bob__ February 2016 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. #include <stdio.h> #include <math.h> // all the loops will test numbers from 1 to 60 included #define LIMIT 61 int main ( int argc, char * argv[] ) { int i, j, k, k2, sum; // print the first line of reference numbers // start with a space to allign the axes putchar(' '); // print every digit... for ( i = 1; i < LIMIT; ++i ) putchar('0' + i%10); // then newline putchar('\n'); // now print every row for ( i = 1; i < LIMIT; ++i ) { // first print the number putchar('0' + i%10); // then test if the indeces (i,j) form a triple: i^2 + j^2 = k^2 // I don't want to call sqrt() every time, so I'll use another approach k = i; k2 = k * k; for ( j = 1; j < i; ++j ) { // this ^^^^^ condition let me find only unique triples and print // only the bottom left part of the picture // compilers should be (and they are) smart enough to optimize this sum = i * i + j * j; // find the next big enough k^2 if ( sum > k2 ) { ++k; k2 = k * k; } // test if the triple i,j,k matches the Pythagorean equation if ( sum == k2 ) // it's a match, so print a '*' putchar('*'); else // otherwise print a space putchar(' '); } // the line is finished, print the diagonal (with a '\\') and newline printf("\\\n"); // An alternative could be to put the reference n Soul February 2016 #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); } } } } printf(" "); 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("*"); } else { printf(" "); } } printf("\n"); } } bool is_perfect_square (int num) { int root = (int)(sqrt(num)); if (num == root * root) { return true; } else { return false; } } 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. ```
``` Post Status Asked in February 2016Viewed 2,511 timesVoted 11Answered 3 times Search Leave an answer ```
``` ```
``` ```
``` Quote of the day: live life .btn-primary{ background-color: #f44336 !important; border-color: #f44336 !important; } Devs Planet ® 2014-2016 www.devsplanet.com Devs Planet © all rights reserved Quick Actions Search // Used to toggle the menu on small screens when clicking on the menu button function myFunction() { var x = document.getElementById("navDemo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className = x.className.replace(" w3-show", ""); } } ```