Click here to Skip to main content
15,893,663 members
Home / Discussions / C#
   

C#

 
AnswerRe: deleting a file from local disk after putting in DB Pin
Giorgi Dalakishvili24-Jun-08 21:23
mentorGiorgi Dalakishvili24-Jun-08 21:23 
AnswerRe: deleting a file from local disk after putting in DB Pin
wasimsharp24-Jun-08 21:24
wasimsharp24-Jun-08 21:24 
GeneralRe: deleting a file from local disk after putting in DB Pin
laziale24-Jun-08 21:36
laziale24-Jun-08 21:36 
GeneralRe: deleting a file from local disk after putting in DB Pin
laziale24-Jun-08 21:56
laziale24-Jun-08 21:56 
GeneralRe: deleting a file from local disk after putting in DB Pin
wasimsharp24-Jun-08 22:30
wasimsharp24-Jun-08 22:30 
GeneralRe: deleting a file from local disk after putting in DB Pin
Giorgi Dalakishvili24-Jun-08 22:00
mentorGiorgi Dalakishvili24-Jun-08 22:00 
AnswerRe: deleting a file from local disk after putting in DB Pin
leppie24-Jun-08 21:57
leppie24-Jun-08 21:57 
Questiondistributed computing?? Pin
Camilo Sanchez24-Jun-08 20:25
Camilo Sanchez24-Jun-08 20:25 
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;
button1.Invoke(new ParameterizedThreadStart(delegate(object state)
{
button1.Text = (string)state;
}), "Start");
progressBar1.Invoke(new ThreadStart(delegate()
{
progressBar1.Visible = false;
}));
}

private bool working = false;
private void button1_Click(object sender, EventArgs e)
{
if (working) //lo tumbo
{
bgWorker.Abort();
((Control)sender).Text = "Start";
progressBar1.Visible = false;
}
else//lo prendo
{
((Control)sender).Text = "Stop";
progressBar1.Visible = true;
bgWorker = new Thread(threadMethod);
bgWorker.Start(new StateInfo { Size = Convert.ToInt32(numericUpDown1.Value), PanMagic=checkBox1.Checked});
}
working = !working;
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
class MagicSquare
{
public class MagicSquareEventArgs : EventArgs
{
public int[,] Matrix { get; set; }
}
public delegate void MagicSquareEventHandler(object sender, MagicSquareEventArgs e);

public event EventHandler FindStart;
public event EventHandler FindEnd;
public event MagicSquareEventHandler MagicSquareFound;

public bool PanMagic { get; private set; }
public int Size { get; private set; }
public const int BOTTOM = 1;
public readonly int TOP;
public int N
{
get
{
return Size * ((Size * Size) + 1) / 2;
}
}

public MagicSquare(int _size, bool _panMagic)
{
Size = _size;
TOP = Size * Size;
PanMagic = _panMagic;
}

private bool isPerfectSquareLine(int number)
{
//double sqrt = Math.Sqrt(number);
//return sqrt == (int)sqrt;
return N == number;
}
private bool isMagicSquare(int[,] square)
{
int MATRIX_SIZE = square.GetLength(0);
int tempSum = 0;
#region |
for (int i = 0; i < MATRIX_SIZE; i++)
{
tempSum = 0;
for (int j = 0; j < MATRIX_SIZE; j++)
tempSum += square[i, j];
if (!isPerfectSquareLine(tempSum))
return false;
}
#endregion
#region -
for (int i = 0; i < MATRIX_SIZE; i++)
{
tempSum = 0;
for (int j = 0; j < MATRIX_SIZE; j++)
tempSum += square[j, i];
if (!isPerfectSquareLine(tempSum))
return false;
}
#endregion

if (PanMagic)
{
#region \
tempSum = 0;
for (int i = 0; i < MATRIX_SIZE; i++)
tempSum += square[i, i];
if (!isPerfectSquareLine(tempSum))
return false;
#endregion
#region /
tempSum = 0;
for (int i = 0; i < MATRIX_SIZE; i++)
tempSum += square[i, MATRIX_SIZE - 1 - i];
if (!isPerfectSquareLine(tempSum))
return false;
#endregion
}
return true;
}
private int[,] blankMatrix()
{
int[,] result = new int[Size, Size];
for (int i = 0; i < Size; i++)
for (int j = 0; j < Size; j++)
result[i, j] = BOTTOM - 1;
return result;
}
private int[,] cloneMatrix(int[,] matrix)
{
int[,] result = new int[matrix.GetLength(0), matrix.GetLength(1)];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
result[i, j] = matrix[i, j];
return result;
}
private bool containsNumber(int n, int[,] matrix)
{
foreach (int i in matrix)
if (i == n) return true;
return false;
}
private bool isFull(int[,] matrix)
{
foreach (int i in matrix)
if (i < BOTTOM) return false;
return true;
}

private List<int[,]> result = null;
private int[,] matrix = null;
public void FindMagicSquares()
{
if (FindStart != null)
FindStart.Invoke(this, new EventArgs());

result = new List<int[,]>();
matrix = blankMatrix();

for (int i = 0; i < Size; i++)
for (int j = 0; j < Size; j++)
findMagicSquares(BOTTOM, i, j);
matrix = null;

if (FindEnd != null)
FindEnd.Invoke(this, new EventArgs());
}
private void findMagicSquares(int n, int i, int j)
{
matrix[i, j] = n++;
if (isFull(matrix))
{
if (isMagicSquare(matrix))
{
result.Add(cloneMatrix(matrix));
if (MagicSquareFound != null)
MagicSquareFound.Invoke(this, new MagicSquareEventArgs { Matrix = result.Last() });
}
}
else
{
if (!containsNumber(n, matrix))
{
for (int _i = 0; _i < Size; _i++)
for (int _j = 0; _j < Size; _j++)
if (matrix[_i, _j] < BOTTOM)
findMagicSquares(n, _i, _j);
}
}

matrix[i, j] = BOTTOM - 1;
}
public List<int[,]> Result { get { return result; } }
}
}
AnswerRe: distributed computing?? Pin
Marek Grzenkowicz24-Jun-08 22:49
Marek Grzenkowicz24-Jun-08 22:49 
GeneralRe: distributed computing?? Pin
Camilo Sanchez25-Jun-08 16:53
Camilo Sanchez25-Jun-08 16:53 
GeneralRe: distributed computing?? Pin
Marek Grzenkowicz26-Jun-08 1:07
Marek Grzenkowicz26-Jun-08 1:07 
QuestionUSP Implemenation Pin
sher_azam24-Jun-08 20:22
sher_azam24-Jun-08 20:22 
AnswerRe: USP Implemenation Pin
Ashfield24-Jun-08 21:14
Ashfield24-Jun-08 21:14 
QuestionCannot marshal 'return value': Pointers cannot reference marshaled structures? Pin
cristi_alonso24-Jun-08 20:18
cristi_alonso24-Jun-08 20:18 
AnswerRe: Cannot marshal 'return value': Pointers cannot reference marshaled structures? Pin
leppie24-Jun-08 22:42
leppie24-Jun-08 22:42 
Questionconnect network PC Pin
Rinki Mukheraji24-Jun-08 19:38
Rinki Mukheraji24-Jun-08 19:38 
AnswerRe: Duplicate Post : Cp Member Please Ignore Pin
Abhijit Jana24-Jun-08 21:43
professionalAbhijit Jana24-Jun-08 21:43 
Questionchecking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 17:23
Mike Bentzen24-Jun-08 17:23 
AnswerRe: checking an object is compatible with another object Pin
Karthik Kalyanasundaram24-Jun-08 18:22
Karthik Kalyanasundaram24-Jun-08 18:22 
AnswerRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 18:23
protectorChristian Graus24-Jun-08 18:23 
QuestionRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:04
Mike Bentzen24-Jun-08 19:04 
AnswerRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 19:07
protectorChristian Graus24-Jun-08 19:07 
GeneralRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:25
Mike Bentzen24-Jun-08 19:25 
GeneralRe: checking an object is compatible with another object Pin
Christian Graus24-Jun-08 19:31
protectorChristian Graus24-Jun-08 19:31 
GeneralRe: checking an object is compatible with another object Pin
Mike Bentzen24-Jun-08 19:46
Mike Bentzen24-Jun-08 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.