In Haskell, we will find the the LCM of two given numbers by using recursion along with gcd and max function. In the first example, we are going to use (gcd) and (lcmOfTwoNumbers) function and in the second example, we are going to use (lcm) and (lcmOfTwoNumbers a b = lcm a b (max a b)) function. Algorithm Step 1 − The Prelude library is imported for hiding the gcd function. Step 2 − Define two user-defined functions - gcd and lcmOfTwoNumbers , Step 3 − Program execution will be started from main function. The main() function has whole ... Read More
In Haskell, we can find the given number is PRIME or not by using recursion along with helper function. In the first example, we are going to use (isPrime n | n Bool isPrime n | n Integer -> Bool isPrimeHelper n d | d > (n `div` 2) = True | n `mod` d == 0 = False | otherwise = isPrimeHelper n (d + 1) main :: IO () main = do let ... Read More
In Haskell, we can find reverse of a given number by using recursion along with helper function. In the first example, we are going to use (reverseNumHelper n rev) function and in the second example, we are going to use (reverseNum n | n < 10 = n| otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10)) function. Finding the reverse of a given number using recursion using the helper function In this method, the reverseNumHelper function takes two arguments: n and rev. n is the number to reverse and rev is ... Read More
In Go programming language, a hash collection contains a hashmap which stores the values as key:value pairs for the efficient execution of the programs. In this article we will use two examples to get the hash collection values as an array. In the first example, we will create a slice and append the strings of map in that slice, in the second example we will hash the string and store it inside the map. In this way we will print the array. Syntax func make ([] type, size, capacity) The make function in go language is used to create ... Read More
In Golang we have inbuild function like ok idiom to check whether a given key exist in hash collection or not. Hashmap is a collection of values paired with key in hashmap collection. In this article, we will create a hashmap using the built-in function then we will use ok idiom that returns true false value to check whether a key exists in the map or not. In this way a success or failure statement will be printed. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in ... Read More
In golang we can merge two hash collection by using map function. A hashmap is present in hash collections. It stores key:value pairs. We can perform various operations on a hashmap like merging them. In this article we will merge two hashmaps using a method. In that method, we will create an additional map to store the merged key:value pairs. Then, the map will be printed on the terminal using fmt package. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in formatting input and output. ... Read More
In Go programming language, a hash collection contains a hashmap which holds values in form of key:value pairs. Here, in this particular program we will convert the map into array which is of fixed size and can be accessed via indexing. We will use two examples to execute the program. In the first example, we will use an index variable to add the values in the array and in the second example we will use an append method to add the values in the array. Syntax func make ([] type, size, capacity) The make function in go language is ... Read More
In Golang, a Hashmap is a part of hash collection and it stores values in the form of key:value pairs. In this articlewe will store items in the hash collection using two different examples. In the very first example the indexing will be used to store items in the map and in the second example item struct will be used to store items. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. Algorithm ... Read More
In golang we can check whether a given value exist in hash collection or not by simply using okidiom function or we can even create an if-else user-defined function to do the same. Hashmap is basically a collection of values paired with their keys in hash collection. In this article we are going to look over two different example to understand how we can use the above two techniques to check the given value in hash collection. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in formatting ... Read More
In golang, we can print the inverted hash collection using the inverted mapping method. Hashmap stored the data in pair of key:Value in hash collection and that reduces the execution time. In this article we are going to see two different example to understand how to create a golang program that will print an inverted hash collection. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. func len(v Type) int The len() function ... Read More