From Saturday, Nov 23rd 7:00 PM CST - Sunday, Nov 24th 7:45 AM CST, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to develop a program to perform factorials?

HI,

trying to gather knowledge as i have been given three weeks to learn labview(as best i can) for a job interview, and i am trying to perform a factorial calculation, and am very stuck.

I want to read the results to a spreadsheet file like:
1 1
2 2
3 6
4 24
5 120

Has anyone any guidance to help me, ive wrote so many different diagrams, it is just actually performing the factorial that i am stuck on?

Thanks any help would be greatly appreciated
Justin Pyfrom
0 Kudos
Message 1 of 14
(11,606 Views)
The most commong factorial algorithm uses recursion, which is a bit tricky in LV.
There is an example for a factorial algorithm on this site here.
There was also a coding challenge here with results.

___________________
Try to take over the world!
0 Kudos
Message 2 of 14
(11,596 Views)
Well, the math is very simple using shift registers, the LabVIEW code can probably fit on a postage stamp 🙂

The LabVIEW challenge that tst mentioned actually had much more difficult requirements: The input can go up to 10000 and all digits of the result needed to be exact. (In the case of n=10000, the result was a number with 35660 digits! An efficient LabVIEW program can calculate them all in under 60 milliseconds 🐵

If you do plain double precision calculations, you'll get a rounded (inexact) result for n>22 and INF for N>170. Still this might be all you need. One simple way to calculate factorials using simple DBL precision, would be similar to the code in the attached image. It is a very illustrative example to learn about shift registers. Please study it carefully, even watch it run with execution highlighting on. 🙂

Message Edited by altenbach on 03-14-2005 01:36 PM

Message 3 of 14
(11,584 Views)
Whoa, Altenbach, you're right. I didn't even put my thinking cap on. I just remembered the standard algorithm as taught in the basic classes regarding recursion, the fact that the example was available, and the fact that there was a coding challenge. I didn't stop to actually think about it or to check the results of my search. My problem is that my fingers are usually faster than my brain.

___________________
Try to take over the world!
0 Kudos
Message 4 of 14
(11,568 Views)
Here are two more methods:

If you have LabVIEW PDS or higher, there is a factorial function built-in. Look in the "Analyze...Math...Special and numeric functions" palette.

Since the product of an empty array is defined as 1, you could also use that to calculate factorials. Since it needs to allocate an array, it is less efficient, but for the sizes involved here it does not matter.

For your task you should modify my earlier example. If you auto-index out of the loop next to the upper shift register, you will obtain a list of all factorials up to n in one swoop. This should help you create your table output. 🙂
0 Kudos
Message 5 of 14
(11,544 Views)
This diagramme gives a wrong results U can try it for n=13 and Up,

a major diagramme that I had seen Had the same problème, So if Some one had a good diagramme I will be so happy to have a look on it

kapernikov_3@hotmail.com
0 Kudos
Message 6 of 14
(11,498 Views)
All three diagrams that I posted give the correct results within the ~15 decimal digit precision of DBL. Maybe you can tell us what results you are getting for n=13 and what kind of result you are expecting.

IF you are doing the calcualtion in I32, it will overflow at 13. This is a limit of the numeric representation. No surprises here. What do you expect? The recursive demo posted in this thread is done in I32 and thus is limited to inputs of 0-12. You can change it to DBL if you want. It is more a demo to show a recursive algorithm than to do any useful computation. It is way too slow anyway.

The algoritms from the factorial challenge can do inputs up to 10000, giving a result with 35660 decimal digits. 🙂
0 Kudos
Message 7 of 14
(11,485 Views)

Hello
Do you think you could explain in detail the last VI you copartied ?
Please

0 Kudos
Message 8 of 14
(2,593 Views)

@yessyjami wrote:

Hello
Do you think you could explain in detail the last VI you copartied ?
Please


What kind of detail do you need?  It's very self-explanatory.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 9 of 14
(2,586 Views)

Why in the index of the first for loop was an "and array elements" added?

0 Kudos
Message 10 of 14
(2,552 Views)