Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions C/KaratsubaAlgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int KaratsubaAlgorithm(char* str1, char* str2){
int i = 0, j = 0;
int num1 = 0, num2 = 0;
while (str1[i] != '\0')
{
num1 *= 2;
if (str1[i] == '0' || str1[i] == '1') num1 += str1[i] - '0';
else return -1;
i++;
}
while (str2[j] != '\0')
{
num2 *= 2;
if (str2[j] == '0' || str2[j] == '1') num2 += str2[j] - '0';
else return -1;
j++;
}
return num1 * num2;
}
3 changes: 2 additions & 1 deletion C/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

# KaratsubaAlgorithm
Karatsuba Algorithm takes two strings are function arguments. Converts them from binary string to a decimal value, and return product of the two numbers. If the numbers are invalid, or any character in the string is neither 1 nor 0, -1 is returned.