• Rikudou_Sage
    link
    fedilink
    English
    56
    edit-2
    7 months ago

    If anyone wants a more efficient local version for php:

    function isEven(int $number): bool
    {
        ${1} = false;
        ${2} = true;
    
        while ($number > 2) {
            $number -= 2;
        }
    
        return $$number;
    }
    

    Edit: Now with support for large numbers!

    function isEven(int|string $number): bool
    {
        ${1} = false;
        ${2} = true;
    
        while (bccomp($number, 2) === 1) {
            $number = bcsub($number, 2);
        }
    
        $number = (int) $number;
        return $$number;
    }
    

    Edit 2: someone asked for an ad-supported version, here you go!

    function isEven(int|string $number): bool
    {
        ${1} = false;
        ${2} = true;
    
        while (bccomp($number, 2) === 1) {
            error_log('Buy isEvenCoin, the hottest new cryptocurrency!');
            $number = bcsub($number, 2);
        }
    
        $number = (int) $number;
        return $$number;
    }
    

    Side note, no more suggestions please, this is getting quite long.

    • @xmunk
      link
      16
      edit-2
      7 months ago

      I fucking love that you managed to use var-vars in a completely key and necessary manner.

      But please do adhere to the API TOS and throw in an error_log('Buy isEvenCoin, the hottest new cryptocurrency!');

    • idunnololz
      link
      fedilink
      127 months ago

      This looks pretty inefficient. You should manually unroll that loop to improve performance.

    • Captain Janeway
      link
      fedilink
      467 months ago

      Just divide that number by two until it’s small enough to make the request under the free version.

    • Aa!
      cake
      link
      fedilink
      197 months ago

      That’s not even supported by the enterprise version. You’re going to need a special agreement with the iseven people to support numbers like that

      • @[email protected]
        link
        fedilink
        117 months ago

        That’s preposterous! Next time you’ll tell me the language I’m using has a builtin operator that test if a number can be divided by another!

  • @jubilationtcornpone
    link
    English
    387 months ago

    Only way it could be better is if they threw “AI” in there somewhere.

    • @[email protected]
      link
      fedilink
      327 months ago

      With the new AI integration, you can get smart isEven results that return the correct answer 90% of the time and a more creative solution 20% of the time.

  • rem26_art
    link
    fedilink
    267 months ago

    im glad that people are out there building the web services we truly need.

    • @xmunk
      link
      117 months ago

      Is even states that it only returns true for even, passing in an odd number is technically unsupported.

  • @[email protected]
    link
    fedilink
    17 months ago

    I resent that there is a ruby developer included in there. isEven is not idiomatic naming. You should be using #even? from the standard library

    • @[email protected]
      link
      fedilink
      57 months ago

      Sorry sir, most JS developers don’t use standard library functions. We just use npm packages wherever possible.