Division without using division, multiplication, or modulus operators Announcing the arrival...
Are all CP/M-80 implementations binary compatible?
Array Dynamic resize in heap
Why do people think Winterfell crypts is the safest place for women, children and old people?
Is it acceptable to use working hours to read general interest books?
std::is_constructible on incomplete types
Co-worker works way more than he should
Multiple options vs single option UI
Determining the ideals of a quotient ring
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
Is Electric Central Heating worth it if using Solar Panels?
What to do with someone that cheated their way though university and a PhD program?
Why does the Cisco show run command not show the full version, while the show version command does?
The art of proof summarizing. Are there known rules, or is it a purely common sense matter?
c++ diamond problem - How to call base method only once
Is it OK if I do not take the receipt in Germany?
Raising a bilingual kid. When should we introduce the majority language?
Is there a noticeable difference in sound quality between a mechanical pipe organ and an electronic one?
What is a 'Key' in computer science?
Is there any hidden 'W' sound after 'comment' in : Comment est-elle?
Do I need to protect SFP ports and optics from dust/contaminants? If so, how?
Expansion//Explosion and Siren Stormtamer
Multiple fireplaces in an apartment building?
Change doc string summary of a function on the fly
How to not starve gigantic beasts
Division without using division, multiplication, or modulus operators
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Base-36 encoding of a byte arrayGCD using Euclid algorithm“Angry Professor” Python implementationSaturated signed additionComputing the divisor sum in bulk without division, multiplication or factorisation (SPOJ DIVSUM)Sum of Square NumbersLeetcode: divide without multiplication, division, and mod operatorChange Arithmetic Right Shift to Logical Right ShiftFind the greatest common divisor of n numbersImplement integer exponentiation faster than repeated multiplication
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
The task
Implement division of two positive integers without using the
division, multiplication, or modulus operators. Return the quotient as
an integer, ignoring the remainder.
My solution
const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;
let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);
if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}
quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}
javascript algorithm programming-challenge ecmascript-6 bitwise
$endgroup$
add a comment |
$begingroup$
The task
Implement division of two positive integers without using the
division, multiplication, or modulus operators. Return the quotient as
an integer, ignoring the remainder.
My solution
const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;
let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);
if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}
quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}
javascript algorithm programming-challenge ecmascript-6 bitwise
$endgroup$
1
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.division(-1,1)
returns-2,
-2 / -1` returns3
and worst any value divide 0 does not return at all.
$endgroup$
– Blindman67
Apr 18 at 23:50
1
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56
add a comment |
$begingroup$
The task
Implement division of two positive integers without using the
division, multiplication, or modulus operators. Return the quotient as
an integer, ignoring the remainder.
My solution
const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;
let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);
if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}
quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}
javascript algorithm programming-challenge ecmascript-6 bitwise
$endgroup$
The task
Implement division of two positive integers without using the
division, multiplication, or modulus operators. Return the quotient as
an integer, ignoring the remainder.
My solution
const division = (dividend, divisor) => {
let remainder = null;
let quotient = 1;
const sign = ((dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0)) ?
~1 : 1;
let tempdividend = Math.abs(dividend);
let tempdivisor = Math.abs(divisor);
if (tempdivisor === tempdividend) {
remainder = 0;
return sign;
} else if (tempdividend < tempdivisor) {
remainder = dividend < 0 ?
sign < 0 ? ~tempdividend : tempdividend :
tempdividend;
return 0;
}
while (tempdivisor << 1 <= tempdividend) {
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}
quotient = dividend < 0 ?
(sign < 0 ? ~quotient : quotient) + division(~(tempdividend-tempdivisor), divisor) :
(sign < 0 ? ~quotient : quotient) + division(tempdividend-tempdivisor, divisor);
return quotient;
}
javascript algorithm programming-challenge ecmascript-6 bitwise
javascript algorithm programming-challenge ecmascript-6 bitwise
edited Apr 18 at 18:00
thadeuszlay
asked Apr 18 at 16:57
thadeuszlaythadeuszlay
1,021616
1,021616
1
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.division(-1,1)
returns-2,
-2 / -1` returns3
and worst any value divide 0 does not return at all.
$endgroup$
– Blindman67
Apr 18 at 23:50
1
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56
add a comment |
1
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.division(-1,1)
returns-2,
-2 / -1` returns3
and worst any value divide 0 does not return at all.
$endgroup$
– Blindman67
Apr 18 at 23:50
1
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56
1
1
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.
division(-1,1)
returns -2,
-2 / -1` returns 3
and worst any value divide 0 does not return at all.$endgroup$
– Blindman67
Apr 18 at 23:50
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.
division(-1,1)
returns -2,
-2 / -1` returns 3
and worst any value divide 0 does not return at all.$endgroup$
– Blindman67
Apr 18 at 23:50
1
1
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
New contributor
$endgroup$
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
$begingroup$
Also first if statement should be<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.
$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217683%2fdivision-without-using-division-multiplication-or-modulus-operators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
New contributor
$endgroup$
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
$begingroup$
Also first if statement should be<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.
$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
|
show 1 more comment
$begingroup$
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
New contributor
$endgroup$
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
$begingroup$
Also first if statement should be<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.
$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
|
show 1 more comment
$begingroup$
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
New contributor
$endgroup$
Is bitwise operation mandatory? There's a much simpler way
const divide = (dividend, divisor) => {
let quotient = 0, neg = false;
if( (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0) ){ neg = true; }
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
if(dividend < divisor) {return 0;}
else if(dividend > 0 && divisor != 0){
while(dividend >= divisor){
dividend -= divisor;
++quotient;
}
} else { // handle what you want to do for those cases..}
return neg ? -quotient : quotient;
}
You get your quotient and remainder is ignored. Just do a check if dividend is negative or divisor is 0.
New contributor
edited Apr 19 at 4:14
New contributor
answered Apr 18 at 19:05
Dblaze47Dblaze47
1564
1564
New contributor
New contributor
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
$begingroup$
Also first if statement should be<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.
$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
|
show 1 more comment
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
$begingroup$
Also first if statement should be<
not>
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if itsdivide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.
$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
1
1
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
$begingroup$
Welcome to Code Review! Better check dividend sign before comparing to divisor.
$endgroup$
– greybeard
Apr 18 at 20:01
1
1
$begingroup$
Also first if statement should be
<
not >
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if its divide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
Also first if statement should be
<
not >
and ... It may be the simple way, but divide(2**32-1, 1) may take some time to complete (On average device 11 seconds), even longer if its divide(Number.MAX_SAFE_INTEGER, -1)
(about 22 years on average device) There is a point where complexity provides practical solutions.$endgroup$
– Blindman67
Apr 18 at 23:49
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@greybeard I have fixed my answer to accommodate the negative cases. Thanks for the suggestion.
$endgroup$
– Dblaze47
Apr 19 at 4:15
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Blindman67 I have fixed the comparison. As for the division case, your case is a valid example that I have taken into account before posting the answer. However, there is no one solution to solve them all, and I thought if the OP was handling smaller cases (possible range was not mentioned), and has already a solution to handle complex cases, why not a simpler one then.
$endgroup$
– Dblaze47
Apr 19 at 4:23
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
$begingroup$
@Dblaze47 Oh my bad, my numbers were way wrong 32Bit int is ~7 seconds, and MAX_SAFE_INTEGER is about ~5 months. Anyways besides the point, "Why not a simpler one then." Why? This is code review, the focus is the review of OP's code, its style, its logic, and with that you can add example alternative/s demonstrating the points reviewed. The OP is after all looking for feedback on their code. If alternatives were the quest then google would be the better path, don't you think.
$endgroup$
– Blindman67
Apr 19 at 15:50
|
show 1 more comment
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217683%2fdivision-without-using-division-multiplication-or-modulus-operators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Your function does not work, very sloppy. I am amazed you found two arguments that would return the correct result.
division(-1,1)
returns-2,
-2 / -1` returns3
and worst any value divide 0 does not return at all.$endgroup$
– Blindman67
Apr 18 at 23:50
1
$begingroup$
It works for positive integers (as the task ask for). But you are right about the other stuff. Will fix it. @Blindman67
$endgroup$
– thadeuszlay
Apr 19 at 8:56