mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
Merge branch 'project_euler/problem_09' into project_euler/master
* project_euler/problem_09: updating DIRECTORY.md optimized solution using only one loop copied from - https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol2.py updating DIRECTORY.md brute force method for Euler# 09 # Conflicts: # DIRECTORY.md
This commit is contained in:
commit
058da0b344
@ -226,6 +226,9 @@
|
||||
* Problem 08
|
||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2008/sol1.c)
|
||||
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2008/sol2.c)
|
||||
* Problem 09
|
||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2009/sol1.c)
|
||||
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2009/sol2.c)
|
||||
|
||||
## Searching
|
||||
* [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)
|
||||
|
18
project_euler/Problem 09/sol1.c
Normal file
18
project_euler/Problem 09/sol1.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int a = 1; a < 300; a++)
|
||||
for (int b = a+1; b < 400; b++)
|
||||
for (int c = b+1; c < 500; c++)
|
||||
{
|
||||
if (a * a + b * b == c * c)
|
||||
if (a + b + c == 1000)
|
||||
{
|
||||
printf("%d x %d x %d = %ld\n", a, b, c, (long int) a*b*c);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
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