Forum Discussion

MafazAnsar's avatar
MafazAnsar
Copper Contributor
Jun 23, 2022
Solved

LAMBDA Bug - wrong output in array form

I created a LAMBDA function using Name manager which included an IF function to the GCD between the two input variables (one in the row & other in the column) and only to perform a calculation if the GCD = 1, otherwise the formula should return 0 as the answer.

 

the function works fine when used in an ordinary set-up. However, when I use it in the array format it fails to perform the IF function and perform the remaining calculation regardless of the GCD of the two inputs.

 

the formula is =LAMBDA(O,E,IF(GCD(E,O)=1,E^2+O^2,0)) and named as TOE in the name manager.

 

 

the file can be accessed here

3 Replies

  • MafazAnsar 

    For the first sample

    it could be

    TOE=
    LAMBDA( O, E,
        LET(
            fn, LAMBDA(O,E,IF(GCD(E,O)=1,E^2+O^2,0)),
            MAKEARRAY( ROWS(O), COLUMNS(E),
                LAMBDA(r,c, fn(INDEX(O,r), INDEX(E, c) ))
            )
        )
    )
  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    MafazAnsar That's not a bug. When you use GCD on two arrays like in your example, the formula returns a single number 1. The the if_true argument is always executed producing the spilled array. 

     

    Should you apply that formula on an array of 4's and another of 2's, GCD will return 2 which is not equal to 1. Thus, the LAMBDA produces a single 0.

    You need to re-think and re-write the LAMBA. I haven't gotten that far.

    • MafazAnsar's avatar
      MafazAnsar
      Copper Contributor

      Riny_van_Eekelen thanks, In the workbook I have used odd numbers in the raws and even numbers in the columns. I tried changes everything to even numbers, basically, whichever the way it computes the GCD the answer should be at least 2, meaning the result should be 0. once I replaced all the odd numbers with even numbers, the formula output became 0 in the very first cell. not across the whole range. (screenshot below). I presume what you suggested was the case, may have to have a newer approach to writing the LAMBDA.

       

Resources