Jump to content

Recommended Posts

Posted

Hello everyone,
I need to perform a factorial operation in GX Works 3.
I need to calculate the factorial of a value entered from the HMI.

I'm writing the program with FBD/LD.
If I subtract 1 from the entered value and keep multiplying, it takes a very long time.
And since the number of digits in the entered number is not fixed, this method doesn’t seem very logical.
I couldn’t find any ready-made block.

I thought FOR/NEXT might work, but I couldn't manage to use it.
Does anyone have any suggestions?
Thanks.


Posted

I have no experience with Mitsubishi, but can you do something similar to this?

Factorial:=HMI_Number;
FinalMultiplier:= HMI_Number-1;
FOR i:=1 TO FinalMultiplier BY 1 DO
        Factorial:=Factorial*i;
END_FOR;

 

HMI_Number is the number the user enters on the screen. Factorial is the factorial 🙂.

At the start of the sequence, set the Factorial to be the same as the HMI_Number, this way we don't have a 0 starting value. Final_Multiplier is the number before the HMI_Number. After that the FOR loop takes care of the rest.

For the number 5, if you were to multiply it on paper the way that code runs, it would look like this: 5*1*2*3*4. Which gives the same result as 5*4*3*2*1.

Posted
16 hours ago, Steve Bailey said:

It might be easier to set up an array of long integers and use the value from the HMI as the index.

i tried , but i couldn't do it

Posted
7 hours ago, BE said:

I have no experience with Mitsubishi, but can you do something similar to this?

Factorial:=HMI_Number;
FinalMultiplier:= HMI_Number-1;
FOR i:=1 TO FinalMultiplier BY 1 DO
        Factorial:=Factorial*i;
END_FOR;

 

HMI_Number is the number the user enters on the screen. Factorial is the factorial 🙂.

At the start of the sequence, set the Factorial to be the same as the HMI_Number, this way we don't have a 0 starting value. Final_Multiplier is the number before the HMI_Number. After that the FOR loop takes care of the rest.

For the number 5, if you were to multiply it on paper the way that code runs, it would look like this: 5*1*2*3*4. Which gives the same result as 5*4*3*2*1.

The logic is correct, but it doesnt seem that I can write this with FBD/LD.

Posted
1 hour ago, sczot said:

Hi, i hope i didn´t solve your homework 😄

facto.png

I'm 31 years old, is it too late to be a student? 😄 
After I finish my tasks, such nonsense keeps bothering me, and I end up obsessing over it...
I can sleep well tonight. The program is working great. Thank you!

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...