What is a factorial?

In short, a factorial is a function that multiplies a number by every number below it till 1.

1! 1
2! 2*1
3! 3*2*1
4! 4*3*2*1
5! 5*4*3*2*1

Goal of challenge: To create a macro that calculates factorials.

To create a Macro, we start with creating a normal workflow that calculates factorials.

Overview of the workflow:

Step 1: Using Generate Rows to work out the numbers that we need to perform multiplication on for each input number.

To do this, we will use Generate Row Tool.  We will create a new field called Row Count and set the condition that the new field will be less than or equal to the input number that we are calculating its factorials. The Loops Expression is Row Count + 1.

In simple English, this configuration means:

Create a new field for me called RowCount, starting with 1, then generate new row by adding 1 to the previous value of RowCount until the number in RowCount column equals to Number. Hence, for each new number in Number column, the process of generating rows will be repeated, starting from 1. The result will be as followed:

Now we have our numbers for each input number that we could do multiplication on to work out the factorial.

Step 2: Using Multi Row Formula tool to perform calculation of factorial.

This configuration implies:

Create a new field called New Field, with the type of Int64.

Num Rows: 1 => We are only looking at one row previous or after the current row.

The expression in simple English means: if the active row’s Number is different from the previous row’s Number, give me the number of column RowCount for the new column created (New Field), which will be 1 for every new number. Otherwise, multiply the current number in RowCount to the previous value in New Field.

The largest number in the column New Field for each distinct number is its factorial.

Step 3: Using Summarize tool to output the factorial of each number only

In simple English this configuration means: looking at the Number column, for each group of the same number, give me the maximum number of the New Field Column and rename it to Factorial. The result is as below:

There we go, we have created a successful workflow to work out factorial for a list of input numbers. Now it’s time to convert our workflow to macro.

Right click on the Input tool: Convert to Macro Input

Right click on the Browse tool: Conver to Macro Output

The result is as below:

Save the Macro and test it in the new workflow by copy the text input before and insert your new created Macro.

That’s it, challenge 74 completed!

Note: Factorial can become a very large number so the data type you choose would determine the largest factorial result that you can get. In my case, I used Int64 and the largest factorial I can get is 20!. The result is not accurate anymore for any number after 20.

Thanks for reading! See you next time 🙂

 

The Data School
Author: The Data School