Recursion in Julia
Last Updated :
22 Mar, 2022
Julia is a dynamic, high-level programming language with high performance and speed that is used to perform operations in scientific computing. It is great for computational complex problems. It is an open-source language so all source code is available easily online.
It is as easy to use as Python but it has much faster execution compared to R and Python. Julia is a general-purpose language and can be used for various tasks such as statistical computations, data analysis, web development, game development and more.
Ways to run Julia:
- Through a .jl file in an IDE
- Command by command in Julia REPL(Read Evaluate Print Loop)
As Julia is a lot similar to other mostly used languages syntactically, it is easier to code and learn Julia. Julia programs are to be saved in a file with .jl extension.
Simple Julia Program:
Simple program to print Hello Geeks, just type the following code after the start of the interpreter.
Before we see Recursion in Julia, we need to know what actually recursion is.
What is Recursion?
It is a process in which a function calls itself either directly or indirectly. It is a technique that helps us to solve complex problems easily with elegant code. Classical problems of recursion include the Towers of Hanoi.
The idea of recursion is to represent a problem in terms of smaller problems, and add some conditions to stop it at some base levels.
Mathematical Interpretation
An object possesses recursive behaviour if it can be defined by the following properties:
- Base Case- it is a terminating scenario that does not use recursion further to form an answer
- Recursive Step- steps that reduce following cases towards the base case
Julia – Recursion
Let us see how recursion works in Julia language with the help of examples.
Example 1:
In this example, we are finding the multiplication of two numbers. Say num1*num2. It is the same as adding num1 to itself repeated num2 times. For example – 5*4 is the same as 5+5+5+5. We will code this problem in both ways iterative and recursive.
Iterative Code:
Julia
function mult_fn(num1, num2)
res = 0
for i in 1 :num2
res + = num1
end
return res
end
ans = mult_fn( 10 , 11 )
println(ans)
|
Output:
Recursive Code:
Julia
function mult_fn(num1, num2)
if num2 = = 1
return num1
else
return num1 + mult_fn(num1, num2 - 1 )
end
end
ans = mult_fn( 10 , 11 )
println(ans)
|
Output:
Example 2:
In this example, we are finding the factorial of a number. n!=n*(n-1)*(n-2) . . . . *1. Let’s say the number is 5. 5!= 5*4*3*2*1=120. In Julia factorial can be found out in three ways – using the built-in factorial function, another way is the iterative way and the third one is the recursive way.
Using Built-In function-
Julia
ans = factorial( 5 )
println(ans)
|
Output:
Iterative Code-
Julia
function fact_fn(num)
res = 1
for i in 1 :num
res * = i
end
return res
end
ans = fact_fn( 10 )
println(ans)
|
Output –
Recursive Code-
Julia
function fact_fn(num)
if num = = 1
return 1
else
return num * fact_fn(num - 1 )
end
end
ans = fact_fn( 5 )
println(ans)
|
Output-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...