Note that there are some explanatory texts on larger screens.

plurals
  1. POproject euler exercise 5 approach
    text
    copied!<p>Question: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p> <p>What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</p> <p>So, I was trying to do exercise 5 on project euler and I came out with this code:</p> <pre><code>#include &lt;stdio.h&gt; #define TRUE 1 #define FALSE 0 int main () { int n, fnd = FALSE, count, i; for (i = 1; fnd == FALSE; i++) { count = 0; for (n = 1; n &lt;= 20; n++) { count += i % n; } printf ("testing %d, count was: %d\n", i, count); if (count == 0) { fnd = TRUE; printf ("%d\n", i); } } return 0; } </code></pre> <p>I believe my apporach is correct, it will surely find the number which is divisible by 1 to 20. But it's been computing for 5 minutes, and still no result. Is my approach correct? If yes, then is there another way to do it? I can't think on another way to solve this, tips would be very much appreciated. Thank you in advance.</p> <p>EDIT: So, based on the advice I was given by you guys I figured it out, thank you so much! So, it's still brute force, but instead of adding 1 to the last number, it now adds 2520, which is the LCM of 1 to 10. And therefore, calculating if the sum of the remainders of the multiples of 2520 divided from 11 to 20 was 0. Since 2520 is already divisible by 1 to 10, I only needed to divide by 11 to 20.</p> <pre><code>#include &lt;stdio.h&gt; #define TRUE 1 #define FALSE 0 int main () { int n, fnd = FALSE, count, i; for (i = 2520; fnd == FALSE; i = i + 2520) { count = 0; for (n = 11; n &lt;= 20; n++) { count += i % n; } printf ("testing %d, count was: %d\n", i, count); if (count == 0 &amp;&amp; i != 0) { fnd = TRUE; printf ("%d\n", i); } } return 0; } </code></pre> <p>Thank you so much, I wouldn't solve it without your help : ) PS: It now computes in less than 10 secs.</p>
 

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