From 4d439bfd46dc6ae17f6e65bd223659d7060ca96d Mon Sep 17 00:00:00 2001 From: archie94 Date: Tue, 28 Jun 2016 15:41:33 +0530 Subject: [PATCH] Implementation of Magic Square --- algorithm/category.json | 5 +-- algorithm/etc/magic_square/basic/code.js | 41 ++++++++++++++++++++++++ algorithm/etc/magic_square/basic/data.js | 14 ++++++++ algorithm/etc/magic_square/desc.json | 13 ++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 algorithm/etc/magic_square/basic/code.js create mode 100644 algorithm/etc/magic_square/basic/data.js create mode 100644 algorithm/etc/magic_square/desc.json diff --git a/algorithm/category.json b/algorithm/category.json index da82e73b..d1d6d3e0 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -105,8 +105,9 @@ "etc": { "list": { "flood_fill": "Flood Fill", - "cellular_automata": "Cellular Automata", - "create_maze": "Create Maze" + "cellular_automata": "Cellular Automata", + "create_maze": "Create Maze", + "magic_square": "Magic Square" }, "name": "Uncategorized" } diff --git a/algorithm/etc/magic_square/basic/code.js b/algorithm/etc/magic_square/basic/code.js new file mode 100644 index 00000000..2b9d2fb8 --- /dev/null +++ b/algorithm/etc/magic_square/basic/code.js @@ -0,0 +1,41 @@ +var i = Math.floor (n/2); +var j = n-1; + +for ( var num = 1; num <= n*n; ) { + logTracer._print ( 'i = ' + i ); + logTracer._print ( 'j = ' + j ); + + if( i == -1 && j == n ) { + j = n - 2; + i = 0; + + logTracer._print ( 'Changing : ' ); + logTracer._print ( 'i = ' + i ); + logTracer._print ( 'j = ' + j ); + } else { + if ( j == n ) { + j = 0; + logTracer._print ( 'Changing : ' + 'j = ' + j); + } + if ( i < 0 ) { + i = n-1; + logTracer._print ( 'Changing : ' + 'i = ' + i ); + } + } + + if ( A[i][j] > 0 ) { + logTracer._print ( ' Cell already filled : Changing ' + ' i = ' + i + ' j = ' + j ); + j -= 2; + i++; + continue; + } else { + A[i][j] = num++; + tracer._notify( i, j, A[i][j] )._wait (); + tracer._denotify ( i, j ); + tracer._select ( i, j )._wait (); + } + j++; + i--; +} + +logTracer._print ( 'Magic Constant is ' + n*(n*n+1)/2 ); \ No newline at end of file diff --git a/algorithm/etc/magic_square/basic/data.js b/algorithm/etc/magic_square/basic/data.js new file mode 100644 index 00000000..83887e48 --- /dev/null +++ b/algorithm/etc/magic_square/basic/data.js @@ -0,0 +1,14 @@ +var n = 7; +var A = new Array (n); +for (var i = n - 1; i >= 0; i--) { + A[i] = new Array (n); +} + +for ( var i = n -1; i >= 0; i-- ) { + for ( var j = n - 1; j >= 0; j-- ) { + A[i][j] = 0; + } +} + +var tracer = new Array2DTracer ('Magic Square')._setData(A); +var logTracer = new LogTracer ( 'Console' ); \ No newline at end of file diff --git a/algorithm/etc/magic_square/desc.json b/algorithm/etc/magic_square/desc.json new file mode 100644 index 00000000..789db7e3 --- /dev/null +++ b/algorithm/etc/magic_square/desc.json @@ -0,0 +1,13 @@ +{ + "Magic Square": "In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e., each number is used once), usually integers, in a square grid, where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number, called the magic constant. A magic square has the same number of rows as it has columns, and in conventional math notation, 'n' stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being of order n. A magic square that contains the integers from 1 to n2 is called a normal magic square. (The term magic square is also sometimes used to refer to any of various types of word squares.)", + "Complexity": { + "time": " O(N2)", + "space": "O(N2)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "basic": "Magic Square" + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy