Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C#

C#

Scheduled Pinned Locked Moved C#
csharpquestion
9 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11127370
    wrote on last edited by
    #1

    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...

    L J OriginalGriffO Richard DeemingR B 5 Replies Last reply
    0
    • U User 11127370

      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...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      if (i != j // if i is not equal to j
      && // AND
      i % j == 0) // i divided by j leaves no remeander
      // then i cannot be prime

      1 Reply Last reply
      0
      • U User 11127370

        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...

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        I guess you did not know the % Operator (C# Reference)[^] also known as Modulo operation - Wikipedia, the free encyclopedia[^].

        1 Reply Last reply
        0
        • U User 11127370

          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...

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • U User 11127370

            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...

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            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 of i, then it will also be evenly divisible by a corresponding number less than the square root of i. 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

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • U User 11127370

              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...

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              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.”

              M C 2 Replies Last reply
              0
              • B BillWoodruff

                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.”

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                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

                B 1 Reply Last reply
                0
                • B BillWoodruff

                  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.”

                  C Offline
                  C Offline
                  CHill60
                  wrote on last edited by
                  #8

                  Quote:

                  If not now, when ?

                  So true ...If Not Now... Lyrics - Tracy Chapman[^]

                  Quote:

                  Pretty soon it may be costly

                  ;P

                  1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    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

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #9

                    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.”

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups