Created
September 2, 2015 21:22
-
-
Save yoshikischmitz/2b4f4306c77cf270bfaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int multiply_like_an_egyptian(int first, int second) { | |
int n; | |
int o; | |
if(first < second) { | |
n = second; o = first; | |
} else { | |
n = first; o = second; | |
} | |
static int n_powers[32]; | |
static int o_powers[32]; | |
int a_size = 64; | |
int n_x = 1; | |
int o_x = o; | |
n_powers[a_size] = 1; | |
o_powers[a_size] = o; | |
int i = a_size - 1; | |
while(n_x < n){ | |
n_powers[i] = n_x + n_x; | |
o_powers[i] = o_x + o_x; | |
n_x += n_x; | |
o_x += o_x; | |
//printf("%d %d %d \n", i, n_powers[i], o_powers[i]); | |
i--; | |
} | |
int l = n - n_powers[i]; | |
int s = 0; | |
while(i <= a_size){ | |
int n_p = n_powers[i]; | |
int o_p = o_powers[i]; | |
//printf("%d %d %d %d\n", n_p, l, s, o_p); | |
if(n_p <= l){ | |
s += o_powers[i]; | |
l = l - n_p; | |
} | |
i++; | |
} | |
return s; | |
} | |
int main() | |
{ | |
int result = multiply_like_an_egyptian(5, 5); | |
printf("%d\n", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment