Example Code

The Collatz Conjecture (an example of recursion 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
It can sometimes be useful to recursively call a function in programming.

This is possible in LabVIEW, putting a VI on its own block diagram.


Description
Start with any natural number (positive integer).

If the number is even, divide by 2.

If the number is odd, multiply by 3 and add 1.

Repeat. Stop if you reach a value of 1.

The Collatz Conjecture states that any starting number will eventually end up at 1, but it remains unproven.

This VI will return the path length for any integer.


Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  1. Enter any number into the control
  2. Run vi.

Additional Information or References

VI Block Diagram

 Block Diagram.png

 

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

 

 

Ian S
Applications Engineer CLA
National Instruments UK&Ireland

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