From 30bc9a201fced98b81b893bc43daa456ab0335bc Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sun, 29 Mar 2020 21:20:49 -0400 Subject: [PATCH 1/4] brute force method for Euler# 09 --- project_euler/Problem 09/sol1.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 project_euler/Problem 09/sol1.c diff --git a/project_euler/Problem 09/sol1.c b/project_euler/Problem 09/sol1.c new file mode 100644 index 00000000..1893425c --- /dev/null +++ b/project_euler/Problem 09/sol1.c @@ -0,0 +1,18 @@ +#include + +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; +} \ No newline at end of file From ebb887a253588604163b7bae2467b3c59523fdfb Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 30 Mar 2020 01:21:21 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5ef05421..f4b4fd13 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -223,6 +223,8 @@ * [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2006/sol.c) * Problem 07 * [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2007/sol.c) + * Problem 09 + * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2009/sol1.c) ## Searching * [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c) From 51115bf0b9e5869185d190b100601bf2687b2c39 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Sun, 29 Mar 2020 21:33:58 -0400 Subject: [PATCH 3/4] optimized solution using only one loop copied from - https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol2.py --- project_euler/Problem 09/sol2.c | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 project_euler/Problem 09/sol2.c diff --git a/project_euler/Problem 09/sol2.c b/project_euler/Problem 09/sol2.c new file mode 100644 index 00000000..fc52c588 --- /dev/null +++ b/project_euler/Problem 09/sol2.c @@ -0,0 +1,38 @@ +#include +#include + +/** + 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; +} \ No newline at end of file From 458404a6cb1ea9144770c4de4f5a5e9ab50345b5 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 30 Mar 2020 01:35:20 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f4b4fd13..6c8ce303 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -225,6 +225,7 @@ * [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2007/sol.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)