Created
April 27, 2012 12:58
-
-
Save josephwegner/2508992 to your computer and use it in GitHub Desktop.
Is Number Even
This file contains 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 isEven($g, $isIt = false) { | |
$g--; | |
$isIt = !$isIt; | |
if($g <= 0) { | |
if($isIt === TRUE) { | |
echo "$ g is even!"; | |
return true; | |
} elseif($isIt === FALSE) { | |
echo "$ g is odd!"; | |
return false; | |
} | |
} elseif($g >= 1) { | |
return isEven($g, $isIt); //Recursion, because people will think I know what I'm doing. | |
} | |
} | |
isEven(5); | |
?> |
Very good approach. I'd suggest adding a seperate isEven function with $isOdd = false
by default. Just for enterprise sake.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure how to fix this. It's pretty efficient on numbers < 10, but it's pretty rough on big numbers.