Skip to content

Instantly share code, notes, and snippets.

@timshen91
Created March 26, 2013 05:24
Show Gist options
  • Save timshen91/5243347 to your computer and use it in GitHub Desktop.
Save timshen91/5243347 to your computer and use it in GitHub Desktop.
div without mul, div and mod
#include <cstdio>
int divImpl(int a, int b, int c, int * acc) {
if (a < b) {
return a;
}
int ret = divImpl(a, b + b, c + c, acc);
if (ret >= b) {
*acc += c;
ret -= b;
}
return ret;
}
int div(int a, int b) {
if (b == 0) {
return -1;
}
int acc = 0;
divImpl(a, b, 1, &acc);
return acc;
}
int main() {
printf("%d\n", div(20000000, 9));
return 0;
}
@LungZeno
Copy link

fork 了一個 loop 版的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment