Created
November 16, 2013 05:40
-
-
Save iporsut/7496418 to your computer and use it in GitHub Desktop.
Friday Puzzle #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function oneMinus() { | |
return (~1 + 1); | |
} | |
function isNegative(x) { | |
return (x < 0); | |
} | |
function plus(x,y) { | |
return x+y; | |
} | |
function minus(x,y) { | |
return x + (~y + 1); | |
} | |
function multi(x,y) { | |
var result = 0; | |
var count = 1; | |
var tX = Math.abs(x); | |
var tY = Math.abs(y); | |
while (count <= tY) { | |
result = result + tX; | |
count = count + 1; | |
} | |
return (isNegative(x) != isNegative(y))? oneMinus() * result: result; | |
} | |
function div(x,y) { | |
var result = 0; | |
var tX = Math.abs(x); | |
var tY = Math.abs(y); | |
while (tX >= tY) { | |
result = result + 1; | |
tX = minus(tX,tY); | |
} | |
return (isNegative(x) != isNegative(y))? oneMinus() * result: result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment