Created
March 9, 2015 23:48
-
-
Save dsoverby1986/3381378ba470a67b556c to your computer and use it in GitHub Desktop.
Prime Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace Trying_Something_New | |
{ | |
public partial class Form1 : Form | |
{ | |
List<int> _divisors = new List<int>(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
//lbDivisors.DataSource = _divisors; | |
} | |
private void btnCheckNumber_Click(object sender, EventArgs e) | |
{ | |
string stringNumber = tbNumber.Text; | |
if(lblResult.Text != "") | |
{ | |
MessageBox.Show("Already displaying results."); | |
} | |
else if(stringNumber == "1" || stringNumber == "0") | |
{ | |
MessageBox.Show("Value must be larger than 1."); | |
tbNumber.Clear(); | |
} | |
else if(tbNumber.Text == "") | |
{ | |
MessageBox.Show("Key-in an integer."); | |
} | |
else if(stringNumber.Contains(".")) | |
{ | |
MessageBox.Show("Value must be an integer."); | |
tbNumber.Clear(); | |
} | |
else | |
{ | |
int number = int.Parse(stringNumber); | |
int absNumber = Math.Abs(number); | |
_divisors.Clear(); | |
for (int i = 1; i <= absNumber; i++) | |
{ | |
int remainder; | |
int dividend = absNumber; | |
remainder = dividend % i; | |
if (remainder == 0) | |
{ | |
_divisors.Add(i); | |
} | |
} | |
if (_divisors.Count > 2) | |
{ | |
//List<int> range = _divisors.GetRange(1, (absNumber - 1)); | |
int last = _divisors[_divisors.Count - 2]; | |
_divisors.RemoveAt(0); | |
_divisors.Remove(absNumber); | |
lblResult.Text = "This number IS NOT prime."; | |
lblNumberOfDivisors.Text = _divisors.Count.ToString(); | |
lblLargest.Text = last.ToString(); | |
//lbDivisors.Items.Add(_divisors.ToString()); | |
lbDivisors.Items.Add | |
} | |
else if (_divisors.Count < 2) | |
{ | |
MessageBox.Show("Something isn't quite right here. Figure it out asshole."); | |
} | |
else | |
{ | |
lblResult.Text = "This number IS prime."; | |
} | |
} | |
} | |
private void tbNumber_MouseClick(object sender, MouseEventArgs e) | |
{ | |
lblResult.Text = ""; | |
tbNumber.Clear(); | |
lblNumberOfDivisors.Text = ""; | |
lblLargest.Text = ""; | |
lbDivisors.Items.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment