Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. distributed computing??

distributed computing??

Scheduled Pinned Locked Moved C#
csharpquestionlinqgraphicscode-review
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Camilo Sanchez
    wrote on last edited by
    #1

    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;

    M 1 Reply Last reply
    0
    • C Camilo Sanchez

      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;

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      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.

      C 1 Reply Last reply
      0
      • M Marek Grzenkowicz

        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.

        C Offline
        C Offline
        Camilo Sanchez
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • C Camilo Sanchez

          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

          M Offline
          M Offline
          Marek Grzenkowicz
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups