Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long long int divide(long long int dividend, long long int divisor)
- {
- int sign = 1;
- if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0))
- {
- sign = -1;
- }
- if(dividend < 0) dividend = -dividend;
- if(divisor < 0) divisor = -divisor;
- unsigned long long int d[64], p[64];
- int last_index = 0;
- p[0] = 1;
- d[0] = divisor;
- for(int i = 1; i < 64 && d[i-1] < dividend; i++)
- {
- d[i] = d[i-1] + d[i-1];
- p[i] = p[i-1] + p[i-1];
- last_index = i;
- }
- unsigned long long int answer = 0;
- for(int i =last_index; i >= 0; i--)
- {
- if(d[i] <= dividend)
- {
- dividend -= d[i];
- answer += p[i];
- }
- }
- if(sign == -1) return -answer;
- else return answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement