A demonstration of a an AWS Step Functions State Machine that can check whether a number is prime, and another state machine which can check a range o

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2024-12-26 17:30:14

A demonstration of a an AWS Step Functions State Machine that can check whether a number is prime, and another state machine which can check a range of numbers against that state machine.

Step Functions state machines cannot do much math - the only mathematical operation available is addition. We can however, use another method available - the States.ArrayPartition method which takes an array and a partition length and splits up the original array into an array of arrays of a length not exceeding the partition length. This actually reveals important information to us - the length of the final sub-array will be either the partition length (if the array length is divisible by the partition length) or the remainder of dividing the array length by the partition length.

Using this, we can calculate whether a number is prime using a trial division algorithm - (where x > 1) x is prime if, for 2 up to x/2 as y, x mod y != 0

Leave a Comment