Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //For "shiggles"
- //A program that performs division with only addition; no shifting, no subtraction, no lookup tables.
- //http://calltheplummer.wordpress.com
- #include <stdio.h>
- static int addCounter;
- int add(int a, int b)
- {
- ++addCounter;
- return a + b;
- }
- int divide(int dividend, int divisor)
- {
- int quotient = 0, accumulator = 0, quantity = divisor, counter = 1;
- while(42) {
- if (add(accumulator, quantity) > dividend) {
- if (counter == 1)
- break;
- counter = 1;
- quantity = divisor;
- }
- else if (add(accumulator, quantity) == dividend) {
- quotient = add(quotient, counter);
- break;
- }
- else {
- quotient = add(quotient, counter);
- accumulator = add(accumulator, quantity);
- quantity = add(quantity, quantity);
- counter = add(counter, counter);
- }
- }
- return quotient;
- }
- void main(char **stuff)
- {
- int a = 5;
- int b = 10;
- int i = 0;
- int q;
- for (i = 0; i < 150; ++i, a+=27) {
- addCounter = 0;
- q = divide(a, b);
- printf("%d / %d = %d | Additions : (%d)\n", a, b, q, addCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement