Advertisement
Guest User

divaddition

a guest
Sep 26th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. //For "shiggles"
  2. //A program that performs division with only addition; no shifting, no subtraction, no lookup tables.
  3. //http://calltheplummer.wordpress.com
  4. #include <stdio.h>
  5.  
  6. static int addCounter;
  7.  
  8. int add(int a, int b)
  9. {
  10.     ++addCounter;
  11.     return a + b;
  12. }
  13.  
  14. int divide(int dividend, int divisor)
  15. {
  16.     int quotient = 0, accumulator = 0, quantity = divisor, counter = 1;
  17.    
  18.     while(42) {
  19.         if  (add(accumulator, quantity) > dividend) {
  20.             if  (counter == 1)
  21.                 break;
  22.            
  23.             counter = 1;
  24.             quantity = divisor;
  25.         }
  26.         else if  (add(accumulator, quantity) == dividend) {
  27.             quotient = add(quotient, counter);
  28.             break;
  29.         }
  30.         else {
  31.             quotient = add(quotient, counter);
  32.             accumulator = add(accumulator, quantity);
  33.             quantity = add(quantity, quantity);
  34.             counter = add(counter, counter);
  35.         }
  36.     }
  37.     return quotient;
  38. }
  39.  
  40. void main(char **stuff)
  41. {
  42.     int a = 5;
  43.     int b = 10;
  44.     int i = 0;
  45.     int q;
  46.    
  47.     for (i = 0; i < 150; ++i, a+=27) {
  48.         addCounter = 0;
  49.         q  = divide(a, b);
  50.         printf("%d / %d = %d | Additions : (%d)\n", a, b, q,  addCounter);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement