Browse Source

Merge branch 'multiplication' into 'master'

Multiplication

See merge request fdai7848/Linkes-Twix!11
master
fdai7930 11 months ago
parent
commit
aa73f8a6e8
  1. 1
      Linkes-Twix
  2. 92
      src/multiplication.c
  3. 66
      src/multiplication.h

1
Linkes-Twix

@ -0,0 +1 @@
Subproject commit b5e43678fdb145c0146f369ab05bf758ae913abf

92
src/multiplication.c

@ -4,38 +4,39 @@
#include <math.h> #include <math.h>
double multi( double ein1, double ein2) {
return (ein1 * ein2);
double multi( double num1, double num2) {
return (num1 * num2);
} }
double square(double x) { double square(double x) {
return x * x; return x * x;
} }
int multiply_three_integers(int a, int b, int c) {
int multi_three_integers(int a, int b, int c) {
return a * b * c; return a * b * c;
} }
int multiply_left_shift(int a, int b) {
int multi_left_shift(int a, int b) {
return a << b; return a << b;
} }
int multiply_right_shift(int a, int b) {
int multi_right_shift(int a, int b) {
return a >> b; return a >> b;
} }
int multiply_by_ten(int a) {
return a * 10;
int multi_by_five(int a) {
return a * 5;
} }
float multiply_by_percentage(float num, float percentage) {
float multi_by_percentage(float num, float percentage) {
return num * (percentage / 100); return num * (percentage / 100);
} }
int multiply_by_random(int num) {
int multi_by_random(int num) {
int random_factor = random_factor % 10 + 1; int random_factor = random_factor % 10 + 1;
return num * random_factor; return num * random_factor;
} }
int multiply_string(const char* str, int factor) {
int multi_string(const char* str, int factor) {
int len = strlen(str); int len = strlen(str);
int result = 0; int result = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@ -44,27 +45,25 @@ int multiply_string(const char* str, int factor) {
return result; return result;
} }
int multiply_by_sum(int num1, int num2) {
return num1 * (num2 + 1);
}
int multiply_using_two_complements(int num1, int num2) {
int multi_two_comp(int num2, int num3) {
int result = 0; int result = 0;
while (num1 != 0) {
if (num1 & 1) {
result += num2;
while (num2 != 0) {
if (num2 & 1) {
result += num3;
} }
num1 >>= 1;
num2 <<= 1;
num2 >>= 2;
num3 <<= 2;
} }
return result; return result;
} }
int multiply_by_even(int num, int factor) {
int multi_by_even(int num, int factor) {
return (num >> 1) << (factor + 1); return (num >> 1) << (factor + 1);
} }
int multiply_using_lookup_table(int num1, int num2) {
int multi_lookup_table(int num1, int num2) {
int lookup_table[10][10] = { int lookup_table[10][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
@ -80,7 +79,7 @@ int multiply_using_lookup_table(int num1, int num2) {
return lookup_table[num1][num2]; return lookup_table[num1][num2];
} }
int multiply_using_logical_operations(int num1, int num2) {
int multi_logical_op(int num1, int num2) {
int result = 0; int result = 0;
while (num2) { while (num2) {
if (num2 & 1) { if (num2 & 1) {
@ -92,15 +91,15 @@ int multiply_using_logical_operations(int num1, int num2) {
return result; return result;
} }
int multiply_using_modulo(int num1, int num2, int modulo) {
int multi_modulo(int num1, int num2, int modulo) {
return (num1 % modulo) * (num2 % modulo) % modulo; return (num1 % modulo) * (num2 % modulo) % modulo;
} }
int multiply_by_prime(int num, int prime) {
return num * (prime - 1) + num;
int multi_by_prime(int num, int prime) {
return num * (prime - 2) + num;
} }
int multiply_using_increment(int num1, int num2) {
int multi_increment(int num1, int num2) {
int result = 0; int result = 0;
for (int i = 0; i < num2; ++i) { for (int i = 0; i < num2; ++i) {
result += num1; result += num1;
@ -108,11 +107,11 @@ int multiply_using_increment(int num1, int num2) {
return result; return result;
} }
int multiply_by_prime_and_its_square(int num, int prime) {
int multi_by_prime_and_its_square(int num, int prime) {
return num * (prime + square (prime)); return num * (prime + square (prime));
} }
int multiply_by_odd(int num, int factor) {
int multi_by_odd(int num, int factor) {
int result = 0; int result = 0;
for (int i = 0; i < factor; ++i) { for (int i = 0; i < factor; ++i) {
result += num; result += num;
@ -120,7 +119,7 @@ int multiply_by_odd(int num, int factor) {
return result; return result;
} }
int multiply_using_binary_enumeration(int num1, int num2) {
int multi_binary_enumeration(int num1, int num2) {
int result = 0; int result = 0;
while (num1 && num2) { while (num1 && num2) {
if (num2 & 1) { if (num2 & 1) {
@ -132,17 +131,17 @@ int multiply_using_binary_enumeration(int num1, int num2) {
return result; return result;
} }
int multiply_using_differences(int num1, int num2) {
int multi_differences(int num1, int num2) {
int difference = (num1 > num2) ? num1 - num2 : num2 - num1; int difference = (num1 > num2) ? num1 - num2 : num2 - num1;
int sum = (num1 > num2) ? num1 + num2 : num2 + num1; int sum = (num1 > num2) ? num1 + num2 : num2 + num1;
return (sum - difference) * difference / 4; return (sum - difference) * difference / 4;
} }
int multiply_recursive_optimized(int num1, int num2) {
int multi_recursive_optimized(int num1, int num2) {
if (num2 == 0) { if (num2 == 0) {
return 0; return 0;
} }
int result = multiply_recursive_optimized(num1, num2 >> 1);
int result = multi_recursive_optimized(num1, num2 >> 1);
result <<= 1; result <<= 1;
if (num2 & 1) { if (num2 & 1) {
result += num1; result += num1;
@ -150,7 +149,7 @@ int multiply_recursive_optimized(int num1, int num2) {
return result; return result;
} }
int multiply_octal_numbers(int num1, int num2) {
int multi_octal_numbers(int num1, int num2) {
int result = 0; int result = 0;
while (num2 != 0) { while (num2 != 0) {
if (num2 & 1) { if (num2 & 1) {
@ -163,7 +162,7 @@ int multiply_octal_numbers(int num1, int num2) {
return result; return result;
} }
int multiply_hex_numbers(int num1, int num2) {
int multi_hex_numbers(int num1, int num2) {
int result = 0; int result = 0;
while (num2 != 0) { while (num2 != 0) {
if (num2 & 1) { if (num2 & 1) {
@ -175,7 +174,7 @@ int multiply_hex_numbers(int num1, int num2) {
return result; return result;
} }
int multiply_exponentiation(int base, int exponent) {
int multi_exponentiation(int base, int exponent) {
int result = 1; int result = 1;
while (exponent > 0) { while (exponent > 0) {
if (exponent & 1) { if (exponent & 1) {
@ -187,15 +186,15 @@ int multiply_exponentiation(int base, int exponent) {
return result; return result;
} }
int multiply_by_euler_prime(int num) {
int multi_by_euler_prime(int num) {
return (num << 1) + (num << 2) - num; return (num << 1) + (num << 2) - num;
} }
int fibonacci_multiply(int num, int fib) {
int fibonacci_multi(int num, int fib) {
return num * fib; return num * fib;
} }
int repeated_division_multiply(int num1, int num2) {
int repeated_division_multi(int num1, int num2) {
int result = 0; int result = 0;
while (num1 > 0) { while (num1 > 0) {
if (num1 % 2 == 1) { if (num1 % 2 == 1) {
@ -207,11 +206,11 @@ int repeated_division_multiply(int num1, int num2) {
return result; return result;
} }
int bernoulli_multiply(int num, int bernoulli) {
int bernoulli_multi(int num, int bernoulli) {
return num * bernoulli; return num * bernoulli;
} }
float power_multiply(float base, int exponent) {
float power_multi(float base, int exponent) {
float result = 1.0; float result = 1.0;
for (int i = 0; i < exponent; i++) { for (int i = 0; i < exponent; i++) {
result *= base; result *= base;
@ -219,7 +218,7 @@ float power_multiply(float base, int exponent) {
return result; return result;
} }
int bitwise_multiply(int num1, int num2) {
int bitwisee_multi(int num1, int num2) {
int result = 0; int result = 0;
while (num1) { while (num1) {
if (num1 & 1) { if (num1 & 1) {
@ -231,7 +230,7 @@ int bitwise_multiply(int num1, int num2) {
return result; return result;
} }
int karatsuba_multiply(int num1, int num2) {
int karatsu_multi(int num1, int num2) {
// Base case: If numbers are less than 10, return their product directly // Base case: If numbers are less than 10, return their product directly
if (num1 < 10 || num2 < 10) { if (num1 < 10 || num2 < 10) {
return num1 * num2; return num1 * num2;
@ -247,9 +246,9 @@ int karatsuba_multiply(int num1, int num2) {
int d = num2 % (int)pow(10, m2); int d = num2 % (int)pow(10, m2);
// Calculate intermediate products // 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;
int ac = karatsu_multi(a, c);
int bd = karatsu_multi(b, d);
int ad_bc = karatsu_multi(a + b, c + d) - ac - bd;
// Calculate the final result // Calculate the final result
return ac * (int)pow(10, 2 * m2) + ad_bc * (int)pow(10, m2) + bd; return ac * (int)pow(10, 2 * m2) + ad_bc * (int)pow(10, m2) + bd;
@ -260,5 +259,8 @@ int karatsuba_multiply(int num1, int num2) {

66
src/multiplication.h

@ -1,37 +1,41 @@
#ifndef MULTIPLICATION_H #ifndef MULTIPLICATION_H
#define MULTIPLICATION_H #define MULTIPLICATION_H
extern double multi(double ein1, double ein2);
extern double multi(double num1, double num2);
double square(double x); 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);
int multi_three_integers(int a, int b, int c);
int multi_left_shift(int a, int b);
int multi_right_shift(int a, int b);
int multi_by_five(int a);
float multi_by_percentage(float num, float percentage);
int multi_by_random(int num);
int multi_string(const char* str, int factor);
int multi_two_comp(int num2, int num3);
int multi_by_even(int num, int factor);
int multi_lookup_table(int num1, int num2);
int multi_logical_op(int num1, int num2);
int multi_modulo(int num1, int num2, int modulo);
int multi_by_prime(int num, int prime);
int multi_increment(int num1, int num2);
int multi_by_prime_and_its_square(int num, int prime);
int multi_by_odd(int num, int factor);
int multi_binary_enumeration(int num1, int num2);
int multi_differences(int num1, int num2);
int multi_recursive_optimized(int num1, int num2);
int multi_octal_numbers(int num1, int num2);
int multi_hex_numbers(int num1, int num2);
int multi_exponentiation(int base, int exponent);
int multi_by_euler_prime(int num);
int fibonacci_multi(int num, int fib);
int repeated_division_multi(int num1, int num2);
int bernoulli_multi(int num, int bernoulli);
float power_multi(float base, int exponent);
int bitwisee_multi(int num1, int num2);
int karatsu_multi(int num1, int num2);

Loading…
Cancel
Save