Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a bit of a spoiler, so if you want to solve this yourself, don't read this yet :). I'll try to provide hints in order of succession, so you can read each hint in order, and if you need more hints, move to the next hint, etc.</p> <p><strong>Hint #1:</strong> If divisor is a divisor of n, then n / divisor is also a divisor of n. For example, 100 / 2 = 50 with remainder 0, so 2 is a divisor of 100. But this also means that 50 is a divisor of 100.</p> <p><strong>Hint #2</strong> Given Hint #1, what this means is that we can loop from i = 2 to i*i &lt;= n when checking for prime factors. For example, if we are checking the number 100, then we only have to loop to 10 (10*10 is &lt;= 100) because by using hint #1, we will get all the factors. That is:</p> <pre><code>100 / 2 = 50, so 2 and 50 are factors 100 / 5 = 20, so 5 and 20 are factors 100 / 10 = 10, so 10 is a factor </code></pre> <p><strong>Hint #3</strong> Since we only care about prime factors for n, it's sufficient to just find the first factor of n, call it divisor, and then we can recursively find the other factors for n / divisor. We can use a <a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes" rel="noreferrer">sieve</a> approach and mark off the factors as we find them.</p> <p><strong>Hint #4</strong> Sample solution in <code>C</code>:</p> <pre><code>bool factors[100000]; void getprimefactors(int n) { // 0 and 1 are not prime if (n == 0 || n == 1) return; // find smallest number &gt;= 2 that is a divisor of n (it will be a prime number) int divisor = 0; for(int i = 2; i*i &lt;= n; ++i) { if (n % i == 0) { divisor = i; break; } } if (divisor == 0) { // we didn't find a divisor, so n is prime factors[n] = true; return; } // we found a divisor factors[divisor] = true; getprimefactors(n / divisor); } int main() { memset(factors,false,sizeof factors); int f = 1234; getprimefactors(f); int largest; printf("prime factors for %d:\n",f); for(int i = 2; i &lt;= f/2; ++i) { if (factors[i]) { printf("%d\n",i); largest = i; } } printf("largest prime factor is %d\n",largest); return 0; } </code></pre> <p>Output:</p> <pre><code>---------- Capture Output ---------- &gt; "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe prime factors for 1234: 2 617 largest prime factor is 617 &gt; Terminated with exit code 0. </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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