Skip to content

Instantly share code, notes, and snippets.

@josephwegner
Created April 27, 2012 12:58
Show Gist options
  • Save josephwegner/2508992 to your computer and use it in GitHub Desktop.
Save josephwegner/2508992 to your computer and use it in GitHub Desktop.
Is Number Even
<?
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);
?>
@josephwegner
Copy link
Author

Not sure how to fix this. It's pretty efficient on numbers < 10, but it's pretty rough on big numbers.

@janjongboom
Copy link

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