Jump to content

[SHARE]compute factorial using recursion


Recommended Posts

Setting the vi to reentrant mode, vi can call itself .computing factorial of n using a recursion.

Lucky!

5180[/snapback]

Luckily, there is no need for that in LV. You could simply use a for loop with a couple of shift registers to perform a factorial (or use the supplied factorial VI - Analyze>Mathematics>Special and numeric functions).

Link to comment
Setting the vi to reentrant mode, vi can call itself .computing factorial of n using a recursion.

Lucky!

5180[/snapback]

While this type of recursion is a nice thing to show that it can be done, it has some serious drawbacks. Invoking reentrant VIs through VI server recursively is a very time and memory intense operation. Such a recursive algorithme is ALWAYS much slower than one which does the recursion through a stack based algorithme. As long as the recursion is only a few levels deep this probably won't make a to big difference but for deep recursion it is definitely a bad choice.

There are several threads about factorial VIs on NIs Developer Exchange LabVIEW forum. Some show this example but there used to be a challange at some point which showed the other aproach which can calculate !3000 without taking 100ds of MBs and half of the life of the universe to get the result. ;-)

Rolf Kalbermatter

Link to comment
Some show this example but there used to be a challange at some point which showed the other aproach which can calculate !3000 without taking 100ds of MBs and half of the life of the universe to get the result. ;-)

5184[/snapback]

This 3000 factorial business sounded interesting so I tried it using array-based math to see how bad of a calculation it would be... it turned out not too bad, only 3 minutes on a 1.6GHz PC and another 45 seconds to convert it into text.

The attached FP/BD screenshot shows my results... I used the VI that is blank as a generic base converter for the binary U32 array once the factorial was finished... the number of characters on top determine what base the text is so the binary array can be converted to decimal (as shown) or hex (base16) if the letters A-F were added to the character set.

If anyone's interested in doing array-based math let me know--it's very simple and can handle incredibly large numbers.

post-360-1120233743.jpg?width=400

Link to comment
There are several threads about factorial VIs on NIs Developer Exchange LabVIEW forum. Some show this example but there used to be a challange at some point which showed the other aproach which can calculate !3000 without taking 100ds of MBs and half of the life of the universe to get the result. ;-)

Rolf Kalbermatter

5184[/snapback]

There was a Labview coding challenge some time ago and the guys there really showed how fast one can do math with big integers in LV. I also took part in the competition and thought that my calculation of 10000! within 95 msec was fast. Three others were still faster, though, and the winning solution by Bruce Ammons calculated 10000! in 57 msec !

You can download his solution from http://www.ni.com/devzone/lvzone/codechallenge6_results.htm

-Franz

Link to comment

Thank you all.

I just to prove this probability.

My real problem is that I want to design a vi to simulate a menu(a single level), but if the menu has a multi level, i must call this vi more times.

However, Labview permit only a instance of vi at one time, if I set the vi as a reentrant, it can't show FP and doesn't respose to any EVENT.

Someone say I could duplicate the vi as many as the level of menu,,which may be take more memory.

Anybody has a better solution?

Link to comment
Thank you all.

I just to prove this probability.

My real problem is that I want to design a vi to simulate a menu(a single level), but if the menu has a multi level, i must call this vi more times.

However, Labview permit only a instance of vi at one time, if I set the vi as a reentrant, it can't show FP and doesn't respose to any EVENT.

Someone say I could duplicate the vi as many as the level of menu,,which may be take more memory.

Anybody has a better solution?

5197[/snapback]

If you save a VI as a template (with the extension .vit) you can create at runtime copies of it using VI Server methods. Basically you use 'Open VI Reference' to instantiate a new VI as a copy of the template. Then you use 'Set Control Value' or 'Set Control Value[Variant]' methods to pass parameters to the newly instantiated VI. Then use 'Run VI' method to run the new VI and finally 'Get Control value' (or 'Get Control Value[Variant]') to retrieve the results. At the end use 'Close VI Reference' to remove the new VI from memory.

There is even a method to programmatically create a new VI completely from scratch and do similar as above. This works with unsupported LV functions, however. In the 'scripting' sub-forum of LAVA you'll find more about it. Or look at an implementation example under http://forums.lavausergroup.org/index.php?...findpost&p=1850

Finally, as a help for your real task of creating a menu hierarchy, look at the thread http://forums.lavausergroup.org/index.php?...&hl=right+click in the 'User Interface' sub-forum. The implementation of context-sensitive right mouse click pop-up menus is discussed there and you'll find lots of ideas.

-Franz

Link to comment
Finally, as a help for your real task of creating a menu hierarchy, look at the thread http://forums.lavausergroup.org/index.php?...&hl=right+click in the 'User Interface' sub-forum. The implementation of context-sensitive right mouse click pop-up menus is discussed there and you'll find lots of ideas.

-Franz

5198[/snapback]

That's an interesting post you linked to... I ran into the same problem, needing to recursively build a right-click context menu that had sub-menus as well. My solution was almost exactly like Michael's desired solution screenshots.... I basically just enter my menu in a string constant now and the VI's will figure out what level each item is and build the submenus. That saves having to manually code the right-click menus with the different sets of menu-building VI's.

Link to comment

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.

×
×
  • Create New...

Important Information

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