mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
optimized solution using only one loop
copied from - https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol2.py
This commit is contained in:
parent
30bc9a201f
commit
51115bf0b9
38
project_euler/Problem 09/sol2.c
Normal file
38
project_euler/Problem 09/sol2.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
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:
|
||||
b = (n^2 - 2*a*n) / (2*n - 2*a)
|
||||
c = n - a - b
|
||||
**/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int N = 1000;
|
||||
|
||||
for (int a = 1; a < 300; a++)
|
||||
{
|
||||
long tmp1 = N * N - 2 * a * N;
|
||||
long tmp2 = 2 * (N - a);
|
||||
div_t tmp3 = div(tmp1, tmp2);
|
||||
int b = tmp3.quot;
|
||||
int c = N - a - b;
|
||||
|
||||
if (a * a + b * b == c * c)
|
||||
{
|
||||
printf("%d x %d x %d = %ld\n", a, b, c, (long int) a*b*c);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user