Jump to content
Sign in to follow this  
usermsg

Labview 7.1 factorial calculus!

Recommended Posts

Hello all,

We are relatively new users of version 7.1 of Labview, and we have been scratching our heads for days now (we are amateurs) about the best way to construct a vi that calculates the factorial of an integer, NOT using any existing vi's (for example Analyze->mathematics->Special and Numeric functions) but from scratch.

We appreciate any thorough help...

:thumbup:

Share this post


Link to post
Share on other sites

Hi:

This factorial calculation problem is one that NI uses to show the recursive VI calling feature of LabVIEW. Recursive VI calling method is considered an advanced LabVIEW topic involving use of VI server. I would say that if you are new to LabVIEW, you should not punch yourself over, and over, if you could not get a quick solution to this 'seeminlgy' simple problem.

... Well, enough said. Pasted below is sample code for factorial calculation. I have also attached the VI.

Anthony

Share this post


Link to post
Share on other sites

Although the factorial function is the classic example used to illustrate recursion, it does not have to be written that way. In fact, it is probably easier to understand (and far easier for a neophyte to write) a direct iterative routine to calculate factorial using LabVIEW.

The value for which factorial is to be calculated is the input value. The For Loop executes this many times, multiplying (loop index +1) by the results of the previous loop's iteration, which is stored on a shift register. The initial value of the shift register is one, to seed the calculation. We need to add one to the loop index because For Loops start at zero, not one. The code illustrated below took less than one minute to write; it took longer to write this description.

http://forums.lavag.org/index.php?act=attach&type=post&id=6070

For you experienced programmers out there: I know that it calculates one extra multiplication (1 x 1) during the first iteration of the loop. Mea culpa.

Share this post


Link to post
Share on other sites

Simpler is better, and Bob's code has the safety advantage that it cannot run away from you in an endless recursive call if someone makes a simple modification mistake, which can easily happen with self calling code.

That being said, the technique in the second post has it's uses later in your LabVIEW life.

Share this post


Link to post
Share on other sites

Hi:

For reference, here is a link to the winning solution for the LabVIEW factorial coding challenge. There is a link to download the code as well.

n! Coding Challenge

. . . Just in case you are after performance and speed.

Anthony

Share this post


Link to post
Share on other sites

And just note that the simple loop example will overrun the I32 limit after only a small number of iterations. With I64 (or U64) you should be better.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.