diff --git a/src/multiplication.c b/src/multiplication.c new file mode 100644 index 0000000..1832133 --- /dev/null +++ b/src/multiplication.c @@ -0,0 +1,264 @@ +#include "multiplication.h" +#include +#include +#include + + +double multi( double ein1, double ein2) { + return (ein1 * ein2); +} + +double square(double x) { + return x * x; +} + +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; +} +int multiply_right_shift(int a, int b) { + return a >> b; +} +int multiply_by_ten(int a) { + return a * 10; +} + +float multiply_by_percentage(float num, float percentage) { + return num * (percentage / 100); +} + +int multiply_by_random(int num) { + int random_factor = random_factor % 10 + 1; + 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; +} + +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; +} + +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]; +} + +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; +} + +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; +} + +int multiply_using_increment(int num1, int num2) { + int result = 0; + for (int i = 0; i < num2; ++i) { + result += num1; + } + return result; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +int multiply_hex_numbers(int num1, int num2) { + int result = 0; + while (num2 != 0) { + if (num2 & 1) { + result += num1; + } + 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; +} + +int multiply_by_euler_prime(int num) { + return (num << 1) + (num << 2) - num; +} + +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; +} + +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; +} + +int bitwise_multiply(int num1, int num2) { + int result = 0; + while (num1) { + if (num1 & 1) { + result += num2; + } + num1 >>= 1; + num2 <<= 1; + } + 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 new file mode 100644 index 0000000..8653b4a --- /dev/null +++ b/src/multiplication.h @@ -0,0 +1,41 @@ +#ifndef MULTIPLICATION_H +#define MULTIPLICATION_H +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); +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); +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); +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); +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); +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); +int bitwise_multiply(int num1, int num2); +int karatsuba_multiply(int num1, int num2); + + + + + +#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