Is there any rule to print all prime numbers within a range and range should pass through rule input ?

rule!Is primenumber{}

how to implement it correctly when I want to get it in a specific range ?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Use this, where lower and uppper are the limits of the range you want to work with.

    a!localVariables(
      local!lower: enumerate(ri!lower) + 1,
      local!upper: enumerate(ri!upper) + 1,
      local!value: remove(local!upper, wherecontains(local!lower, local!upper)),
      
    
      local!count: a!forEach(
        items: local!value,
        expression: sum(mod(fv!item, enumerate(fv!item) + 1) = 0) <= 2
      ),
     
      index(local!value, wherecontains(true, local!count))
      
    )

  • 0
    Certified Lead Developer
    in reply to David J (NTT DATA)

    just for the fun of it:

    a!localVariables(
      /*
      local!lower: enumerate(ri!lower) + 1,
      local!upper: enumerate(ri!upper) + 1,
      local!value: remove(local!upper, wherecontains(local!lower, local!upper)),
      (the above can just use "difference()" also, though it's really not necessary either way)
      */
      
      local!betterArray: enumerate(ri!upper - ri!lower) + (1 + ri!lower),
    
      local!count: a!forEach(
        items: local!betterArray,
        expression: sum(mod(fv!item, enumerate(fv!item) + 1) = 0) <= 2
      ),
    
      index(
        local!betterArray,
        /*wherecontains(true, local!count)*/
        where(local!count)
      )
    )

Reply
  • 0
    Certified Lead Developer
    in reply to David J (NTT DATA)

    just for the fun of it:

    a!localVariables(
      /*
      local!lower: enumerate(ri!lower) + 1,
      local!upper: enumerate(ri!upper) + 1,
      local!value: remove(local!upper, wherecontains(local!lower, local!upper)),
      (the above can just use "difference()" also, though it's really not necessary either way)
      */
      
      local!betterArray: enumerate(ri!upper - ri!lower) + (1 + ri!lower),
    
      local!count: a!forEach(
        items: local!betterArray,
        expression: sum(mod(fv!item, enumerate(fv!item) + 1) = 0) <= 2
      ),
    
      index(
        local!betterArray,
        /*wherecontains(true, local!count)*/
        where(local!count)
      )
    )

Children
No Data