Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol2.c File Reference

Problem 9 solution More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for sol2.c:

Functions

int main (void)
 

Detailed Description

Problem 9 solution

Author
Krishna Vedala

Problem Statement: A Pythagorean triplet is a set of three natural numbers, \(a < b < c\), for which, \(a^2 + b^2 = c^2\). For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\). There exists exactly one Pythagorean triplet for which \(a + b + c = 1000\). Find the product abc.

Given \(a^2 + b^2 = c^2\) and \(a+b+c = n\), we can write:

\begin{eqnarray*} b &=& \frac{n^2 - 2an}{2n - 2a}\\ c &=& n - a - b \end{eqnarray*}

Function Documentation

◆ main()

int main ( void  )

Main function

24 {
25  int N = 1000;
26 
27  for (int a = 1; a < 300; a++)
28  {
29  long tmp1 = N * N - 2 * a * N;
30  long tmp2 = 2 * (N - a);
31  div_t tmp3 = div(tmp1, tmp2);
32  int b = tmp3.quot;
33  int c = N - a - b;
34 
35  if (a * a + b * b == c * c)
36  {
37  printf("%d x %d x %d = %ld\n", a, b, c, (long int)a * b * c);
38  return 0;
39  }
40  }
41 
42  return 0;
43 }
N
#define N
Definition: sol1.c:109