Skip to main content
March 15, 2022
Solved

How do I get Prime numbers 1-100

  • March 15, 2022
  • 17 replies
  • 0 views

How do I get prime numbers 1-100 ?

    Best answer by stefanhelzle0001

    filter(
      rule!isPrimeNumber(_),
      enumerate(100)+1,
    )

    17 replies

    gopalk2865
    March 15, 2022

    Hi Shilpa, Create an exp rule to check prime number and then use below code. 

     

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

    stefanhelzle0001
    March 15, 2022

    filter(
      rule!isPrimeNumber(_),
      enumerate(100)+1,
    )

    gopalk2865
    March 15, 2022

    Clean..

    davel001150
    March 15, 2022

    My solution:

    Start with a few simple primes, such as local!primes:{2,3,5,7,13}

    forEach( items: enumerate(100) +1

    expression: if

    any(local!candidate = fv!item, /*any() function returns true if any of the elements in a boolean array are true*/

    forEach( items: local!primes)

    expression: mod(fv!candidate, fv!item) = 0 /*if a prime evenly divides your candidate number, it's not prime)

    true, /*true in this case means it's true that it's not prime*/

    false

    ), /*the for loop inside the any() function is just the first parameter of the if, if any are true, then it's not prime*/

    null, /*so if it's not prime, don't return it, or if none of the primes we picked out divide it, maybe it's prime, return it*/

    fv!item

    )

    Take the output of this and append it to local!primes to refine it.  You can keep adding more and more to get more and more primes.  It starts to get time consuming up into the 4 and 5 digit primes, but it will continue to work if you continue to add to the starting primes.

    You could also just go to wolfram alpha and copy and paste the answer into a constant.

    March 16, 2022

    ----------------

    gives all the prime numbers

    P_checkPrime

    a!localVariables(
    local!testWith:enumerate(tointeger(sqrt(ri!number)))+2,

    local!test:remove(local!testWith,local!testWith[length(local!testWith)-1]),

    where(a!forEach(
    items: local!test,
    expression:if(mod(ri!number,fv!item)=0,true,false())
    )
    )

    )

    ------------------------------------------------------

    P_getPrimenumbers

    a!forEach(
    items: enumerate(100)+3,
    expression:
    if(length(rule!P_checkPrime(fv!item))>0
    ,
    {},
    fv!item
    )

    )

    krishnap9567
    January 1, 2024

    a!localVariables(
    local!value: enumerate(ri!number) + 1,
    a!forEach(
    items: local!value,
    expression: if(
    length(
    wherecontains(
    0,
    tointeger(mod(fv!item, local!value))
    )
    ) = 2,
    fv!item & " is Prime Number",
    {}
    )
    )
    )

    harshitb6843
    January 16, 2024

    varunm4367
    December 28, 2025

    Hi Please check out this solution,

    As according to the definition of prime number that it should be divisible by itself creates the logic. Bellow is the code

    a!localVariables(
         local!listOfNumbers: enumerate(ri!number),
         a!forEach(
             local!listOfNumbers,
             {
                 a!localVariables(
                       local!currentItem: fv!item,
                       if(
                          count(
                          a!forEach(
                                local!listOfNumbers,
                                if(mod(local!currentItem, fv!item) = 0, 1, {})
                                )
                           ) > 2,
                           {},
                           fv!item
                         )
                    )
              }

          ) 

    )

    January 5, 2026

    I'd like to propose a very different, scalable, very Appian equivalent of prime number detection:

    1. Confirm that the business needs prime numbers 1-100 and assess the future risk that 100 is simply too low.
      1. If you presume that one day they will need number > 100 then you can likely solve all of their future use cases with another 20 minutes of effort.
      2. Recognize that if you miscalculate this risk, performance will significantly degrade very quickly
    2. Pre-load a sync'ed record with with your dataset of the first N prime numbers.
      1. See the attached sync expression JS_primes for a dynamic calculation where N = 13,848(testing numbers 1-150k); however this is not the only approach, and YMMV with this approach if the business truly needs a gargantuan set of prime numbers to choose from.
      2. The nightly sync is disabled - this data never changes after deployment anyways
      3. N is <= the maximum record allowance (we'll say 4 million rows for most of us). If you're sure you only want primes between 1-100, then N = 25. Update the record sync expression or data loading approach accordingly.
      4. The prime integer can be stored as an integer for record sync purposes since the 10,000,000th prime integer is 179,424,673 - thus we don't need to worry about integer overflow for now. If Appian increases the max record sync'ed rows beyond 50,000,000, then this could change.
      5. No non-prime number should exist in the sync'ed record
    3. Run a!queryRecordType() on the table with a!queryFilter(field: integer_int, operator:"<=", value: 100)

    Sync Expression:

    Optimized isPrime calculation:

    Since this exercise is usually brought up as a way to teach complexity of calculations / compute time vs tradeoffs of storage / memory: