Fibonacci¶
This tutorial looks towards creating an AR visualisation of the fibonacci sequence with arrays using ARgorithm. It is advised you go through the previous tutorial to understand this better.
Writing the code¶
The Fibonacci sequence is a very popular beginner’s problem to explain the concepts of recursion and dynamic programming. In this example, we will be implementing an iterative approach. The ARgorithmToolkit
class that we'll be using in this example is Variable
.
import ARgorithmToolkit
def run(**kwargs):
n = kwargs["n"]
algo = ARgorithmToolkit.StateSet()
if n <= 0:
algo.add_comment("n has to be natural number, try again")
return algo
var1 = ARgorithmToolkit.Variable("first",algo,1,comments="The first fibonacci number")
if n == 1:
algo.add_comment("1 is the first fibonacci number")
return algo
var2 = ARgorithmToolkit.Variable("second",algo,1,comments="The second fibonacci number")
for _ in range(2, n):
temp1 = var1.value
temp2 = var2.value
algo.add_comment(f"adding {temp1} and {temp2} and we get next fibonacci number {temp1+temp2}")
var1.value = temp2
var2.value = temp1+temp2
algo.add_comment(f"{var2.value} is the {n}th fibonacci number")
return algo
The Variable
class is a little different from the other classes and interfaces in ARgorithmToolkit. This class does not have any functionality other than to remember the states of objects of datatypes like int
,float
,string
,bool
. It is advised to use this only when you need a particular variable rendered in augmented reality that can prove helpful to understanding the code.
Warning
Be careful when reading or writing the value of the Variable
class.
>>> var = ARgorithmToolkit.Variable("var",algo,1)
>>> var
Variable(1)
>>> var.value = 3
>>> var
Variable(3)
>>> var = 3
>>> var
3
Variable
object will overwrite the object and states will no longer be recorded for it.
In the Fibonacci sequence, each number is the sum of its previous two Fibonacci numbers. We'll be using two variables to store the previously generated Fibonacci.
import ARgorithmToolkit
def run(**kwargs):
n = kwargs["n"]
algo = ARgorithmToolkit.StateSet()
if n <= 0:
algo.add_comment("n has to be natural number, try again")
return algo
var1 = ARgorithmToolkit.Variable("first",algo,1,comments="The first fibonacci number")
if n == 1:
algo.add_comment("1 is the first fibonacci number")
return algo
var2 = ARgorithmToolkit.Variable("second",algo,1,comments="The second fibonacci number")
for _ in range(2, n):
temp1 = var1.value
temp2 = var2.value
algo.add_comment(f"adding {temp1} and {temp2} and we get next fibonacci number {temp1+temp2}")
var1.value = temp2
var2.value = temp1+temp2
algo.add_comment(f"{var2.value} is the {n}th fibonacci number")
return algo
Setting up the config file¶
For this code, we only need one parameter that is n
which is the index of the number required in the sequence. We will provide the default value for n
as 4. As shown in previous tutorial, you can create this using your code editor or the configure
command (how to use configure)
{
"argorithmID": "fibonacci",
"file": "fibonacci.py",
"function": "run",
"parameters": {
"n" : {
"description" : "Enter the position of number in fibonacci series",
"type" : "INT",
"start" : 1
}
},
"example": {
"n" : 4
},
"description": "Print the nth fibonacci number"
Finally, we can submit the file to the server.
$ ARgorithm submit fibonacci
[SUCCESS]: FILES FOUND
[SUCCESS]: FILES VERIFIED
[SUCCESS]: SUBMITTED