distributed computing??
-
Hi everyone, I'm supposed to make a program that finds all the magic squares of size 6, but there are 36! possible combinations of squares to check, so my programs runs forever, perhaps with a million servers I could actually find a solution, so here is my question: Anyone knows a way of running my C# application in multiple computers at the same time? Here is my code, perhaps anyone can improve it. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Linq; using System.Threading; namespace MagicSquare { public partial class Form1 : Form { Thread bgWorker = null; public Form1() { InitializeComponent(); } struct StateInfo { public int Size { get; set; } public bool PanMagic { get; set; } } private void threadMethod(object state) { MagicSquare msq = new MagicSquare(((StateInfo)state).Size, ((StateInfo)state).PanMagic); msq.FindEnd += new EventHandler(msq_FindEnd); msq.FindStart += new EventHandler(msq_FindStart); msq.MagicSquareFound += new MagicSquare.MagicSquareEventHandler(msq_MagicSquareFound); msq.FindMagicSquares(); } void msq_MagicSquareFound(object sender, MagicSquare.MagicSquareEventArgs e) { StringBuilder temp = new StringBuilder(textBox1.Text); temp.Append("---START---\r\n"); for (int i = 0; i < e.Matrix.GetLength(0); i++) { for (int j = 0; j < e.Matrix.GetLength(1); j++) temp.Append(string.Format("{0}{1}", e.Matrix[j, i], j + 1 == e.Matrix.GetLength(0) ? string.Empty : ", ")); temp.Append("\r\n"); } temp.Append("---END---\r\n"); textBox1.Invoke(new ParameterizedThreadStart(delegate(object state) { textBox1.Text = (string)state; }), temp.ToString()); } void msq_FindStart(object sender, EventArgs e) { working = true; textBox1.Invoke(new ParameterizedThreadStart(delegate(object state) { textBox1.Text = (string)state; }), string.Empty); } void msq_FindEnd(object sender, EventArgs e) { working = false;
-
Hi everyone, I'm supposed to make a program that finds all the magic squares of size 6, but there are 36! possible combinations of squares to check, so my programs runs forever, perhaps with a million servers I could actually find a solution, so here is my question: Anyone knows a way of running my C# application in multiple computers at the same time? Here is my code, perhaps anyone can improve it. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Linq; using System.Threading; namespace MagicSquare { public partial class Form1 : Form { Thread bgWorker = null; public Form1() { InitializeComponent(); } struct StateInfo { public int Size { get; set; } public bool PanMagic { get; set; } } private void threadMethod(object state) { MagicSquare msq = new MagicSquare(((StateInfo)state).Size, ((StateInfo)state).PanMagic); msq.FindEnd += new EventHandler(msq_FindEnd); msq.FindStart += new EventHandler(msq_FindStart); msq.MagicSquareFound += new MagicSquare.MagicSquareEventHandler(msq_MagicSquareFound); msq.FindMagicSquares(); } void msq_MagicSquareFound(object sender, MagicSquare.MagicSquareEventArgs e) { StringBuilder temp = new StringBuilder(textBox1.Text); temp.Append("---START---\r\n"); for (int i = 0; i < e.Matrix.GetLength(0); i++) { for (int j = 0; j < e.Matrix.GetLength(1); j++) temp.Append(string.Format("{0}{1}", e.Matrix[j, i], j + 1 == e.Matrix.GetLength(0) ? string.Empty : ", ")); temp.Append("\r\n"); } temp.Append("---END---\r\n"); textBox1.Invoke(new ParameterizedThreadStart(delegate(object state) { textBox1.Text = (string)state; }), temp.ToString()); } void msq_FindStart(object sender, EventArgs e) { working = true; textBox1.Invoke(new ParameterizedThreadStart(delegate(object state) { textBox1.Text = (string)state; }), string.Empty); } void msq_FindEnd(object sender, EventArgs e) { working = false;
You calculate the sums of all rows, columns and diagonals to check if a given array is a magic square, right? You don't have to do it - you can: (1) stop the calculations right after you get two different sums (two sums at minimum are needed to determine that a given array is not a magic square), (2) or even better, use the formula SUM = n*(n^2+1)/2 and calculate the sums only to the point when you get one that does not equal SUM (even one sum may prove that a given array is not a magic square). This should speed up things, although I don't know if this will be enough. Please use the code block tags when pasting code into your message. And consider pasting only the important pieces.
-
You calculate the sums of all rows, columns and diagonals to check if a given array is a magic square, right? You don't have to do it - you can: (1) stop the calculations right after you get two different sums (two sums at minimum are needed to determine that a given array is not a magic square), (2) or even better, use the formula SUM = n*(n^2+1)/2 and calculate the sums only to the point when you get one that does not equal SUM (even one sum may prove that a given array is not a magic square). This should speed up things, although I don't know if this will be enough. Please use the code block tags when pasting code into your message. And consider pasting only the important pieces.
hey man thanks for your answer. but I do check for n*(n^2+1)/2, this is the N property of my class, and the method isMagicSquare returns false if there is a single row or column wich sum is different from N. I made a multithreaded approach in a newer to solve the problem, but it still takes too long. but thanks anyway and sorry for the long code. I thought it will be smaller
-
hey man thanks for your answer. but I do check for n*(n^2+1)/2, this is the N property of my class, and the method isMagicSquare returns false if there is a single row or column wich sum is different from N. I made a multithreaded approach in a newer to solve the problem, but it still takes too long. but thanks anyway and sorry for the long code. I thought it will be smaller
Camilo Sánchez Herrera wrote:
this is the N property of my class, and the method isMagicSquare returns false if there is a single row or column wich sum is different from N
Sorry, I didn't notice that. I cannot understand how you generate the squares to check, but, if you're not already doing it, maybe you could check every generated row and only generate a next one if the sum of the previous one equals N. BTW, is it a school/university assignment?