Quang Dinh Luong

Quang Dinh Luong

  • NA
  • 76
  • 18.6k

My app search for Prime Numbers work incorrectly

Oct 7 2015 10:28 PM
Here is my source code: (Also app code attached)
 =============================================================================
namespace IsThisPrime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
checkInput();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.MaxLength = 19;
/* SET THE FORM FULL SCREEN
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
this.Location = new Point(0, 0);
this.Size = new Size(width, height);
*/
}
private void checkInput()
{
// Check Validation
string text1 = textBox1.Text;
long num1;
bool check = long.TryParse(text1, out num1);
if (check == false)
{
textBox2.Text = "Overloaded!";
}
else
{
mainContent();
}
}
private void mainContent()
{
// Processing part
long x = Int64.Parse(textBox1.Text);
int i = 0;
bool Flag = false;
for (i = 2; i <= x / 2; i++)
{
if ((i == x))
i = i + 1;
if ((x % i == 0))
{
Flag = true;
break;
}
}
if ((Flag == false))
{
// This num is prime number
textBox2.Text = "PRIME!";
MessageBox.Show("PRIME");
}
else
{
// This num is not prime number
textBox2.Text = i.ToString();
MessageBox.Show("NOT PRIME");
}
}
}
}
=============================================================================
 The problem is when I check 1000000016531 ( A prime number), it takes a long time caculating and show that 1000000016531  isn't a prime number because 1000000016531 % (-1) == 0 
It can't be like that because i starts from 2, so how can it be like this i = -1
Anyone knows the reason? I really appriciated if you read this :(
 
 
 

Attachment: isthisprime.rar

Answers (9)