usermsg Posted June 12, 2007 Report Share Posted June 12, 2007 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: Quote Link to comment
alukindo Posted June 13, 2007 Report Share Posted June 13, 2007 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 Quote Link to comment
BobHamburger Posted June 13, 2007 Report Share Posted June 13, 2007 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. Quote Link to comment
Mike Ashe Posted June 13, 2007 Report Share Posted June 13, 2007 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. Quote Link to comment
alukindo Posted June 13, 2007 Report Share Posted June 13, 2007 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 Quote Link to comment
Yair Posted June 13, 2007 Report Share Posted June 13, 2007 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. Quote Link to comment
usermsg Posted June 20, 2007 Author Report Share Posted June 20, 2007 Many thanks..we think that we are going to stick with Bob's code though :-) Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.