From 3fa1f1877a00cb2e101c471a1820a36544202a81 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 21:46:43 +0100 Subject: [PATCH 01/15] defined ggT.h --- src/ggT/ggt.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/ggT/ggt.h diff --git a/src/ggT/ggt.h b/src/ggT/ggt.h new file mode 100644 index 0000000..6823031 --- /dev/null +++ b/src/ggT/ggt.h @@ -0,0 +1,6 @@ +#ifndef GGT_H +#define GGT_H + +int ggt(int a, int b); + +#endif From 28e2262a263f0f2bea60ae08714db9fb76ab225d Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 22:02:04 +0100 Subject: [PATCH 02/15] added prototype function ggT(int a, int b) --- src/ggT/ggT.c | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/ggT/ggT.c diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c new file mode 100644 index 0000000..ca3f5be --- /dev/null +++ b/src/ggT/ggT.c @@ -0,0 +1,4 @@ +#include + + +int ggT(int a, int b); \ No newline at end of file From e5293217a7656029b183923c8ad7eef1c2cd3b79 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 22:39:07 +0100 Subject: [PATCH 03/15] renamed from ggt.h to ggT.h --- src/ggT/{ggt.h => ggT.h} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/ggT/{ggt.h => ggT.h} (61%) diff --git a/src/ggT/ggt.h b/src/ggT/ggT.h similarity index 61% rename from src/ggT/ggt.h rename to src/ggT/ggT.h index 6823031..4dc2850 100644 --- a/src/ggT/ggt.h +++ b/src/ggT/ggT.h @@ -1,6 +1,6 @@ #ifndef GGT_H #define GGT_H -int ggt(int a, int b); +int ggT(int a, int b); #endif From 8bb76200ce7372631e3c1449d0a9ce5f668b333a Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 22:42:55 +0100 Subject: [PATCH 04/15] implemented basic function for the ggT --- src/ggT/ggT.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c index ca3f5be..484acb6 100644 --- a/src/ggT/ggT.c +++ b/src/ggT/ggT.c @@ -1,4 +1,13 @@ #include -int ggT(int a, int b); \ No newline at end of file +int ggT(int a, int b){ + while(a != b){ + if(a < b){ + b -= a; + } else { + a -= b; + } + } + return a; +} \ No newline at end of file From 80e07d854ec7fb6e9ad0cbdf3ce42353d50db305 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 22:43:32 +0100 Subject: [PATCH 05/15] added simple test for ggT of 2 and 10 --- test/ggT/test_ggT.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/ggT/test_ggT.c diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c new file mode 100644 index 0000000..3b46367 --- /dev/null +++ b/test/ggT/test_ggT.c @@ -0,0 +1,18 @@ +#include "unity.h" +#include "ggT.h" + +void setUp(){} +void tearDown(){} + +void test_ggTOf2And10(){ + //arrange + int a = 2; + int b = 10; + int expected = 5; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_STRING(expected, result); +} From 1fdc7373670b644e6dcdb93efef08b8a4ec0c4c5 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:05:49 +0100 Subject: [PATCH 06/15] refactoring: changed to Euclidean modulo algorithm --- src/ggT/ggT.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c index 484acb6..cccb8e9 100644 --- a/src/ggT/ggT.c +++ b/src/ggT/ggT.c @@ -2,12 +2,10 @@ int ggT(int a, int b){ - while(a != b){ - if(a < b){ - b -= a; - } else { - a -= b; - } + while (b != 0) { + int temp = b; + b = a % b; + a = temp; } return a; } \ No newline at end of file From d76b8867fea6f4885d6d7fa552e661e5ceb2ba13 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:07:58 +0100 Subject: [PATCH 07/15] added edge cases for one or both numbers = 0 --- src/ggT/ggT.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c index cccb8e9..ac0dd30 100644 --- a/src/ggT/ggT.c +++ b/src/ggT/ggT.c @@ -2,6 +2,14 @@ int ggT(int a, int b){ + if(a == 0 && b == 0){ + return 0; + } else if(a == 0){ + return b; + } else if(b == 0){ + return a; + } + while (b != 0) { int temp = b; b = a % b; From 9074ec8f018d313134997d826a0b7e79236a7147 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:15:12 +0100 Subject: [PATCH 08/15] fixed test_ggTOf5And10 --- test/ggT/test_ggT.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index 3b46367..c8473d5 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -4,9 +4,9 @@ void setUp(){} void tearDown(){} -void test_ggTOf2And10(){ +void test_ggTOf5And10(){ //arrange - int a = 2; + int a = 5; int b = 10; int expected = 5; @@ -14,5 +14,5 @@ void test_ggTOf2And10(){ int result = ggT(a,b); //assert - TEST_ASSERT_EQUAL_STRING(expected, result); + TEST_ASSERT_EQUAL_INT(expected, result); } From b49e556e9e9742c4603d9fc0e86a22f56504c353 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:16:01 +0100 Subject: [PATCH 09/15] added test for first number 0 and second number positive --- test/ggT/test_ggT.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index c8473d5..6230370 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -16,3 +16,16 @@ void test_ggTOf5And10(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); } + +void test_ggTOf0AndPositiveNumber(){ + //arrange + int a = 0; + int b = 5; + int expected = 5; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} From b054c14fceef604ddd225e789cd3bfe8ef08e05d Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:22:23 +0100 Subject: [PATCH 10/15] added test for first number positive and second number 0 --- test/ggT/test_ggT.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index 6230370..f2d3f12 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -29,3 +29,16 @@ void test_ggTOf0AndPositiveNumber(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); } + +void test_ggTOfPositiveAnd0(){ + //arrange + int a = 10; + int b = 0; + int expected = 10; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected, result); +} \ No newline at end of file From 5a03f4763940b17d5191e73d84c726744274567f Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:24:39 +0100 Subject: [PATCH 11/15] added test for both numbers 0 --- test/ggT/test_ggT.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index f2d3f12..8fabac0 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -41,4 +41,17 @@ void test_ggTOfPositiveAnd0(){ //assert TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_ggTOfBoth0(){ + //arrange + int a = 0; + int b = 0; + int expected = 0; + + //act + int result = ggT(a,b); + + //assert + TEST_ASSERT_EQUAL_INT(expected,result); } \ No newline at end of file From e7da9ff1087cc78bbe541914a1ddba57541f0c47 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Thu, 1 Feb 2024 23:28:12 +0100 Subject: [PATCH 12/15] added test for coprime numbers --- test/ggT/test_ggT.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index 8fabac0..5730eac 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -52,6 +52,19 @@ void test_ggTOfBoth0(){ //act int result = ggT(a,b); + //assert + TEST_ASSERT_EQUAL_INT(expected,result); +} + +void test_ggTOfCoprimes(){ + //arrange + int a = 13; + int b = 27; + int expected = 1; + + //act + int result = ggT(a,b); + //assert TEST_ASSERT_EQUAL_INT(expected,result); } \ No newline at end of file From 9ed80a6f35a78c4d61c0e0b21f22c5a6c13f4b65 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Fri, 2 Feb 2024 12:38:16 +0100 Subject: [PATCH 13/15] refactoring: added comments for better understanding --- src/ggT/ggT.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c index ac0dd30..7f28bbc 100644 --- a/src/ggT/ggT.c +++ b/src/ggT/ggT.c @@ -2,14 +2,16 @@ int ggT(int a, int b){ + // handles the case if both inputs are 0 if(a == 0 && b == 0){ return 0; - } else if(a == 0){ + } else if(a == 0){ // handles the case if first number is 0 return b; - } else if(b == 0){ + } else if(b == 0){ // handles the case if second number is 0 return a; } + // Euclidean algorithm with modulo for getting the ggT while (b != 0) { int temp = b; b = a % b; From 6aad7be0b25ad05deec982393d1576520c504b5e Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Fri, 2 Feb 2024 12:49:10 +0100 Subject: [PATCH 14/15] refactoring: simplified the zero cases --- src/ggT/ggT.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/ggT/ggT.c b/src/ggT/ggT.c index 7f28bbc..ef9b8ef 100644 --- a/src/ggT/ggT.c +++ b/src/ggT/ggT.c @@ -2,14 +2,9 @@ int ggT(int a, int b){ - // handles the case if both inputs are 0 - if(a == 0 && b == 0){ - return 0; - } else if(a == 0){ // handles the case if first number is 0 - return b; - } else if(b == 0){ // handles the case if second number is 0 - return a; - } + // handles the zero cases + if(a == 0) return b; + if(b == 0) return a; // Euclidean algorithm with modulo for getting the ggT while (b != 0) { From b99246a677b4cf9af94b7200fbfed1340262beb1 Mon Sep 17 00:00:00 2001 From: cxnnqr Date: Fri, 2 Feb 2024 12:54:32 +0100 Subject: [PATCH 15/15] refactoring: renamed tests for simplified the zero cases --- test/ggT/test_ggT.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ggT/test_ggT.c b/test/ggT/test_ggT.c index 5730eac..6c14258 100644 --- a/test/ggT/test_ggT.c +++ b/test/ggT/test_ggT.c @@ -17,7 +17,7 @@ void test_ggTOf5And10(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_ggTOf0AndPositiveNumber(){ +void test_ggTOfFirstNumberZero(){ //arrange int a = 0; int b = 5; @@ -30,7 +30,7 @@ void test_ggTOf0AndPositiveNumber(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_ggTOfPositiveAnd0(){ +void test_ggTOfSecondNumberZero(){ //arrange int a = 10; int b = 0; @@ -43,7 +43,7 @@ void test_ggTOfPositiveAnd0(){ TEST_ASSERT_EQUAL_INT(expected, result); } -void test_ggTOfBoth0(){ +void test_ggTOfBothNumbersZero(){ //arrange int a = 0; int b = 0;