How to get the least common multiple and greatest common divisor.(Power Automate Desktop)
Introduction to how to get the least common multiple and greatest common divisor in Power Automate Desktop.

Overall view of Flow

Robin(for copy and paste)
It can be copied and pasted into Power Automate Desktop.
SET NumList TO [6, 9, 27]
Excel.LaunchExcel.LaunchUnderExistingProcess Visible: False Instance=> ExcelInstance
SET RowCounter TO 1
LOOP FOREACH CurrentItem IN NumList
Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: CurrentItem Column: 1 Row: RowCounter
Variables.IncreaseVariable Value: RowCounter IncrementValue: 1
END
Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: '=LCM(A1:A' + NumList.Count + ')' Column: 2 Row: 1
Excel.ReadFromExcel.ReadCell Instance: ExcelInstance StartColumn: 2 StartRow: 1 ReadAsText: False CellValue=> LCM
Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: '=GCD(A1:A' + NumList.Count + ')' Column: 2 Row: 2
Excel.ReadFromExcel.ReadCell Instance: ExcelInstance StartColumn: 2 StartRow: 2 ReadAsText: False CellValue=> GCD
Excel.CloseExcel.Close Instance: ExcelInstance
Flow creation steps
This Flow requires a lot of work using Power Automate Desktop alone, so an Excel sheet is utilized.

Prepare a List of numbers for which you wish to calculate the least common multiple and greatest common divisor.

Set up an Excel Launch Action and create an empty Excel book.

Parameter | Value |
---|---|
Launch Excel | with a blank document |
Make instance visible | OFF |
Create a RowCounter by placing a Set Variable Action and set the initial value to 1.

Loop through List variables with For each.
Specify the List variable as the Value to be iterated.

Set up a Write to Excel worksheet Action in For each.
Parameter | Value |
---|---|
Value to write | %CurrentItem% |
Write mode | On specified cell |
Column | 1 |
Row | %RowCounter% |

Set up an Increase variable in For each to count up RowCounter by 1.

Set up a Write to Excel worksheet Action outside of For each. The value to be written is the formula for the LCM function to get the least common multiple in Excel.
Parameter | Value |
---|---|
Value to write | %'=LCM(A1:A' + NumList.Count + ')'% |
Write mode | On specified cell |
Column | 2 |
Row | 1 |

Retrieving the result of this formula with Read from Excel worksheet Action will take the least common multiple into a variable.
Parameter | Value |
---|---|
Retrieve | The value of a single cell |
Start column | 2 |
Start row | 1 |
Variables produced | LCM |

Set up a Write to Excel worksheet Action outside of For each. The value to be written is the formula for the GCD function to get the greatest common divisor in Excel.
Parameter | Value |
---|---|
Value to write | %'=GCD(A1:A' + NumList.Count + ')'% |
Write mode | On specified cell |
Column | 2 |
Row | 2 |

Retrieving the result of this formula with Read from Excel worksheet Action will take the greatest common divisor into the variable.
Parameter | Value |
---|---|
Retrieve | The value of a single cell |
Start column | 2 |
Start row | 2 |
Variables produced | GCD |

Finally, close the book with Close Excel Action. There is essentially no need to save.

When this flow is executed, the Read from Excel worksheet Action The least common multiple and Greatest common divisor are stored in the Variables produced. As an example, the following results are obtained when running for %[6, 9, 27]%.

---
Discussion
New Comments
No comments yet. Be the first one!