Sieve of Eratosthenes with ControlLogix


Introduction to the Sieve of Eratosthenes with ControlLogix

The Sieve of Eratosthenes with ControlLogix is a method to find the prime numbers within a given limit. I thought this would be a fun project to try out on the ControlLogix. We’ll give this a shot, and hope we don’t fault the processor! Understanding the logic is a good way to introduce yourself to commands in structured text.

This is a method to find prime numbers without taking time to try each combination, as doing so would be very slow. Basically, we start with the value of 2. After that, we just flag multiples of each prime number as NOT prime. We do this within the range we specify. We’ll have to create a few tags, and an array with enough bits to cover the range of numbers we wish to mark.

Obviously, as we approach the top of the range we specify, we will be locating primes exponentially faster.

Keep in mind that this project is just for fun and learning. I would not recommend doing this on any production equipment. Also, be careful of your array sizes. You don’t want to exceed the memory limitation of the processor.

Basically, I’m converting this logic over from a BASIC program I found on an Altair 8800 disk. I’m just converting this over to ControlLogix.

Create Your Tags

At this point, we’ll create some tags. I’m just going to create this in program tags. We’l have a FLAGS Array, which is 224 BOOL bits. The purpose of this array is to keep track integers we need to take a closer look at, and the ones we can ignore. At first, our program will set all of the bits in this array. After that we’ll mark off integers that we know we don’t need to look at.

The Tank array will store the Integers which are prime.

We use PRIME and K to rule out non-prime numbers in our logic. The COUNT tag will keep track of the total number of primes. It will also serve as an index for indirect addressing when we log our primes to the TANK tag. DONE should be set to 1 to start. When you are ready to perform the calculation, set DONE to 0. The calculation will run, and set the DONE bit when it’s finished. Consider this a warning! If your scan time to perform this logic exceeds your watchdog timer for the task, the processor will fault. Don’t try this on production equipment!

Write the Logic for the ControlLogix Sieve of Eratosthenes

At this point, we’ll write the logic. I simply converted an older BASIC program from the late 1970’s. If you create a new structured text routine, don’t forget to use the JSR statement to execute the routine.

At first, we check to see if the logic has already ran. If so, then there is no need to run it again. After that, we initialize the count and K variables to 0.

Next, we set all of the bits in the flags array. Later on, we’ll shut these bits off when we discover numbers that are not prime.

Our second for… do… loop moves to the next prime number in the flags array. Next, we find multiples of that prime number, and mark them off in our FLAGS list. Obviously, multiples of a prime number cannot be prime.

Finally, we set the DONE tag, and our logic is complete.

Test Your Work for the Sieve of Eratosthenes with ControlLogix

After you have assembled all edits, reset the DONE tag to zero. Your logic should execute. After it executes, your DONE tag should go back to 1. Open your TANK array, and you should see the prime numbers.

To better visualize how this works, check out this YouTube video.

For more information, check out the ControlLogix Category Page!

— Ricky Bryce

Leave a comment

Your email address will not be published. Required fields are marked *