Blog
Walkthrough - Solving maths puzzles with Power Apps
For learning purposes, it can be useful to see a practical example of how to carry out mathematical tasks with Power Apps. This post walks through a demonstration of how to answer a quiz question using Power FX functions that include ForAll, Sequence, and Mod.
The image beneath describes the question - what number can be fully divided into 95, 114, 228, 152, 209, and 38 without any remainder?
We can answer this type of question with almost all programming languages and in this post, I'll describe one technique to answer this question with Power Apps.
How to find a number that can divide fully into several input values
The high-level methodology of a technique to solve this problem is to build a sequence of divisors (for example, 1-2000), and to attempt to divide each divisor by all the required dividends (152,95,209,114,38,228).
To implement this technique, we would call the ForAll function to iterate over the sequence of divisors. From within each iteration, we can test whether the divisor is divisible by all required dividends. If so, we can record the divisor that works. The formula beneath shows the formula that provides the answer to the question.
With({targetDividends:RenameColumns([152,95,209,114,38,228],
"Value",
"Dividend"
)
},
ForAll(Sequence(2000),
If(Sum(
AddColumns(targetDividends,
"IsDivisible",
If(Mod(Dividend,Value)=0,1,0)
),
IsDivisible
)=6,
Collect(colResults, {Dividend:Value})
)
)
)
How the formula works
The ForAll function iterates over a sequence of values that range from 1-2000. We create this sequence by calling the Sequence function.
From within each iteration, we specify a formula that adds a column to the targetDividends table. The column we add is called "IsDivisible" and we set the value to the result of whether the dividend is divisible by the current sequence value in the iteration. We call the Mod function to determine if the dividend is fully divisible by the divider. The Mod function returns the remainder following the division of 2 input values.
We can then Sum the "IsDivisible" column and if the result is 6 (the target number of dividends), it means that the sequence value is divisible by all the dividends.
When this condition equals true, we call the Collect function to store the value in a collection called colResult.
Conclusion
- Categories:
- formulas
- Apps - Migrating OnStart formula to use App.StartScreen/ Fixing existing apps that implement deep linking
- Calculations - What mistakes can happen when we fail to use round brackets correctly in calculations?
- Formulas - Review of how to write formulas using natural language
- Formula - converting centimeters/meters to feet and inches, and vice versa
- Dates - 4 tips to make sure that dates display correctly in UK "dd mm yyyy" format
- Formulas - How to calculate the distance between 2 points
- SharePoint - How to Patch the 6 most complex data types
- Formulas - Generating Row Numbers - Part 2
- Data - How to access nested collections/tables
- Formulas - Show Running Totals
- Formulas - Generating Row Numbers