generally executing something like a groovy script works well in CUBA. I actually did that in the app component cuba-component-runtime-diagnose. CUBA has a bean to run groovy scripts: the Scripting interface.
I used it in the app component here. You can also inject certain variables to the script so that they are available within the script to use (see here).
Besides this general information, I don’t really see this to be applicable with your examples. Here’s why:
In this example, what do you want to be dynamic here. With dynamic I mean that the calculation should be changeable at runtime like it is done with the groovy script in the access groups.
To me it seems that it is a very concrete calculation that will never change. Therefore it can perfectly be represented as an algorithim in the application as source code. Can you elaborate on what needs to be dynamic here?
Besides this, the problem “to calculate how many small boxes fit into a lager box” you describe is actually a NP-hard problem. This means, that you most likely will need a little bit more then just a formular to calculate it. You can read more about the Bin packing problem and at a related question on SO.
This as well something that can be modeled perfectly in the application code. Because you need to have some UI that will give the user the possibility to select a trigger this thing by entering age & gender and should return the rate.
For the second example what might be working is that you use the reports feature of CUBA. Since it has more or less the building blocks that you need for that. You can dynamically select the input entities / attributes (with the generic UI for the user to select concrete values). Then you have a scripting part which runs some groovy which would allow this kinds of calculation. And then the output could be something like a PDF with the result, or as CUBA recently added the feature to render a CUBA table to display the results.
This just came to my mind as it might be something that will conceptually fit. But only for the second example. For the first example there is no correlation what so ever.
Or are you talking about this examples itself as the things that should be dynamically added into your applications? In this case, this would not only require the calculation but also the UI, the workflow to by dynamic, right?
Perhaps you can clarify a little bit what you mean and how you think the solution should look like. Perhaps you do some UI mockups to understand the UI workflow etc.