Example Code

Sieve of Eratosthenes in LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

Coding that exemplifies the implementation of the sieve of Eratosthenes algorithm in LabVIEW.

 

Description

The sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer. To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

1. Create a list of consecutive integers from 2 through "n": (2, 3, 4, ..., n).
2. Initially, let “p” equal 2, the smallest prime number.
3. Enumerate the multiples of “p” by counting to “n” from “2p” in increments of “p”, and mark them in the list (these will be 2p, 3p, 4p, ...; the p itself should not be marked).
4. Find the first number greater than “p” in the list that is not marked. If there was no such number, stop. Otherwise, let “p” now equal this new number (which is the next prime), and repeat from step 3.

 

When the algorithm terminates, the numbers remaining not marked in the list are all the primes below “n”.

 

Requirements

LabVIEW 2012 or compatible

 

Steps to Implement or Execute Code
1. Select a number to limit the search
2. Run the VI

 

Additional Information or References

 Snippet.png
**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.