From 9911b1d974cd85f872a0a369778204e1c89ef643 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Tue, 6 Feb 2024 13:49:50 +0100 Subject: [PATCH 01/32] added multi function --- src/multiplication.c | 11 +++++++++++ src/multiplication.h | 4 ++++ test/test_multiplication.c | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 src/multiplication.c create mode 100644 src/multiplication.h create mode 100644 test/test_multiplication.c diff --git a/src/multiplication.c b/src/multiplication.c new file mode 100644 index 0000000..7b29b87 --- /dev/null +++ b/src/multiplication.c @@ -0,0 +1,11 @@ +#include "multiplication.h" +#include + + +double multi( double ein1, double ein2) { + return (ein1 * ein2); +} + + + + diff --git a/src/multiplication.h b/src/multiplication.h new file mode 100644 index 0000000..a8570d0 --- /dev/null +++ b/src/multiplication.h @@ -0,0 +1,4 @@ +#ifndef MULTIPLICATION_H +#define MULTIPLICATION_H +extern double multi(double ein1, double ein2); +#endif // MULTIPLICATION_H diff --git a/test/test_multiplication.c b/test/test_multiplication.c new file mode 100644 index 0000000..1f564ac --- /dev/null +++ b/test/test_multiplication.c @@ -0,0 +1,20 @@ +#ifdef TEST + +#include "unity.h" + +#include "multiplication.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_multiplication_NeedToImplement(void) +{ + TEST_IGNORE_MESSAGE("Need to Implement multiplication"); +} + +#endif // TEST From c64fbf526be07343d5a1ba90e30f3804bbeda9d5 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Wed, 7 Feb 2024 22:48:56 +0100 Subject: [PATCH 02/32] double square --- src/multiplication.c | 6 ++++++ src/multiplication.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 7b29b87..30e335c 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -6,6 +6,12 @@ double multi( double ein1, double ein2) { return (ein1 * ein2); } +double square(double x) { + return x * x; +} + + + diff --git a/src/multiplication.h b/src/multiplication.h index a8570d0..c16d9ca 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -1,4 +1,6 @@ #ifndef MULTIPLICATION_H #define MULTIPLICATION_H extern double multi(double ein1, double ein2); +double square(double x); #endif // MULTIPLICATION_H + From 2f7b0d79a87201eef423b662c51ca9dceb0ba2b4 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Wed, 7 Feb 2024 23:14:32 +0100 Subject: [PATCH 03/32] multiplication of three integers --- src/multiplication.c | 4 ++++ src/multiplication.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 30e335c..d9cca83 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -10,6 +10,10 @@ double square(double x) { return x * x; } +int multiply_three_integers(int a, int b, int c) { + return a * b * c; +} + diff --git a/src/multiplication.h b/src/multiplication.h index c16d9ca..441d28a 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -2,5 +2,6 @@ #define MULTIPLICATION_H extern double multi(double ein1, double ein2); double square(double x); +int multiply_three_integers(int a, int b, int c); #endif // MULTIPLICATION_H From 53fce56ca795958295c0810927f356d844fb9eeb Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Wed, 7 Feb 2024 23:20:39 +0100 Subject: [PATCH 04/32] multiplication left shift --- src/multiplication.c | 3 +++ src/multiplication.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index d9cca83..1b3717e 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -14,6 +14,9 @@ int multiply_three_integers(int a, int b, int c) { return a * b * c; } +int multiply_left_shift(int a, int b) { + return a << b; +} diff --git a/src/multiplication.h b/src/multiplication.h index 441d28a..c4fdd6b 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -3,5 +3,7 @@ extern double multi(double ein1, double ein2); double square(double x); int multiply_three_integers(int a, int b, int c); +int multiply_left_shift(int a, int b); + #endif // MULTIPLICATION_H From e1c962683b2588501603225eddcf2ce4d26a4379 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Wed, 7 Feb 2024 23:27:51 +0100 Subject: [PATCH 05/32] multiplication right shift --- src/multiplication.c | 3 +++ src/multiplication.h | 1 + 2 files changed, 4 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 1b3717e..58f1e8e 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -17,6 +17,9 @@ int multiply_three_integers(int a, int b, int c) { int multiply_left_shift(int a, int b) { return a << b; } +int multiply_right_shift(int a, int b) { + return a >> b; +} diff --git a/src/multiplication.h b/src/multiplication.h index c4fdd6b..0a77b5a 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -4,6 +4,7 @@ extern double multi(double ein1, double ein2); double square(double x); int multiply_three_integers(int a, int b, int c); int multiply_left_shift(int a, int b); +int multiply_right_shift(int a, int b); #endif // MULTIPLICATION_H From 1afe231b7c153546387c9a30a03ff9e6960cfeff Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Wed, 7 Feb 2024 23:33:38 +0100 Subject: [PATCH 06/32] multiplication by ten --- src/multiplication.c | 3 +++ src/multiplication.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/multiplication.c b/src/multiplication.c index 58f1e8e..bf381f4 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -20,6 +20,9 @@ int multiply_left_shift(int a, int b) { int multiply_right_shift(int a, int b) { return a >> b; } +int multiply_by_ten(int a) { + return a * 10; +} diff --git a/src/multiplication.h b/src/multiplication.h index 0a77b5a..5d0b41c 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -5,6 +5,6 @@ double square(double x); int multiply_three_integers(int a, int b, int c); int multiply_left_shift(int a, int b); int multiply_right_shift(int a, int b); - +int multiply_by_ten(int a); #endif // MULTIPLICATION_H From 8a60c03078006d2f1eb9fe97ba626028c828fa77 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 00:05:03 +0100 Subject: [PATCH 07/32] multiplication by percentage --- src/multiplication.c | 3 +++ src/multiplication.h | 1 + 2 files changed, 4 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index bf381f4..0bb5f0e 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -24,6 +24,9 @@ int multiply_by_ten(int a) { return a * 10; } +float multiply_by_percentage(float num, float percentage) { + return num * (percentage / 100); +} diff --git a/src/multiplication.h b/src/multiplication.h index 5d0b41c..9f09f48 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -6,5 +6,6 @@ int multiply_three_integers(int a, int b, int c); int multiply_left_shift(int a, int b); int multiply_right_shift(int a, int b); int multiply_by_ten(int a); +float multiply_by_percentage(float num, float percentage); #endif // MULTIPLICATION_H From d39998fe0f8907519a840a8fece598fc851377c4 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 00:33:28 +0100 Subject: [PATCH 08/32] multiplication by random --- src/multiplication.c | 5 +++++ src/multiplication.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 0bb5f0e..cef7c1c 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -28,6 +28,11 @@ float multiply_by_percentage(float num, float percentage) { return num * (percentage / 100); } +int multiply_by_random(int num) { + int random_factor = rand() % 10 + 1; + return num * random_factor; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 9f09f48..894f020 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -7,5 +7,7 @@ int multiply_left_shift(int a, int b); int multiply_right_shift(int a, int b); int multiply_by_ten(int a); float multiply_by_percentage(float num, float percentage); +int multiply_by_random(int num); + #endif // MULTIPLICATION_H From b484b79f1f0f94a9f32cdb781bfc9a5d9ebeae3a Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 00:48:17 +0100 Subject: [PATCH 09/32] multiplication string --- src/multiplication.c | 10 ++++++++++ src/multiplication.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index cef7c1c..24be7fd 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -1,5 +1,7 @@ #include "multiplication.h" #include +#include + double multi( double ein1, double ein2) { @@ -33,6 +35,14 @@ int multiply_by_random(int num) { return num * random_factor; } +int multiply_string(const char* str, int factor) { + int len = strlen(str); + int result = 0; + for (int i = 0; i < len; i++) { + result += str[i] * factor; + } + return result; +} diff --git a/src/multiplication.h b/src/multiplication.h index 894f020..1e6fddf 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -8,6 +8,8 @@ int multiply_right_shift(int a, int b); int multiply_by_ten(int a); float multiply_by_percentage(float num, float percentage); int multiply_by_random(int num); +int multiply_string(const char* str, int factor); + #endif // MULTIPLICATION_H From 8cc602428e4caaa46f87626ed14ecd3849fcea0d Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 00:57:02 +0100 Subject: [PATCH 10/32] multiplication by sum --- src/multiplication.c | 4 ++++ src/multiplication.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 24be7fd..b583823 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -44,5 +44,9 @@ int multiply_string(const char* str, int factor) { return result; } +int multiply_by_sum(int num1, int num2) { + return num1 * (num2 + 1); +} + diff --git a/src/multiplication.h b/src/multiplication.h index 1e6fddf..77be6d8 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -9,6 +9,7 @@ int multiply_by_ten(int a); float multiply_by_percentage(float num, float percentage); int multiply_by_random(int num); int multiply_string(const char* str, int factor); +int multiply_by_sum(int num1, int num2); #endif // MULTIPLICATION_H From f112747e8ab07ecc06a8a0483f18cec4c135070d Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:05:02 +0100 Subject: [PATCH 11/32] multiplication using two complement --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index b583823..143fa25 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -48,5 +48,17 @@ int multiply_by_sum(int num1, int num2) { return num1 * (num2 + 1); } +int multiply_using_two_complements(int num1, int num2) { + int result = 0; + while (num1 != 0) { + if (num1 & 1) { + result += num2; + } + num1 >>= 1; + num2 <<= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 77be6d8..dd99a1f 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -10,6 +10,7 @@ float multiply_by_percentage(float num, float percentage); int multiply_by_random(int num); int multiply_string(const char* str, int factor); int multiply_by_sum(int num1, int num2); +int multiply_using_two_complements(int num1, int num2); #endif // MULTIPLICATION_H From 1f58e49e5539a30aeb44aafbef49e0230aafa0d0 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:08:59 +0100 Subject: [PATCH 12/32] multiplication by even --- src/multiplication.c | 4 ++++ src/multiplication.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 143fa25..3c5c479 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -60,5 +60,9 @@ int multiply_using_two_complements(int num1, int num2) { return result; } +int multiply_by_even(int num, int factor) { + return (num >> 1) << (factor + 1); +} + diff --git a/src/multiplication.h b/src/multiplication.h index dd99a1f..a3c42b1 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -11,6 +11,7 @@ int multiply_by_random(int num); int multiply_string(const char* str, int factor); int multiply_by_sum(int num1, int num2); int multiply_using_two_complements(int num1, int num2); +int multiply_by_even(int num, int factor); #endif // MULTIPLICATION_H From d35e733799bcd1131f5e0f15657f5c5f7fff37d3 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:15:15 +0100 Subject: [PATCH 13/32] multiplication using lookup table --- src/multiplication.c | 16 ++++++++++++++++ src/multiplication.h | 1 + 2 files changed, 17 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 3c5c479..9cbc11f 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -64,5 +64,21 @@ int multiply_by_even(int num, int factor) { return (num >> 1) << (factor + 1); } +int multiply_using_lookup_table(int num1, int num2) { + int lookup_table[10][10] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}, + {0, 3, 6, 9, 12, 15, 18, 21, 24, 27}, + {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}, + {0, 5, 10, 15, 20, 25, 30, 35, 40, 45}, + {0, 6, 12, 18, 24, 30, 36, 42, 48, 54}, + {0, 7, 14, 21, 28, 35, 42, 49, 56, 63}, + {0, 8, 16, 24, 32, 40, 48, 56, 64, 72}, + {0, 9, 18, 27, 36, 45, 54, 63, 72, 81} + }; + return lookup_table[num1][num2]; +} + diff --git a/src/multiplication.h b/src/multiplication.h index a3c42b1..378e860 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -12,6 +12,7 @@ int multiply_string(const char* str, int factor); int multiply_by_sum(int num1, int num2); int multiply_using_two_complements(int num1, int num2); int multiply_by_even(int num, int factor); +int multiply_using_lookup_table(int num1, int num2); #endif // MULTIPLICATION_H From c9ef902922cab37921ab5fd18bac0d6740e44bc3 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:20:15 +0100 Subject: [PATCH 14/32] multiplication using logical operations --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 9cbc11f..06056de 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -80,5 +80,17 @@ int multiply_using_lookup_table(int num1, int num2) { return lookup_table[num1][num2]; } +int multiply_using_logical_operations(int num1, int num2) { + int result = 0; + while (num2) { + if (num2 & 1) { + result ^= num1; + } + num1 <<= 1; + num2 >>= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 378e860..b242c9b 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -13,6 +13,7 @@ int multiply_by_sum(int num1, int num2); int multiply_using_two_complements(int num1, int num2); int multiply_by_even(int num, int factor); int multiply_using_lookup_table(int num1, int num2); +int multiply_using_logical_operations(int num1, int num2); #endif // MULTIPLICATION_H From 798be4d82c735601f7506c4e2d05754c68a1201a Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:24:25 +0100 Subject: [PATCH 15/32] multiplication using modulo --- src/multiplication.c | 4 ++++ src/multiplication.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 06056de..a162d6b 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -92,5 +92,9 @@ int multiply_using_logical_operations(int num1, int num2) { return result; } +int multiply_using_modulo(int num1, int num2, int modulo) { + return (num1 % modulo) * (num2 % modulo) % modulo; +} + diff --git a/src/multiplication.h b/src/multiplication.h index b242c9b..a0d92dd 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -14,6 +14,7 @@ int multiply_using_two_complements(int num1, int num2); int multiply_by_even(int num, int factor); int multiply_using_lookup_table(int num1, int num2); int multiply_using_logical_operations(int num1, int num2); +int multiply_using_modulo(int num1, int num2, int modulo); #endif // MULTIPLICATION_H From fb9c872867eacf119683e24467c063ffef401d47 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:28:23 +0100 Subject: [PATCH 16/32] multiplication by prime --- src/multiplication.c | 5 +++++ src/multiplication.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index a162d6b..201093f 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -96,5 +96,10 @@ int multiply_using_modulo(int num1, int num2, int modulo) { return (num1 % modulo) * (num2 % modulo) % modulo; } +int multiply_by_prime(int num, int prime) { + return num * (prime - 1) + num; +} + + diff --git a/src/multiplication.h b/src/multiplication.h index a0d92dd..26da04e 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -15,6 +15,7 @@ int multiply_by_even(int num, int factor); int multiply_using_lookup_table(int num1, int num2); int multiply_using_logical_operations(int num1, int num2); int multiply_using_modulo(int num1, int num2, int modulo); +int multiply_by_prime(int num, int prime); #endif // MULTIPLICATION_H From 78f549c633389c8dd8480d8d0a77588a94dbfe2f Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:33:28 +0100 Subject: [PATCH 17/32] multiplication using increment --- src/multiplication.c | 7 +++++++ src/multiplication.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 201093f..d0fcf59 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -100,6 +100,13 @@ int multiply_by_prime(int num, int prime) { return num * (prime - 1) + num; } +int multiply_using_increment(int num1, int num2) { + int result = 0; + for (int i = 0; i < num2; ++i) { + result += num1; + } + return result; +} diff --git a/src/multiplication.h b/src/multiplication.h index 26da04e..4955c4d 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -16,6 +16,7 @@ int multiply_using_lookup_table(int num1, int num2); int multiply_using_logical_operations(int num1, int num2); int multiply_using_modulo(int num1, int num2, int modulo); int multiply_by_prime(int num, int prime); +int multiply_using_increment(int num1, int num2); #endif // MULTIPLICATION_H From 5b92dee256315a2d38179125cc204bc5297b86f3 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:44:06 +0100 Subject: [PATCH 18/32] multiplication by prime and its square --- src/multiplication.c | 8 ++++++-- src/multiplication.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/multiplication.c b/src/multiplication.c index d0fcf59..e222e4d 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -1,7 +1,7 @@ #include "multiplication.h" #include #include - +#include double multi( double ein1, double ein2) { @@ -31,7 +31,7 @@ float multiply_by_percentage(float num, float percentage) { } int multiply_by_random(int num) { - int random_factor = rand() % 10 + 1; + int random_factor = random_factor % 10 + 1; return num * random_factor; } @@ -108,5 +108,9 @@ int multiply_using_increment(int num1, int num2) { return result; } +int multiply_by_prime_and_its_square(int num, int prime) { + return num * (prime + square (prime)); +} + diff --git a/src/multiplication.h b/src/multiplication.h index 4955c4d..bd20e4a 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -17,6 +17,7 @@ int multiply_using_logical_operations(int num1, int num2); int multiply_using_modulo(int num1, int num2, int modulo); int multiply_by_prime(int num, int prime); int multiply_using_increment(int num1, int num2); +int multiply_by_prime_and_its_square(int num, int prime); #endif // MULTIPLICATION_H From 5bf1c9753eb3bfde8a8a35a1316ff17ab7344478 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:47:38 +0100 Subject: [PATCH 19/32] multiplication by odd --- src/multiplication.c | 9 +++++++++ src/multiplication.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index e222e4d..e8e4602 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -112,5 +112,14 @@ int multiply_by_prime_and_its_square(int num, int prime) { return num * (prime + square (prime)); } +int multiply_by_odd(int num, int factor) { + int result = 0; + for (int i = 0; i < factor; ++i) { + result += num; + } + return result; +} + + diff --git a/src/multiplication.h b/src/multiplication.h index bd20e4a..ff24d5e 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -18,6 +18,7 @@ int multiply_using_modulo(int num1, int num2, int modulo); int multiply_by_prime(int num, int prime); int multiply_using_increment(int num1, int num2); int multiply_by_prime_and_its_square(int num, int prime); +int multiply_by_odd(int num, int factor); #endif // MULTIPLICATION_H From c155f79362bf69072aa0d17fed482e48b44487be Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:52:02 +0100 Subject: [PATCH 20/32] multiplication using binary enumeration --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index e8e4602..db11bdd 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -120,6 +120,18 @@ int multiply_by_odd(int num, int factor) { return result; } +int multiply_using_binary_enumeration(int num1, int num2) { + int result = 0; + while (num1 && num2) { + if (num2 & 1) { + result += num1; + } + num1 <<= 1; + num2 >>= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index ff24d5e..65e2e34 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -19,6 +19,7 @@ int multiply_by_prime(int num, int prime); int multiply_using_increment(int num1, int num2); int multiply_by_prime_and_its_square(int num, int prime); int multiply_by_odd(int num, int factor); +int multiply_using_binary_enumeration(int num1, int num2); #endif // MULTIPLICATION_H From 924796010f704418c11c78c98d843eae36633a43 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:55:29 +0100 Subject: [PATCH 21/32] multiplication using differences --- src/multiplication.c | 6 ++++++ src/multiplication.h | 1 + 2 files changed, 7 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index db11bdd..9512607 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -132,6 +132,12 @@ int multiply_using_binary_enumeration(int num1, int num2) { return result; } +int multiply_using_differences(int num1, int num2) { + int difference = (num1 > num2) ? num1 - num2 : num2 - num1; + int sum = (num1 > num2) ? num1 + num2 : num2 + num1; + return (sum - difference) * difference / 4; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 65e2e34..c2b8540 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -20,6 +20,7 @@ int multiply_using_increment(int num1, int num2); int multiply_by_prime_and_its_square(int num, int prime); int multiply_by_odd(int num, int factor); int multiply_using_binary_enumeration(int num1, int num2); +int multiply_using_differences(int num1, int num2); #endif // MULTIPLICATION_H From d937085f049800c8daade51433bbdaab2cc96b0e Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 02:00:09 +0100 Subject: [PATCH 22/32] multiplication recursive optimized --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 9512607..5262de3 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -138,6 +138,18 @@ int multiply_using_differences(int num1, int num2) { return (sum - difference) * difference / 4; } +int multiply_recursive_optimized(int num1, int num2) { + if (num2 == 0) { + return 0; + } + int result = multiply_recursive_optimized(num1, num2 >> 1); + result <<= 1; + if (num2 & 1) { + result += num1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index c2b8540..1524c22 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -21,6 +21,7 @@ int multiply_by_prime_and_its_square(int num, int prime); int multiply_by_odd(int num, int factor); int multiply_using_binary_enumeration(int num1, int num2); int multiply_using_differences(int num1, int num2); +int multiply_recursive_optimized(int num1, int num2); #endif // MULTIPLICATION_H From df7be46b47981559cecfe94508599023696b53d6 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 11:43:27 +0100 Subject: [PATCH 23/32] multiplication octal numbers --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 5262de3..2704834 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -150,6 +150,18 @@ int multiply_recursive_optimized(int num1, int num2) { return result; } +int multiply_octal_numbers(int num1, int num2) { + int result = 0; + while (num2 != 0) { + if (num2 & 1) { + result += num1; + } + num1 <<= 3; // Décalage octal + num2 >>= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 1524c22..83e2f89 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -22,6 +22,7 @@ int multiply_by_odd(int num, int factor); int multiply_using_binary_enumeration(int num1, int num2); int multiply_using_differences(int num1, int num2); int multiply_recursive_optimized(int num1, int num2); +int multiply_octal_numbers(int num1, int num2); #endif // MULTIPLICATION_H From 6ff0b7ab18381d1e3f9a32bcdfd97915cfc01771 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 11:47:40 +0100 Subject: [PATCH 24/32] multiplication hex numbers --- src/multiplication.c | 14 ++++++++++++++ src/multiplication.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 2704834..24afd8c 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -159,9 +159,23 @@ int multiply_octal_numbers(int num1, int num2) { num1 <<= 3; // Décalage octal num2 >>= 1; } + return result; } +int multiply_hex_numbers(int num1, int num2) { + int result = 0; + while (num2 != 0) { + if (num2 & 1) { + result += num1; + } + num1 <<= 4; // Décalage hexadécimal + num2 >>= 1; + } + return result; +} + + diff --git a/src/multiplication.h b/src/multiplication.h index 83e2f89..890caa7 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -23,6 +23,7 @@ int multiply_using_binary_enumeration(int num1, int num2); int multiply_using_differences(int num1, int num2); int multiply_recursive_optimized(int num1, int num2); int multiply_octal_numbers(int num1, int num2); +int multiply_hex_numbers(int num1, int num2); #endif // MULTIPLICATION_H From 36de9a7d0e5a735198b694232a30703a598e7e6e Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 11:55:38 +0100 Subject: [PATCH 25/32] multiplication of exponentiation added --- src/multiplication.c | 13 ++++++++++++- src/multiplication.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/multiplication.c b/src/multiplication.c index 24afd8c..e5d98cf 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -169,12 +169,23 @@ int multiply_hex_numbers(int num1, int num2) { if (num2 & 1) { result += num1; } - num1 <<= 4; // Décalage hexadécimal + num1 <<= 4; num2 >>= 1; } return result; } +int multiply_exponentiation(int base, int exponent) { + int result = 1; + while (exponent > 0) { + if (exponent & 1) { + result *= base; + } + base *= base; + exponent >>= 1; + } + return result; +} diff --git a/src/multiplication.h b/src/multiplication.h index 890caa7..8c42303 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -24,6 +24,7 @@ int multiply_using_differences(int num1, int num2); int multiply_recursive_optimized(int num1, int num2); int multiply_octal_numbers(int num1, int num2); int multiply_hex_numbers(int num1, int num2); +int multiply_exponentiation(int base, int exponent); #endif // MULTIPLICATION_H From 3460453ea892b826fb92c68777fedfb743ad6f29 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 12:01:13 +0100 Subject: [PATCH 26/32] multiplication by euler prime added --- src/multiplication.c | 4 ++++ src/multiplication.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index e5d98cf..af39497 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -187,6 +187,10 @@ int multiply_exponentiation(int base, int exponent) { return result; } +int multiply_by_euler_prime(int num) { + return (num << 1) + (num << 2) - num; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 8c42303..eefb4ff 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -25,6 +25,7 @@ int multiply_recursive_optimized(int num1, int num2); int multiply_octal_numbers(int num1, int num2); int multiply_hex_numbers(int num1, int num2); int multiply_exponentiation(int base, int exponent); +int multiply_by_euler_prime(int num); #endif // MULTIPLICATION_H From 4f1230194d015272f78ca29313f0bad224f119e0 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 12:26:58 +0100 Subject: [PATCH 27/32] multiplication fibonacci fumktion added --- src/multiplication.c | 4 ++++ src/multiplication.h | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index af39497..94d6fcb 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -189,6 +189,10 @@ int multiply_exponentiation(int base, int exponent) { int multiply_by_euler_prime(int num) { return (num << 1) + (num << 2) - num; +} + +int fibonacci_multiply(int num, int fib) { + return num * fib; } diff --git a/src/multiplication.h b/src/multiplication.h index eefb4ff..809a214 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -26,6 +26,9 @@ int multiply_octal_numbers(int num1, int num2); int multiply_hex_numbers(int num1, int num2); int multiply_exponentiation(int base, int exponent); int multiply_by_euler_prime(int num); +int fibonacci_multiply(int num, int fib); + + #endif // MULTIPLICATION_H From 6247b18422973d274b8fc989311376cfc9dea92e Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 12:31:16 +0100 Subject: [PATCH 28/32] repeated division multiplication fonction added --- src/multiplication.c | 14 ++++++++++++++ src/multiplication.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 94d6fcb..15884c2 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -195,6 +195,20 @@ int fibonacci_multiply(int num, int fib) { return num * fib; } +int repeated_division_multiply(int num1, int num2) { + int result = 0; + while (num1 > 0) { + if (num1 % 2 == 1) { + result += num2; + } + num1 >>= 1; + num2 <<= 1; + } + return result; +} + + + diff --git a/src/multiplication.h b/src/multiplication.h index 809a214..1d81954 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -27,6 +27,7 @@ int multiply_hex_numbers(int num1, int num2); int multiply_exponentiation(int base, int exponent); int multiply_by_euler_prime(int num); int fibonacci_multiply(int num, int fib); +int repeated_division_multiply(int num1, int num2); From b0b32c18d993ec211cfbd3aa7e6f25432f534c83 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 12:43:20 +0100 Subject: [PATCH 29/32] bernouli multiplication added --- src/multiplication.c | 7 ++++++- src/multiplication.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/multiplication.c b/src/multiplication.c index 15884c2..6a3c5a3 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -205,7 +205,12 @@ int repeated_division_multiply(int num1, int num2) { num2 <<= 1; } return result; -} +} + +int bernoulli_multiply(int num, int bernoulli) { + return num * bernoulli; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 1d81954..97be960 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -28,6 +28,8 @@ int multiply_exponentiation(int base, int exponent); int multiply_by_euler_prime(int num); int fibonacci_multiply(int num, int fib); int repeated_division_multiply(int num1, int num2); +int bernoulli_multiply(int num, int bernoulli); + From 6590afc6991a49ca2622ec5f9f22f766d8c332ad Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 12:54:19 +0100 Subject: [PATCH 30/32] power multiplication fonction added --- src/multiplication.c | 10 +++++++++- src/multiplication.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/multiplication.c b/src/multiplication.c index 6a3c5a3..0734802 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -206,11 +206,19 @@ int repeated_division_multiply(int num1, int num2) { } return result; } - + int bernoulli_multiply(int num, int bernoulli) { return num * bernoulli; } +float power_multiply(float base, int exponent) { + float result = 1.0; + for (int i = 0; i < exponent; i++) { + result *= base; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 97be960..c29b077 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -29,7 +29,7 @@ int multiply_by_euler_prime(int num); int fibonacci_multiply(int num, int fib); int repeated_division_multiply(int num1, int num2); int bernoulli_multiply(int num, int bernoulli); - +float power_multiply(float base, int exponent); From ccb098243951821e32986f6017c954849ecb8a12 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 13:25:05 +0100 Subject: [PATCH 31/32] bitwise multiplication fonction added --- src/multiplication.c | 13 +++++++++++++ src/multiplication.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 0734802..062c50b 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -219,6 +219,19 @@ float power_multiply(float base, int exponent) { return result; } +int bitwise_multiply(int num1, int num2) { + int result = 0; + while (num1) { + if (num1 & 1) { + result += num2; + } + num1 >>= 1; + num2 <<= 1; + } + return result; +} + + diff --git a/src/multiplication.h b/src/multiplication.h index c29b077..342bd91 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -30,6 +30,8 @@ int fibonacci_multiply(int num, int fib); int repeated_division_multiply(int num1, int num2); int bernoulli_multiply(int num, int bernoulli); float power_multiply(float base, int exponent); +int bitwise_multiply(int num1, int num2); + From cf0cbc304b5ce57fcd8aaa021fe4e3b5b4d948cb Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 13:33:32 +0100 Subject: [PATCH 32/32] karatsuba multiplication fonction added --- src/multiplication.c | 24 ++++++++++++++++++++++++ src/multiplication.h | 1 + 2 files changed, 25 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 062c50b..1832133 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -231,6 +231,30 @@ int bitwise_multiply(int num1, int num2) { return result; } +int karatsuba_multiply(int num1, int num2) { + // Base case: If numbers are less than 10, return their product directly + if (num1 < 10 || num2 < 10) { + return num1 * num2; + } + + int m = (int)fmax(log10(num1), log10(num2)) + 1; // Maximum length of numbers + int m2 = m / 2; // Divide maximum length by two + + // Divide numbers into parts + int a = num1 / pow(10, m2); + int b = num1 % (int)pow(10, m2); + int c = num2 / pow(10, m2); + int d = num2 % (int)pow(10, m2); + + // Calculate intermediate products + int ac = karatsuba_multiply(a, c); + int bd = karatsuba_multiply(b, d); + int ad_bc = karatsuba_multiply(a + b, c + d) - ac - bd; + + // Calculate the final result + return ac * (int)pow(10, 2 * m2) + ad_bc * (int)pow(10, m2) + bd; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 342bd91..8653b4a 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -31,6 +31,7 @@ int repeated_division_multiply(int num1, int num2); int bernoulli_multiply(int num, int bernoulli); float power_multiply(float base, int exponent); int bitwise_multiply(int num1, int num2); +int karatsuba_multiply(int num1, int num2);