Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, so forget about C. Suppose I give you a number and ask you to determine if it's prime. How do you do it? Write down the steps clearly, <em>then</em> worry about translating them into code.</p> <p>Once you have the algorithm determined, it will be much easier for you to figure out how to write a program, and for others to help you with it.</p> <p><strong>edit:</strong> Here's the C# code you posted:</p> <pre><code>static bool IsPrime(int number) { for (int i = 2; i &lt; number; i++) { if (number % i == 0 &amp;&amp; i != number) return false; } return true; } </code></pre> <p>This is <em>very nearly</em> valid C as is; there's no <code>bool</code> type in C, and no <code>true</code> or <code>false</code>, so you need to modify it a little bit (edit: Kristopher Johnson correctly points out that C99 added the stdbool.h header). Since some people don't have access to a C99 environment (but you should use one!), let's make that very minor change:</p> <pre><code>int IsPrime(int number) { int i; for (i=2; i&lt;number; i++) { if (number % i == 0 &amp;&amp; i != number) return 0; } return 1; } </code></pre> <p>This is a perfectly valid C program that does what you want. We can improve it a little bit without too much effort. First, note that <code>i</code> is always less than <code>number</code>, so the check that <code>i != number</code> always succeeds; we can get rid of it.</p> <p>Also, you don't actually need to try divisors all the way up to <code>number - 1</code>; you can stop checking when you reach sqrt(number). Since <code>sqrt</code> is a floating-point operation and that brings a whole pile of subtleties, we won't actually compute <code>sqrt(number)</code>. Instead, we can just check that <code>i*i &lt;= number</code>:</p> <pre><code>int IsPrime(int number) { int i; for (i=2; i*i&lt;=number; i++) { if (number % i == 0) return 0; } return 1; } </code></pre> <p>One last thing, though; there was a small bug in your original algorithm! If <code>number</code> is negative, or zero, or one, this function will claim that the number is prime. You likely want to handle that properly, and you may want to make <code>number</code> be unsigned, since you're more likely to care about positive values only:</p> <pre><code>int IsPrime(unsigned int number) { if (number &lt;= 1) return 0; // zero and one are not prime unsigned int i; for (i=2; i*i&lt;=number; i++) { if (number % i == 0) return 0; } return 1; } </code></pre> <p>This definitely isn't the fastest way to check if a number is prime, but it works, and it's pretty straightforward. We barely had to modify your code at all!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload