Skip to main content
New Participant
May 12, 2021
Question

How to write a code to get even numbers between 1 to 100?

  • May 12, 2021
  • 4 replies
  • 1 view

How to write a code to get even numbers between 1 to 100?

    4 replies

    stefanhelzle0001
    Brainy
    May 12, 2021

    reject( /* Drops number where fn!mod returns 1 */
      fn!mod(_,2), /* Returns 0 for even numbers */
      1 + enumerate(100) /* Create list 1 ... 100 */
    )

    richardm900458
    Inspiring
    May 12, 2021

    Best way I have ever seen, stefan

    my personal method:

    a!forEach(
        items: enumerate(100)+1,
        expression:{
            if(
             mod(fv!item,2),
             {},
             fv!item
            )
        }
    )
    


    requirements:
    if needed, check the mathematical meaning of modulus and how its working.
    the value 1 (as integer) -> can be interpreted as the boolean value "true"
    and the value 0 as false.

    stefanhelzle0001
    Brainy
    May 12, 2021

    As you know, using foreach to filter lists is not my prefered way.