This question already has answers here:
How can I test for primality?
(16 answers)
Closed 8 years ago.
I need to use a boolean variable to identify if a number inserted in a text box is a prime number and be written in C#
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = true;
for (int i = 0; i < TestNumber; i++)
{
while (TestNumber % i == 0)
{
bool isPrime = true;
yesNoPrimeTextBox.Text = "prime";
break;
}
while (TestNumber % i == 0)
{
bool isPrime = false;
yesNoPrimeTextBox.Text = "not prime";
break;
}
}
}
Extract IsPrime as a method and you'll have something like this:
public static Boolean IsPrime(int value) {
if (value <= 1)
return false;
else if (value <= 3) // 2 and 3 are primes
return true;
else if (value % 2 == 0) // even numbers (2 excluded) are not primes
return false;
// Test odd numbers 3, 5, 7, ... as potential dividers
// up to square root of the value
int n = (int) (Math.Sqrt(value) + 1);
for (int i = 3; i <= n; i += 2)
if (value % i == 0)
return false;
return true;
}
...
protected void isPrimeButton_Click(object sender, EventArgs e) {
int testNumber;
if (!int.TryParse(primeNumberTextBox.Text, out testNumber)) {
// primeNumberTextBox.Text is not a int (incorrect format)
//TODO: probably you have to put some text into yesNoPrimeTextBox.Text
return;
}
if (IsPrime(testNumber))
yesNoPrimeTextBox.Text = "prime";
else
yesNoPrimeTextBox.Text = "not prime";
}
Use this method to check :
Also consider changing the return type , maybe you want it to return boolean instead of string
public static string CheckPrime(int number)
{
bool isPrime = true;
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
return number.ToString() + " is a Prime number";
}
else
{
return number.ToString() + " is not a Prime number";
}
}
Not very efficient, but this should work:
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = true;
for (int i = 2; i < TestNumber; i++)
{
if (TestNumber % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
yesNoPrimeTextBox.Text = "prime";
else
yesNoPrimeTextBox.Text = "not prime";
}
This should do it.
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = false;
for (int i = 2; i < TestNumber-1; i++)
{
if (TestNumber % i == 0)
{
isPrime = true;
yesNoPrimeTextBox.Text = "prime";
break;
}
}
}
Related
I'm trying to give the letters in my richtextbox different colors for my subnet calculator, but the richtextbox doesn't change the colors until the 26th letter.
How it looks:
int iValueSm = trackBarSmMask.Value;
rtbScroll.Text = "";
rtbScroll.SelectionStart = rtbScroll.TextLength;
rtbScroll.SelectionLength = 0;
for (int i = 1; i <= iValueSm; i++)
{
rtbScroll.SelectionColor = Color.Blue;
rtbScroll.AppendText("N");
if (i%8==0 && i != 32)
{
rtbScroll.Text = rtbScroll.Text + ".";
}
}
for (int i = iValueSm+1; i <= 32; i++)
{
rtbScroll.SelectionColor = Color.Red;
rtbScroll.AppendText("H");
if (i % 8 == 0 && i != 32)
{
rtbScroll.Text = rtbScroll.Text + ".";
}
}
labelAmountNetID.Text = "/" + iValueSm.ToString();
Well, can be a lot of approaches to deal with this problem but here is one suggestion:
// Track bar definitions...
private void SetTrackBarVals()
{
trackBar1.Minimum = 0;
trackBar1.Maximum = 31;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
var counter = 0;
var dotsCounter = 0;
rtbScroll.Text = "";
int iValueSm = trackBar1.Value + 1; // +1 because we start counting from 0
for (int i = 1; i <= 32; i++)
{
if (counter > 0 && counter % 8 == 0)
{
// new octet
rtbScroll.AppendText(".");
dotsCounter++;
}
if (i > iValueSm)
{
// It is red
rtbScroll.AppendText("H");
rtbScroll.SelectionStart = (i - 1) + dotsCounter;
rtbScroll.SelectionLength = 1 ;
rtbScroll.SelectionColor = Color.Red;
}
else
{
rtbScroll.AppendText("N");
}
counter++;
}
}
Anytime you set the .Text() property, you RESET all formatting back to black and white.
Here is how I'd write it using SelectedText:
private void Form1_Load(object sender, EventArgs e)
{
updateRTB();
}
private void trackBarSmMask_ValueChanged(object sender, EventArgs e)
{
updateRTB();
}
private void trackBarSmMask_Scroll(object sender, EventArgs e)
{
updateRTB();
}
private void updateRTB()
{
rtbScroll.Text = "";
rtbScroll.SelectionStart = 0;
rtbScroll.SelectionLength = 0;
int iValueSm = trackBarSmMask.Value;
labelAmountNetID.Text = "/" + iValueSm.ToString();
for (int i = 1; i <= 32; i++)
{
rtbScroll.SelectionColor = (i <= iValueSm) ? Color.Blue : Color.Red;
rtbScroll.SelectedText = (i <= iValueSm) ? "N" : "H";
if (i % 8 == 0 && i != 32)
{
rtbScroll.SelectionColor = Color.Black;
rtbScroll.SelectedText = ".";
}
}
}
I have the whole code working but need a little help getting the converted array to display correctly in the wordListBox. Any help would be appreciated. The code I have is listed below and the string[] word is declared in the class level.
private void Convert(string[] wordArray, int count)
{
//Convert numbers
for (int index = 0; index < count; index++)
{
if (numbers[index] == 1)
{
wordArray[index] = "one";
}
else if (numbers[index] == 2)
{
wordArray[index] = "two";
}
else if (numbers[index] == 3)
{
wordArray[index] = "three";
}
else if (numbers[index] == 4)
{
wordArray[index] = "four";
}
else if (numbers[index] == 5)
{
wordArray[index] = "five";
}
else if (numbers[index] < 1)
{
wordArray[index] = "lower";
}
else
{
wordListBox.Items.Add("higher");
}
}
}
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
wordListBox.Items.Add(word);
}
I would use List<string> instead of string[] for the word variable and method parameter wordArray, so you don't have to initialize the array size. In the ConvertButton_Click you are missing a foreach loop that iterates trough all of the elements in wordArray and adds them to the wordListBox. Here is an example:
int[] numbers = { -5, 3, 6, 9, -2, 1, 0, 4};
int count = 8;
List<string> word = new List<string>();
private void Convert(List<string> wordArray, int count)
{
//Convert numbers
for (int index = 0; index < count; index++)
{
if (numbers[index] == 1)
{
wordArray.Add("one");
}
else if (numbers[index] == 2)
{
wordArray.Add("two");
}
else if (numbers[index] == 3)
{
wordArray.Add("three");
}
else if (numbers[index] == 4)
{
wordArray.Add("four");
}
else if (numbers[index] == 5)
{
wordArray.Add("five");
}
else if (numbers[index] < 1)
{
wordArray.Add("lower");
}
else
{
wordArray.Add("higher");
}
}
}
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
foreach (var item in word)
{
wordListBox.Items.Add(item);
}
}
Result:
Try:
private void ConvertButton_Click(object sender, EventArgs e)
{
wordListBox.Items.Clear();
Convert(word, count);
wordListBox.Items.AddRange(word);
}
I am trying to write a code that lists roots of given number.
This is what I did so far. The result I get is 2*2*5*5 which is true but I want to get this instead: 2^2*5^2.
public partial class Form1 : Form
{
List<int> divisor;
public Form1()
{
InitializeComponent();
}
private void list_Click(object sender, EventArgs e)
{
int number;
divisor = new List<int>();
showroot.Text = "";
number = Int32.Parse(usernum.Text);
for (int i = 2; i <= number; i++)
{
if (number % i == 0)
{
divisor.Add(i);
number = number / i;
i = 1;
}
}
for (int i = 0; i < divisor.Count; i++)
{
print(""+ divisor[i]);
}
}
private void print(String text)
{
if (showroot.Text != "")
{
showroot.Text = showroot.Text + "*" + text;
}
else
{
showroot.Text = text;
}
}
}
I tried to check how much same root and count them by two for statements nested but that brings another errors within.
for (int i = 0; i < divisor.Count; i++) {
for (int a = 0; i < divisor.Count; a++) {
if (i == a) {
base[i]++;
}
}
}
What to do?
Split the task into easy to implement portions, extract methods:
First of all, let's collect all prime divisors (divisors can repeat):
private static IEnumerable<int> AllPrimeDivisors(int value) {
if (value <= 1)
yield break;
for (; value % 2 == 0; value /= 2)
yield return 2;
int n = (int)(Math.Sqrt(value) + 0.5);
for (int d = 3; d <= n; d += 2) {
while (value % d == 0) {
yield return d;
value /= d;
n = (int)(Math.Sqrt(value) + 0.5);
}
}
if (value > 1)
yield return value;
}
Then combine them in required format (we should GroupBy the same - repeating - divisors and represent them either in divisor or in divisor^power format)
private static string Solve(int value) {
var terms = AllPrimeDivisors(value)
.GroupBy(divisor => divisor)
.Select(group => group.Count() == 1
? $"{group.Key}"
: $"{group.Key}^{group.Count()}");
return string.Join("*", terms);
}
Finally add UI:
private void list_Click(object sender, EventArgs e) {
if (int.TryParse(usernum.Text, out var number))
showroot.Text = Solve(number);
else
showroot.Text = "Incorrect Input, Syntax Error";
}
Tests:
int[] tests = new int[] {
3, 5, 9, 12, 16, 41, 81, 100,
};
var result = tests
.Select(item => $"{item,3} == {Solve(item)}");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
3 == 3
5 == 5
9 == 3^2
12 == 2^2*3
16 == 2^4
41 == 41
81 == 3^4
100 == 2^2*5^2
A naive implementation would be by changing your for to this:
for (int i = 2; i <= number; i++)
{
count = 0;
while (number % i == 0)
{
number = number / i;
count++;
}
if (count > 0)
{
divisor.Add(i);
powers.Add(count);
}
}
However a lot of optimizations can be done.
This question already has an answer here:
Why do I only see some of my text output when my Window Forms application has a loop?
(1 answer)
Closed 7 years ago.
I am trying to combine prime numbers, even numbers and odd numbers and their results in a Windows Form Application. I have tested the code in Console but in Windows Form it will not loop to the next applicable number. For example: In console 1 - 10 in primes would result in "2, 3, 5, 7", however in Windows Form Application it will result in "2"
public partial class NumberCalc : Form
{
public NumberCalc()
{
InitializeComponent();
}
private void Primes_CheckedChanged(object sender, EventArgs e)
{
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
bool isPrime = true;
for (f = 0; f <= i; f++)
{
for (int j = 2; j <= i; j++)
{
if (f != j && f % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
string final;
final = ("The Prime Numbers Are:" + f);
Result.Text = final;
}
isPrime = true;
}
}
}
private void Result_TextChanged(object sender, EventArgs e)
{
}
private void Min_TextChanged(object sender, EventArgs e)
{
}
private void Evens_CheckedChanged(object sender, EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
for (f = 0; f >= i; f++)
{
if (f % 2 == 0)
{
{
string final;
final = ("The Even Numbers Are:" + f);
Result.Text = final;
}
}
}
}
private void Odds_CheckedChanged(object sender, EventArgs e)
{
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
for (f = 0; f <= i; f++)
{
if (f % 2 != 0)
{
{
string final;
final = ("The Even Numbers Are:" + f);
Result.Text = final;
}
}
}
}
}
}
Change your code to:
private void Primes_CheckedChanged(object sender, EventArgs e)
{
{
string final = "The Prime Numbers Are:";// you need to keep the result out of the loop instead of reset it everytime
int f = Convert.ToInt32(Min.Text);
int i = Convert.ToInt32(Max.Text);
bool isPrime = true;
for (f = 0; f <= i; f++)// why set f=0 here ? Does not f = min already ?
{
for (int j = 2; j <= i; j++)// maybe j < f not j <= i
{
if (f != j && f % j == 0)// then remove f != j here
{
isPrime = false;
break;
}
}
if (isPrime)
final = final + " " + f;// then add your found number to the result here
isPrime = true;
}
Result.Text = final;
}
}
Even and Odd goes the same.BTW 1 is not prime number, am I right ?
I would combine your loops/checks into one method like this:
private void Form1_Load(object sender, EventArgs e)
{
this.Primes.CheckedChanged += Options_CheckedChanged;
this.Evens.CheckedChanged += Options_CheckedChanged;
this.Odds.CheckedChanged += Options_CheckedChanged;
this.Min.TextChanged += Range_Changed;
this.Max.TextChanged += Range_Changed;
CheckNumbers();
}
private void Range_Changed(object sender, EventArgs e)
{
CheckNumbers();
}
private void Options_CheckedChanged(object sender, EventArgs e)
{
CheckNumbers();
}
private void CheckNumbers()
{
int min, max;
try
{
min = Convert.ToInt32(Min.Text);
max = Convert.ToInt32(Max.Text);
}
catch (Exception)
{
Results.Text = "Invalid Range!";
return;
}
List<int> lstPrimes = new List<int>();
List<int> lstEvens = new List<int>();
List<int> lstOdds = new List<int>();
if (Primes.Checked || Evens.Checked || Odds.Checked)
{
bool isPrime;
for (int f = min; f <= max; f++)
{
if (Primes.Checked)
{
isPrime = true;
for (int j = 2; j <= max; j++)
{
if (f != j && f % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
lstPrimes.Add(f);
}
}
int modResult = f % 2;
if (Evens.Checked && modResult == 0)
{
lstEvens.Add(f);
}
if (Odds.Checked && modResult != 0)
{
lstOdds.Add(f);
}
}
}
StringBuilder sb = new StringBuilder();
if (Primes.Checked)
{
sb.AppendLine("The Prime Numbers Are:" + String.Join(",", lstPrimes));
}
if (Evens.Checked)
{
sb.AppendLine("The Even Numbers Are:" + String.Join(",", lstEvens));
}
if (Odds.Checked)
{
sb.AppendLine("The Odd Numbers Are:" + String.Join(",", lstOdds));
}
Results.Text = sb.ToString();
}
I think LINQ is more suitable here, You can try this:
int[] numbers = Enumerable.Range(f, i-f).ToArray<int>();
string oddNumbers=string.Join(",", from number in numbers
where (number % 2)!=0
select number);
string evenNumbers = string.Join(",", from number in numbers
where (number % 2) == 0
select number);
Result.Text = "The Even Numbers Are:" + evenNumbers;
Result.Text = "The Odd Numbers Are:" + oddNumbers;
Updates for Prime number:
var primeNumbers= string.Join(",",from number in numbers
where (IsPrime(number))
select number);
Where IsPrime() method is defined as follows?
private static bool IsPrime(int number)
{
for (int i = 2; i < number; i ++)
if (number % i == 0) return false;
return true;
}
Here is a snippet from my code. Basically once the button is clicked this logic should fire out and determine if the number is prime or not. The problem is that some numbers are returning as "not prime", when in reality they are. Can anyone point out where the flaw is?
Thank you
private void bntTestPrime_Click(object sender, EventArgs e)
{
int num;
double num_sqrt;
int num_fl;
num = Convert.ToInt32(txtInput.Text);
num_sqrt = Math.Sqrt(num);
num_fl = Convert.ToInt32(Math.Floor(num_sqrt));
for (int i = 1; i <= num_fl; i++)
{
if (num % i == 0 && i != num)
lblResult_prime.Text = "Number " + num + " is not Prime.";
else
lblResult_prime.Text = "Number " + num + " is Prime.";
}
}
To add to Blender's answer, I'd like to point out that you're simply setting the output text on every iteration loop. That means your result will only depend upon the last number checked. What you need to do is assume the number is prime and loop through until a divisor is found. If a divisor is found. The number is prime if and only if no divisors are found. In the end the code should look something like this:
private bool IsPrime(int num)
{
double num_sqrt = Math.Sqrt(num);
int num_fl = Convert.ToInt32(Math.Floor(num_sqrt));
for (int i = 2; i <= num_fl; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
private void bntTestPrime_Click(object sender, EventArgs e)
{
int num = Convert.ToInt32(txtInput.Text);
bool isPrime = IsPrime(num);
if (isPrime)
lblResult_prime.Text = "Number " + num + " is Prime.";
else
lblResult_prime.Text = "Number " + num + " is not Prime.";
}
1 is a factor of every number, so you shouldn't check it. Start at 2. Also, you're already looping from 2 to sqrt(num), so there's no way for i to be equal to num.
You can decrease the performance hit on checking large numbers by using a conditional to check the first 4 primes, then start the loop at 11 and increment by 2. Something like this:
private bool IsPrime(int num)
{
double num_sqrt = Math.Sqrt(num);
int num_fl = Convert.ToInt32(Math.Floor(num_sqrt));
if (num !=1 && num !=2 && num != 3 && num != 5 && num != 7 && num % 2 > 0 _
&& num % 3 > 0 && num % 5 > 0 && num % 7 > 0)
{
for (int i = 11; i <= num_fl; i+=2)
{
if (num % i == 0)
{
return false;
}
}
}
else
return false;
return true;
}
You can shorten your code and increase the performance tremendously by using a List of primes that go big enough to cover the upper limit you want to check. Then use the Contains method to test for prime.
Try the code below.
bool IsPrime(int number) {
if(number%2==0 && number!=2) return false; //no need to check for even numbers
for (int i = 2; i < number; i++) {
if (number % i == 0 && i != number) return false;
}
return true;
}
Try this code below:
bool isPrimeNubmer(int n)
{
if (n >=0 && n < 4) //1, 2, 3 are prime numbers
return true;
else if (n % 2 == 0) //even numbers are not prime numbers
return false;
else
{
int j = 3;
int k = (n + 1) / 2 ;
while (j <= k)
{
if (n % j == 0)
return false;
j = j + 2;
}
return true;
}
}
public class PrimeChecker
{
public static bool Prime(int m)
{
for (int i =2; i< m; i++)
{
if (m % i ==0)
{
return false ;
}
}
return true;
}
public static void Main()
{
Console.WriteLine(Prime(13));
}
}