Total Pageviews

Sunday, November 6, 2016

Recursion and Recursive function

What is recursion!

Hi! Guys,how are you all? Today's post I'm gonna tell you what is recursion and also recursive function.So if you just completed your begginer level and starting to solve mid-level programming it might be useful and better understandable  for you.When I was new to the word recursion or recursive function I just google it.What I see in my search list I was surprised! See the image below

Is it a mistake! Nope,you will understand it after reading this post.
According to wikipedia  "Recursion is the process by which something is defined in terms of itself or its type"
Check the below images.

Now you may find some clue what I'm talking about! Recursion!
Better if I gave you some realistic example of recursion.Have you ever been a saloon or a parlor.I'm saying you this because they had mirrored room with opposite stand.So one mirror reflects to another mirror.If you look at any of this mirror you will visualize a wonderfull recursion.


Recursive Function

In terms of computer languages recursion means that a function can call itself.
Normally main function can call another function or a function but in terms of recursion a function call itself.

Before start explaining I would like to share with you something that is similar  to recursive function.Did you guys  watched 2010 released Christopher Nolan's movie inception? Where casted Leo DiCaprio as Dom Cob who is a thief can extract information from people mind using dreams.Cob and his associates architects dream into dream.
So how they came into reality! well, using kick.
Kick means something destructive happening in a dream or in their mind to wake up.
All I'm saying you to you this because recursive function has similar functionalities like inception.It can call itself like dreams in a dream,in dream, And what is the kick of recursive function? Well it has base case or halting case or you can say arguments which terminate the loop avoid becoming an infinite loop.Without base case your function will be a paradox recursion.

Again Function

Before starting coding let's do some math.Let's define function of a real number 
If the input of the function is x=5 the output will be 
Similarly we can find  any value of this function just putting the value of x=6,7,8,9....
Although the input of a function can't always be a number .It can be anything like imagine a function where we input a city name the output will be the country
ex.(Dhaka)=Bangladesh
     g(Paris)=France
That means for any function first of all we have to declare what will be the input, Like f's input was a number and g's input was a city and according to the input we have to describe what will be the output like it's a  number or a city.
We can describe it as a mathematical formula or may be writing a program or may be a algorithm.
A function can have many inputs.A function h is defined by two real numbers x and y
So folk if you're familiar with Pythagoras theorem you already known that x is the adjacent and y is the opposite of a right angle  where h represent the hypotenuse 
So guys if we want we can represent h using f
ex.

If you're smart enough you already known that 3 and 2 both equations are doing same things using 1 we find 
So we find
which is similar to the equation (2).In equation (3) the function h defined by function f.
So we are clear that to find one function value we can use another function.But the interesting thing a function can defined itself.This kind of function we called recursive function.
Look at the sequence below
0,1,1,1,3,8,13,21........
Is it familiar to you!Yup this is our favourite fibonacci sequence.Fibonacci was a mathematician who describe the series the next number is found by adding up the two numbers before it.
We find the third number of the Fibonacci sequence 0+1=1
and like all the Fibonacci number we find the 9th Fibonacci number is 21=8+31
so we can find the nth position Fibonacci number is
F(n)=F(n-1)+F(n-2)
If you're clever enough you will see we already made a recursive function.So here F(n) is a recursive function because it is defined by its own.
Lets print 5th position Fibonacci number using recursive function
  1.  
  2. #include <iostream>

  3. using namespace std;

  4. int fibonacci(int n)
  5. {
  6.     if(n==0)
  7.     {
  8.         return 0;
  9.     }
  10.     else if(n==1)
  11.     {
  12.         return 1;
  13.     }
  14.     else
  15.     {
  16.         return (fibonacci(n-1)+fibonacci(n-2));
  17.     }
  18. }
  19. int main ()
  20. {
  21.     cout<<fibonacci(5);
  22.     return 0;
  23. }

Output:5
If you're not clear how this recursive function work you can see the below recursion tree.



I will give you another example.Your task is find out how it work.
  1. #include <iostream>

  2. using namespace std;

  3. int fib(int m);

  4. int main()
  5. {
  6.     int i,n;
  7.     cout<<"Enter the value of n: ";
  8.     cin>>n;
  9.     for(i=1;i<=n;i++)
  10.     {
  11.         cout<<fib(i)<<" ";
  12.     }
  13. }
  14. int fib(int m)
  15. {
  16.     if(m<=2)
  17.     {
  18.     return 1;
  19.     }

  20.     else
  21.     {
  22.         return fib(m-1)+fib(m-2);
  23.     }
  24. }
Some programming languages have no loop ex. for loop,while loop, they use pure recursion.
Lisp,Eel,OCaml etc.Which don't have iterative or looping,So they represent iteration using recursive function.

Wednesday, October 26, 2016

Goooooo!With 'GO'

               Goooooo!With 'GO'

Did you hear about google's latest trending topic!Well its their latest ammo in their armour track!Sound destructive huuuuuh!Well all I'm talking about google's latest launched programming language "Go" (Don't get confused with their another AI project AlphaGo).Well! not so latest because 


So now Go has a version 1.7.3 a stable release is running from october 19,2016.Huuuh! not so latest right!


Before starting discussion about Go,I would like to do some chit chat about Ken Thompson.Though I didn't start using Go in my projects but I believe it will achieve a high dependencies and easy going approach to the programmer because of Ken Thompson.Ken Thompson is a God to a programmer.He jointly work with Dennis Ritchie for developing UNIX, created "B" a programming language.The direct predecessor to the C programming language.Belle (chess machine),UTF-8,Endgame tablebase and the latest Go!

   


 So this guy is the genious to make Go more compatible with the latest devices.


Why google develop Go!

Apple  has their very own programming language called  "swift",Microsoft have their "C#" known as C sharp.So why not google have one.Go is  designed to take advantage of modern multi-core processor without too much tinkering.This make Go run faster than any programming language.It also can meet the demand of networked systems,compute clusters and web applications.GO's built in concurrency gives an advantage to networking,distributed function.It can run Windows,Linux,Mac and even on a small devices like Raspberry pi.

Gooo!With Go!

OK! Here some demonstration  of Go's codeing.Let print " Hello world" program

package main
import "fm"
func main ()
{
   fmt.println("Hello,world")
}

NOw let's find sum and product of an array with Go programming

package main
import "fmt"
func main ()
{
     total,product:=0,1
for:_,y:=range [ ] int {1,2,5}
      total+=y
       product*=y
}
fmt.println(total,product)
}

 So the first impression for me it is not that much difficult to understand.Kinda look like C's type face.As for my personal opinion I feel Go is strong as python and safe and dependable like C/C++.It have another advantage and it is speed.It run faster than python vanila.You will be happy to know Go programms can talk to external C libraries or make native system call.

Is Java threaten by Go!

Well! Time  will say this but for now we can predict that Go can be rival to java enterprise.As far we see before launching Swift, IOS devloper using objective C for its devlopment  but still now they can use objective C though having Swift.So I guess it depends to the developer side if they are compfortable they will adopt it faster.
Faster adoption of this language make it furiously popular,Then it can be threaten to any language.
Good luck Go!

Companies which are already using Go!

 UK's startup bank Mondo  
  • BBc world wide
  • New york times
  • Booking.com
  • AeroFS 
  • Chango
  • Dropbox
  • Netflix
  • Novartis
  • Uber 
and more approximately 20 other company.

Projects which are already using Go!

  • Docker
  • Doozer
  • Ethereium
  • Juju
  • Snappy
  • Syncthing