Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tower of Hanoi #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions FabonacciList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;

// Recursive function to return Fibonacci List
int fibonacciList(int n)
{
// Checks if n<=1, i.e. 0 and 1
if (n <= 1)
{
return n;
}
else
{
// Returns the function recursively for the next number
return fibonacciList(n - 1) + fibonacciList(n - 2);
}
}

int main()
{

/*
Fibonacci List Algorithm Implementation in C++

Output : Return the the first 'n' elements of the Fibonacci Series
Time complexity : O(n*2^n)

Example:
n=10

Output:
0 1 1 2 3 5 8 13 21 34
*/

int n, m1 = 0, m2 = 1;
n = 10;
for (int i = 0; i < n; i++)
cout << fibonacciList(i) << " ";
cout << endl;
}
30 changes: 30 additions & 0 deletions Recursion, Backtracking etc./RecursiveSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Title - Recusive Sum
Description- A Program to recursively calculate sum of N natural numbers.
*/

#include<bits/stdc++.h>
using namespace std;

// A recursive function to calculate the sum of N natural numbers
int recursiveSum(int num)
{
if(num == 0)
return 0;
return (recursiveSum(num - 1) + num);
}

int main()
{
int num;
cout<<"\n Enter a number N to find sum of first N natural numbers: ";
cin>>num;
cout<<"\n Sum of first "<<num<<" natural numbers is = "<<recursiveSum(num);
return 0;
}

/*
Time complexity - O(n)
Sample Input - 5
Sample Output - 15
*/
35 changes: 35 additions & 0 deletions Recursion, Backtracking etc./Tower-of-Hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Problem-Tower of Hanoi
// Tower of hanoi is a problem in which we have 3 rods and n disks.
// The objective of the problem is to move the entire stack to another rod, by following the below rules:-
// 1)Only one disk can be moved at a time.
// 2)Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack
// i.e. a disk can only be moved if it is the uppermost disk on a stack.
// 3)No disk may be placed on top of a smaller disk


#include <bits/stdc++.h>
using namespace std;

void towerOfHanoi(int n, char source,char destination, char intermediate)
{
if (n == 1)
{
cout << "Move disk 1 from rod " << source <<" to rod " << destination<<endl;
return;
}
//Move disk from source to intermediate using destination rod
towerOfHanoi(n - 1, source, intermediate, destination);
cout << "Move disk " << n << " from rod " << source <<" to rod " << destination << endl;
//Finally Move disk from intermediate to destination using source rod
towerOfHanoi(n - 1, intermediate, destination, source);
}

// Driver code
int main()
{
cout<<"Enter number of disks"<<endl;
int n ;//Number of disks
cin>>n;
towerOfHanoi(n, 'S', 'I', 'D'); // S,I,D represents source ,intermediate and destination rods.
return 0;
}