Quantcast
Channel: Implement division using only addition - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 18

Answer by ST3 for Implement division using only addition

$
0
0

My solution is C/C++ code and it makes many additions (200402), but anyway...

#include <iostream>int total = 0;int sum(int a, int b){++total;    return a + b;}int divide(int a, int b){    int x = 1;    if (a < b)        return 0;    else        return sum(x, divide(sum(a, -b), b));}int main(){    for (int i = 1; i <= 200; ++i)        for (int j = 1; j <= 200; ++j)        {            if (divide(i, j) != (i / j))                std::cout << "Failure: a="<< i << " b="<< j << "\n";        }    std::cout << "Total additions: "<< total << std::endl;    system("pause");}

And the output is:

Total additions: 200402Press any key to continue . . .

Viewing all articles
Browse latest Browse all 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>