Friday, October 24, 2014

Magic Squares

Was just remembering my childhood days this diwali, during childhood which we used to have lots of fun with friends and used to play a lot.

Suddenly remembered that trick which we used to use to fool around with friends, and that's magic-squares.

Magic-Squares is just arrangement of numbers in the square shape where, sum of each of its rows and sum of each of its column and sum of each diagonal is equal to same number.

So now being a computer science enthusiastic, I thought ok.. let me tell my computer (which had g++ compiler) to solve magic-squares.

Here is the result of my computer did. It did all these below ones in less than a fraction of a second ;) :P

NOTE:
After the symbol => is the rowSum
After the symbol \/ is the columnSum
RTLB -- Sum of numbers across Right Top to Left Bottom Diagonal
LTRB -- Sum of numbers across Left Top to Right Bottom Diagonal



Well for those who are interested in having a look at the code, here it is for them.

This is our main function:
 int main()  
 {  
   int num = 0;  
   cout << "Enter any odd number: ";  
   cin >> num;  
   cout << endl << endl;  
   if(num % 2 == 0) {  
     cout << "Even number not supported !! " << endl;  
     return 1;  
   }  
   MagicSquare mSq(num);  
   mSq.computeMagicSquare();  
   return 0;  
 }  

This is the main part where the magic square is begin populated.
   void getRowAndColumn(int& row, int& col, const int counter)  
   {  
     if(counter == 1) { col = num / 2; return;} // Initial condition to insert 0  
     else if(row == 0 && (col == num / 2)) { row = num - 1; col++; return;}  
     else if(col+1 == num && row == 0) { row++; return;}  
     else if(col+1 == num) { col = 0; row--; return;}  
     else if(row == 0) { row = num -1; col++; return;}  
     else if((row!=0) && (col!=num-1) && magicSquare[row-1][col+1] != 0) { row++; return;}  
     else { row--; col++; return;}  
   }  
   void computeMagicSquare(void)  
   {  
   // Printing Dummy Magic Square  
   // Solution for magic square   
   // Step 1 : Placing 1 in the middle of the first row 1st row of the matrix  
     cout << "Printing matrix template:" << endl;  
     printMagicSquare();  
     int counter = 1;  
     int row = 0;  
     int col = 0;  
     while(counter <= num*num)  
     {  
       getRowAndColumn(row, col, counter);  
       // cout << "Row = " << row << "Col = " << col << "Counter = " << counter << endl;  
       if(row < 0 || row >= num || col < 0 || col >= num) cout << "Error in calculating Row/Column Row = " << row  
         << "Column = " << col << endl;  
       magicSquare[row][col] = counter++;  
     }  
     cout << "\n\nPrinting magicSquare: " << num << "*" << num << endl;  
     printMagicSquare();  
   }  
This is the printing portion of the magicSquares.
   void printMagicSquare(void)  
   {  
     int LTRBDiagSum = 0;  
     int LBRTDiagSum = 0;  
     int *columnSum = new int[num];  
     for(int row = 0; row < num; row++)  
     {  
       int rowSum = 0;  
       for(int col = 0; col < num; col++)  
       {  
         rowSum += magicSquare[row][col];  
         if(row == col) LTRBDiagSum += magicSquare[row][col];  
         if(row+col == num-1) LBRTDiagSum += magicSquare[row][col];  
         columnSum[col] += magicSquare[row][col];  
         cout << "\t" << magicSquare[row][col];  
       }  
       cout << " => " << rowSum << endl;  
     }  
     cout << "RTLB(" << LBRTDiagSum << ")";  
     for(int i = 0; i < num; i++)  
     {  
       cout << "\\/";  
       if(i != num-1) cout << "\t";  
     }  
     cout << "LTRB(" << LTRBDiagSum << ")";  
     cout << endl;  
     for(int i = 0; i < num; i++)  
     {  
       cout << "\t" << columnSum[i] ;  
     }  
     cout << endl;  
     delete[] columnSum;  
   }  
And finally our class looked like this
 class MagicSquare {  
   public:  
   MagicSquare(int n)  
   {  
     num = n;  
     magicSquare = new int* [num];  
     for(int i = 0; i < num; i++)  
     {  
       magicSquare[i] = new int[num];  
     }  
   }  
   ~MagicSquare()  
   {  
     for(int i = 0; i < num; i++)  
     {  
       delete[] magicSquare[i];  
     }  
     delete[] magicSquare;  
   }  
   void printMagicSquare(void);  
   void getRowAndColumn(int& row, int& col, const int counter);  
   void computeMagicSquare(void);  
   private:  
   int **magicSquare;  
   int num;  
 };  


Well for those is really interested to execute this source code at your end, you can get the code from here.

I sincerely thank this blog for helping me formatting above code snippets.

Yeah thats it about magic-squares. Lets see if time permits will try to enhance code for even numbers as well.

Sunday, October 19, 2014

Weekend trip from Bengaluru

Bengaluru--Sringeri--Agumbe--Udupi--Dharmastala--Kukke--Bengaluru

Traveling is amazing if you have a good company. This time it was with 2 of my cousins. We just thought bangalore is heating up very much this summer and want to beat the heat by hanging out in some nature's beauty where we can chill out.

So meet the crew :

Nagarjun(L) with Kiran(and thats myself;) )(R)
Nagarjun(L) with Shreyas(R)

Plan :

Initial plan was to leave bangalore on friday evening and then visit Sringeri early in the morning, witness the temples and holy places there, later go to Agumbe and enjoy nature's beauty, which happpend accordingly and the rest of the tour was dynamically planned.

We hired a auto-guy in agumbe who helped us roam around that place.(That place was real nature's beauty)

Meet this auto-guy. (Took his permission to take his photo)
And his auto with his phone number (Took his permission to take this photo as-well)
Here are some of the photos which we captured, Nagarjun did take a lot of photos which i am missing now while writing this post.

The view from the top of the hill was breath-taking.

This is the very ancient temple on the hill.

Looks small but very very deep well.

Amazing is'nt it!! Looks like one of those children slides


Nagarjun and Shreyas busy boating !! (This photo was taken while boating so the photo quality got messed-up;))


After witnessing the world famous sunset point in Agumbe, we headed towards Udupi which is 55 Kms form Agumbe, there are of plenty local mini buses which ply from nearby places to Udupi via Agumbe.

Udupi . We visited Sri Krishna Temple and Madhwa Charya Matha there.
We got chance to see elephant moving the chariot of Lord Sri Krishna and then after evening Aarthi at 8.30 pm we went inside the temple for darshnam, we had a very good darshan and it was very blissful experience to see Lord Sri Krishna of Udupi, At that time of darshan, i was remembering the Madhurastakam which has the line in that as "...Udupiya Sri Krishna ..." It was a very good experience. On the day we visited it was Ekadashi, so we missed the chance to have dinner in temple.

Then next day we woke up early in the morning by 4.00 am and again went to temple to see Subrabatha seva to Sri Lord Krishna and we have again had a very good darshnam and also got a chance to see Abhishekam being perform to Lord Krishna.

Later we headed towards Dharmastala. Saw very famous Sri Dharmasthala Manjunateshwara Temple there. We had to stand in queue for quite some time there as we went during Maha neiveidya timings 11 - 12 noon.

There are many buses with lots of frequency from Dharmastala to Kukke. Went to Sir Kukke Subramanya Swamy temple and it was very pleasant evening, really had a very peaceful time.

We had a return booking from Kukke to Bangalore in KSRTC Airawat we just got in to the bus and by the time we opened our eyes we were in Majestic, Bangalore
Back home :)

Dasara - Navratri Alankaram's (Temples in Yelahanka)

Dasara Times - Temple decorations in Yelahanka-Oldtown

Ardha-Nareshwar Alankaram (Incarnation of Shiva-Parvathi) [Vasavi Devi Temple]



PanchRangi Alankaram (Alankaram with colors) [Yelamma Devi Temple]



Saraswathi Alankaram [Vasavi Devi Temple]



Shiva-Parvathi in Pancharangi Alankaram (Alankaram with Colors) [Vasavi Devi Temple]



Shiv Ji (Har Har Mahadev) [Vasavi Devi Temple]



Lakshmi Narayana in Sindhur-Carpoor Alankaram [Vasavi Devi Temple]



Ganesh ji in Pancharangi Alankaram [Vasavi Devi Temple]



Swaamiye Sharam Aiyappa [Muneeshwara Swamy Temple]



Saraswathi Alankaram [Chowdeshwari Devi Temple]



Subramanya Swamy in Chandan Alankaram [Chowdeshwari Devi Temple]



Ganesh ji in Benne (Butter) Alankaram [Chowdeshwari Devi Temple]



Sri MahaVishnu [ Muneeshwara Swamy Temple]



Durga Devi Alankaram [Chowdeshwari Devi Temple]



Sri Parvathi Parameshwara in Chandan Alankaram [Vasavi Devi Temple]



Durga Devi Alankaram [Vasavi Devi Temple]



Hatti (Cotton) Alankaram [Vasavi Devi Temple]



Drakshi Godambhi (Resins and Cashew) Alankaram [Chowdeshwari Devi Temple]



Gayatri Devi Alankaram [Vasavi Devi Temple]



Lord Venkateshwara Alankaram [Chowdeshwari Devi Temple]



Vajranghi Alankaram [Sri Ganesh Temple]



Droupadi Devi Alankaram [Maheshwari Devi Temple]