Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok. So you said you understand the basics, but you're not sure EXACTLY how it works. First of all, this is a great answer to the Project Euler question it stems from. I've done a lot of research into this problem and this is by far the simplest response.</p> <p>For the purpose of explanation, I'll let <code>n = 20</code>. To run the real Project Euler problem, let <code>n = 600851475143</code>.</p> <pre><code>n = 20 i = 2 while i * i &lt; n: while n%i == 0: n = n / i i = i + 1 print (n) </code></pre> <p>This explanation uses two <code>while</code> loops. The biggest thing to remember about <code>while</code> loops is that they run until they are no longer <code>true</code>. </p> <p>The outer loop states that while <code>i * i</code> isn't greater than <code>n</code> (because the largest prime factor will never be larger than the square root of <code>n</code>), add <code>1</code> to <code>i</code> after the inner loop runs. </p> <p>The inner loop states that while <code>i</code> divides evenly into <code>n</code>, replace <code>n</code> with <code>n</code> divided by <code>i</code>. This loop runs continuously until it is no longer true. For <code>n=20</code> and <code>i=2</code>, <code>n</code> is replaced by <code>10</code>, then again by <code>5</code>. Because <code>2</code> doesn't evenly divide into <code>5</code>, the loop stops with <code>n=5</code> and the outer loop finishes, producing <code>i+1=3</code>.</p> <p>Finally, because <code>3</code> squared is greater than <code>5</code>, the outer loop is no longer <code>true</code> and prints the result of <code>n</code>.</p> <p>Thanks for posting this. I looked at the code forever before realizing how exactly it worked. Hopefully, this is what you're looking for in a response. If not, let me know and I can explain further.</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. 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