All the answers I have seen so far are nice (and sometimes very creative), but have to be copied if someone wants to use it. In order to keep the Mendix community feeling alive, I created a published REST-service, accessible for the entire Mendix-community at https://fibonacci104-sandbox.mxapps.io/.
The only microflow anybody needs is therefore as simple as...:
Functionality in REST-service can also be seen on the homepage of the REST-service: https://fibonacci104-sandbox.mxapps.io/p/REST-service-microflow (because adding wide images in this post, doesn't really work)
Here is my microflow. I initialize the FibonnaciList with the first Item at 0.
And then i loop until the 10th number of the List.
Hope it’s a simplier and optimized microflow :)
We are using :
- a string to return the FibonacciList, init : ‘0’
- a integer to store the current Fibonacci number, init :0
- a integer to store the next Fibonacci number, init :1
- a temp variable to calculate the sum Fib+NextFib, init :0
We iterate until we get the 10th number of the Fibonacci List.
In each iteration, we are summing the Fib+NextFib into the temp, incrementing the NextFib with the temp variable and adding the Fib to the List.
At the end, we show a pop-up message with the Fibonacci List.
That’s it :)
FibonacciList: String :='0';
Fib: integer := 0;
NextFib: integer := 1;
Tmp : integer := 0;
while Fib!=34 Loop
Tmp := Fib+ NextFib;
Fib := NextFib;
NextFib := Tmp;
Less is more!
The logic is simple; “the next number is found by adding up the two numbers before”. The solution can be as elegant and simple as the logic by using recursion.
Initiate the sequence:
That’s all folk!
So I start the microflow with variables for:
- The sequence string to show as message as output
- A counter for the number of tries the loop will take
- A variable for the first number (default 0)
- A variable for the second number ( default 1)
- Then it sets the start of the sequence in the string variable i.e: 0,1 ($First + ‘, ‘ + $ Second)
- Then i create a outcome variable which does the $First + $ Second
- Then i add the $Outcome to the string sequence
- Then we check if the number of tries is 10 (could be anything)
- If not I change the $First variable to $Second, change the $Second variable to $Outcome and add the number of tries + 1
- Then run the loop again untill it reaches the 10th trie.
Outcome: ‘Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144’
You have all gone way overboard. This is the challenge:
“ The challenge is to create a single microflow, which would generate the Fibonacci sequence up to the 10th number in the series. “
So here is the only correct solution that does not do more than asked and does not waste processing power calculating an fixed outcome time an time again:
By the way: Wouldn’t it be nice to have a tool for sharing your microflow with other developers….
*Editted* On second thought I had some inspiration and have come up with another Fibonacci-microflow which I hope you all like. Check it out on https://mydemoversion8-sandbox.mxapps.io/p/Fibonacci under tab "Fibonacci to the 10th”
*Editted again* For persons that prefer to see a parameter as input and a generated Fibonacci-flow, all you need is this:
Used type long for the numbers so you can go up to 92 to get the Fibonacci-serie. You can test it at https://mydemoversion8-sandbox.mxapps.io/p/Fibonacci under tab “Fibonacci by parameter”.
I have created simple microflow to create a fibonacci series with detail explanation using annotation.
Hi Fellow Mendix makers
here is my answer – the solution can also be found at: https://makerofthemonthaug2020-sandbox.mxapps.io/index.html?profile=Responsive
my solution consists of 3 parts.
that’s all folks – see you at Mendix World 2020.
Here I have post microflow of fibonacci series and added annotations too
Here is my solution.
Here is my answer. You pass in how many results you want in your sequence in ‘n’. So if we pass in 10, we get ‘0, 1, 1, 2, 3, 5, 8, 13, 21, 34’. We shortcut if n is 0 or 1 as we don’t need to iterate to find the starting values.
We can call SUB_Fib like this...
So passing in 10 to SUB_Fib gives us
a. change the buffer value with the current value.
b. change the current value with the sum of currentvalue and precedingValue ($currentValue+$preceedingValue).
c. change the preceding value with the buffer value.
d. change the series as the follows
if $iterator = 9
e. incrementing the iterator.
My way to create Fabonacci series using Mircroflow:
OK, I am a bit late to the party, but here is my microflow:
Also, I made a page where I could specify the number of entries to generate. Here is a video of this page in action: https://youtu.be/XBTvPx7hEno
Here are the results showing the sequence
I’m quite new to Mendix, just in my third week, and having seen everyone elses solutions I thought I’d try something a bit different. I’ve used a list variable, based on a non-persistable entity, and a recursive loop to generate the values. This could be optimised by nesting a sub-microflow but that wasn’t part of the challenge.
Apologies for the weird shape of the flow but I’ve tried to compress it to make it more readable in a screen shot.
Here goes! See annotations for more information
Hi girls & guys,
I looked for a solution without hardcoding (besides the starting zero). I added a little flexibility to be able to change the desired number of numbers in the generated Fibonacci sequence, this by using an integer constant (duh). I couldn’t help to add in a small error handler to avoid negative numbers (you’ll see what I mean).
Enjoy the tropical weekend everybody :-)
In this comment, I cannot view the image as a single piece even I cannot resize the Image So I am splitting the Image as two If anyone has an idea of how to edit a wide image please reply in the comment I tried many ways compressing the Image but nothing worked.
In the above microflow, I created objects So I can able to display the series
the above Image is just continuation of the first Image
The above shows the Entity I have created to store the series
The final pic displays the output when I click on generate series
Recursive solution not overloading action stack because it is using the followupmicroflow module.
If you are only interested in the result string (0, 1, 1, 2, ...) this may be a bit too much. You can have it easier by only using a single object. If you want to have the full picture, you can use this one. It adds a list of fibonacci Numbers to to the fibonacho sequence object and maintains the solution string. The special thing about this solution is the usage of the Follow Up microflow module that allows you to build a recursive logic where each call is handled in a new transaction in a new context. With this, you can make sure, that your action stack never grows too big.
Since ‘normal’ calculation has already been done, and I was afraid someone else might snipe the Java action (well done, Javith Baig!), I decided to implement the recursive algorithm. This is of course horrible, if you want to display all the numbers, so most of the code is concerned with storing the calculated numbers so that the list can be displayed neatly.
The module contains this domain model and microflows:
How to calculate:
Storing the intermediate results:
And finally: transform the intermediate result to a string:
using 2 entities, first entities to keep the input (n) and the output to show the final Fibonacci (xn)
The Rule is xn = xn−1 + xn−2
Code self-explanatory (as always ;-) )
This is my solution, with one entity to store the output