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.
Julia
# Julia code to print Hello Geeks
# print function
print("Hello Geeks!")
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
# iterative approach
for i in 1:num2
res += num1
end
return res
end
# Function call
ans = mult_fn(10, 11)
println(ans)
Output:
Recursive Code:
Julia
function mult_fn(num1, num2)
# Base Case
if num2 == 1
return num1
else
# recursive call
return num1 + mult_fn(num1, num2 - 1)
end
end
# Function call
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
# using built-in function
ans = factorial(5)
println(ans)
Output:
Iterative Code-
Julia
function fact_fn(num)
res = 1
# iterative approach
for i in 1:num
res *= i
end
return res
end
# Function call
ans = fact_fn(10)
println(ans)
Output -
Recursive Code-
Julia
function fact_fn(num)
# Base Case
if num == 1
return 1
else
# recursive call
return num*fact_fn(num - 1)
end
end
# Function call
ans = fact_fn(5)
println(ans)
Output-
Similar Reads
Vectors in Julia Vectors in Julia are a collection of elements just like other collections like Array, Sets, Dictionaries, etc. Vector are different from Sets because vectors are ordered collections of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Vectors are one-d
5 min read
Strings in Julia Strings in Julia are a set of characters or a single character that is enclosed within double quotes(" "). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets([ ]). Creating a String Strings in Julia can be c
6 min read
Sets in Julia Set in Julia is a collection of elements just like other collections like arrays, dictionaries, etc. Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered in Arrays. Also, Arrays and dictionaries may contain dupl
8 min read
Serialization in Julia Like other programming languages, Julia also provides support for Serialization and De-serialization. The process of conversion of an object into byte streams (IO buffers) for the purpose of storing it into memory, file, or database is termed as Serialization. It is performed to save the object stat
2 min read
Variables in Julia Variables are some names given to the memory location to which they are assigned. These memory locations are used to store values that can be accessed by using the name of the location, i.e. Variable. Unlike C and Java, variables in Julia need not to be written with a Datatype. Julia auto-assigns th
4 min read
Polynomial Regression in Julia In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modeled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the correspondin
3 min read