C#
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
I guess you did not know the % Operator (C# Reference)[^] also known as Modulo operation - Wikipedia, the free encyclopedia[^].
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
The code:
if (i != j && i % j == 0)
{
isPrime = false;
break;
}It's a conditional statement: if the condition matches (i.e. i is not the same as j, and i is a multiple of j) then i is not prime, so it exits the inner loop - and the inner loop only. It still executes all the steps of the outer loop. It's a very inefficient way to list all the primes up to one hundred, is all.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
Member 11161625 wrote:
for (int j = 2; j <= 100; j++)
No number is ever going to be evenly divisible by anything greater than itself, so you can eliminate those numbers from your loop:
if (int j = 2; j < i; j++)
But we can do better than that. If
i
is evenly divisible by a number greater than the square root ofi
, then it will also be evenly divisible by a corresponding number less than the square root ofi
. Therefore, we can ignore all numbers greater than the square root:int squareRoot = (int)Math.Floor(Math.Sqrt(i));
for (int j = 2; j < squareRoot; j++)But we can still do better. Currently, you're checking for divisibility by every number in the range. But if it's not divisible by 2, then it won't be divisible by any multiple of 2. Using the Sieve of Eratosthenes[^], you can eliminate all multiples of the prime numbers, leaving just the prime numbers:
var notPrime = new HashSet<int>();
for (int i = 2; i <= 100; i++)
{
if (!notPrime.Contains(i))
{
// i is a prime number:
Console.WriteLine("\t{0}", i);// All multiples of i are NOT prime numbers: for (int j = i \* 2; j <= 100; j += i) { notPrime.Add(j); } }
}
There are other even more efficient ways to find prime numbers, but they tend to be much more complicated, and would be overkill for a simple scenario like this.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi, class Program { static void Main(string[] args) { bool isPrime = true; Console.WriteLine("Prime Numbers are:"); for (int i = 2; i <= 100; i++) { for (int j = 2; j <= 100; j++) { if (i != j && i % j == 0) { isPrime = false; break; } } if (isPrime) { Console.Write("\t"+i); } isPrime = true; } Console.ReadKey(); } } in this code,i didn't understand this line of code,Please explain me??? if (i != j && i % j == 0) { isPrime = false; break; } what it means??? Thanks...
What this code is telling me is: since November, last year, you have racked-up a very large number of down-votes on a large number of questions in most of which you have demonstrated you have made very little effort to educate yourself. It is time for you to make a decision to go to work doing the basic study that anyone has to do to become competent, at an entry level, in mastering any program language (or any subject matter). When you have code like the code shown here (relatively simple), put it in a C# project, and put break-points in it, run the code, single-step through it (F11 in Visual Studio) observing how the values of variables change, and what the flow-of-control is. Every time you see something that puzzles you like the character "%" : select the character, or whatever: hit the F1 key and go to the built-in help browser (or go-on-line and look it up) : read the relevant content, think about it. Study, experiment, observe, refine, analyze, conclude ... repeat this forever with enthusiasm ... that is the only way to achieve any competency. CodeProject, StackOverflow, MSDN, blog sites (many exceptionally good blog sites run by Indians who are also regulars on CodeProject): these resources are amazingly deep, and can support you in whatever you pursue with C# and .NET. Get a good fundamental book on C# .NET and read: alternate reading with trying out small code experiments directly related to what you read. Charles Petzold's ".NET Book Zero" is a free on-line download, and a good resource. Look in the "Free Tools" forum here for my previous post (January 11, 2015) on a free (English) book on C# by an association of programmers in Bulgaria. "If not you, who ? If not now, when ?"
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
-
What this code is telling me is: since November, last year, you have racked-up a very large number of down-votes on a large number of questions in most of which you have demonstrated you have made very little effort to educate yourself. It is time for you to make a decision to go to work doing the basic study that anyone has to do to become competent, at an entry level, in mastering any program language (or any subject matter). When you have code like the code shown here (relatively simple), put it in a C# project, and put break-points in it, run the code, single-step through it (F11 in Visual Studio) observing how the values of variables change, and what the flow-of-control is. Every time you see something that puzzles you like the character "%" : select the character, or whatever: hit the F1 key and go to the built-in help browser (or go-on-line and look it up) : read the relevant content, think about it. Study, experiment, observe, refine, analyze, conclude ... repeat this forever with enthusiasm ... that is the only way to achieve any competency. CodeProject, StackOverflow, MSDN, blog sites (many exceptionally good blog sites run by Indians who are also regulars on CodeProject): these resources are amazingly deep, and can support you in whatever you pursue with C# and .NET. Get a good fundamental book on C# .NET and read: alternate reading with trying out small code experiments directly related to what you read. Charles Petzold's ".NET Book Zero" is a free on-line download, and a good resource. Look in the "Free Tools" forum here for my previous post (January 11, 2015) on a free (English) book on C# by an association of programmers in Bulgaria. "If not you, who ? If not now, when ?"
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
Such sage advice which, being the 5th response and does not include any code will probably not have the least impact on such as the OP.
Never underestimate the power of human stupidity RAH
-
What this code is telling me is: since November, last year, you have racked-up a very large number of down-votes on a large number of questions in most of which you have demonstrated you have made very little effort to educate yourself. It is time for you to make a decision to go to work doing the basic study that anyone has to do to become competent, at an entry level, in mastering any program language (or any subject matter). When you have code like the code shown here (relatively simple), put it in a C# project, and put break-points in it, run the code, single-step through it (F11 in Visual Studio) observing how the values of variables change, and what the flow-of-control is. Every time you see something that puzzles you like the character "%" : select the character, or whatever: hit the F1 key and go to the built-in help browser (or go-on-line and look it up) : read the relevant content, think about it. Study, experiment, observe, refine, analyze, conclude ... repeat this forever with enthusiasm ... that is the only way to achieve any competency. CodeProject, StackOverflow, MSDN, blog sites (many exceptionally good blog sites run by Indians who are also regulars on CodeProject): these resources are amazingly deep, and can support you in whatever you pursue with C# and .NET. Get a good fundamental book on C# .NET and read: alternate reading with trying out small code experiments directly related to what you read. Charles Petzold's ".NET Book Zero" is a free on-line download, and a good resource. Look in the "Free Tools" forum here for my previous post (January 11, 2015) on a free (English) book on C# by an association of programmers in Bulgaria. "If not you, who ? If not now, when ?"
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
-
Such sage advice which, being the 5th response and does not include any code will probably not have the least impact on such as the OP.
Never underestimate the power of human stupidity RAH
I don't know why, Mycroft, that your comment brought to my mind Milton's great sonnet on his blindness, specifically the final line: "They also serve who only stand and wait." "Least impact" could well describe my total presence on CodeProject :) ... and ... yet ... cheers, Bill
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”