• Rikudou_Sage@lemmings.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    11 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@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      11 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!');

  • 4am@lemm.ee
    link
    fedilink
    arrow-up
    1
    ·
    11 months ago

    Incoming trademark lawsuit from iSeven, the API that tells you if a number is seven or not

    • fckreddit@lemmy.ml
      link
      fedilink
      arrow-up
      0
      ·
      11 months ago

      Easy workaround, just test the last digit. If it is even, the entire number is even, else odd.

      • marcos@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        11 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!

    • jol@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      11 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.