diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 7eb214fc1cee89b06e02bf45b86a272ae6c5d6a4..6cd304c3f1022380bb9f3a9d91e8676f4da6ad75 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -4734,29 +4734,28 @@ try (final Scanner scan = new Scanner(System.in)) { <answer> <para>We propose the following solution:</para> - <programlisting language="java"> public static void main(String[] args) { - - final int highestDivisor = 20; // May be adjusted to other limits. + <programlisting language="java">public static void main(String[] args) { - int candidate = highestDivisor; // start with highest divisor. + final int highestDivisor = 20; // May be adjusted to other limits. - boolean atLeastOneRemainder; + int candidate = highestDivisor; // start with highest divisor. - do { - candidate++; // next candidate value. - atLeastOneRemainder = false; + boolean atLeastOneRemainder; - for (int i = 2; i <= highestDivisor; i++) { - if (0 != candidate % i) { // Is there a non-zero remainder? - atLeastOneRemainder = true; // Continue outer while. - break; // Leave current for loop. - } - } + do { + candidate++; // next candidate value. + atLeastOneRemainder = false; - } while (atLeastOneRemainder); // Increase candidate further? + for (int i = highestDivisor; 2 <= i; i--) {// Higher values are more likely having a remainder + if (0 != candidate % i) { // Is there a non-zero remainder? + atLeastOneRemainder = true; // Continue outer while. + break; // Leave current for loop. + } + } + } while (atLeastOneRemainder); // Increase candidate further? - System.out.println(candidate); - }</programlisting> + System.out.println(candidate); +}</programlisting> <para>Executing this code results in a value of 232792560 for the smallest desired value.</para> @@ -4773,8 +4772,8 @@ try (final Scanner scan = new Scanner(System.in)) { <qandaentry> <question> <para>Solving the previous exercise by a program is quite a - no-brainer (from a software developer's point of view). - Provide a different solution purely based on algebraic + no-brainer (from an experienced software developer's point of + view). Provide a different solution purely based on algebraic considerations. In other words: Your solution should work by using paper and pencil exclusively.</para> @@ -5648,37 +5647,47 @@ try (final Scanner scan = new Scanner(System.in)) { Thus we are looking for a better (more efficient) solution.</para> - <para>There are several issues with the above code. It - creates a great number of combinations like <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mo>(</m:mo> + <para>There are several issues with the above code:</para> - <m:mrow> - <m:mn>1</m:mn> + <orderedlist> + <listitem> + <para>It creates a great number of combinations like + <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>(</m:mo> - <m:mo>,</m:mo> + <m:mrow> + <m:mn>1</m:mn> - <m:mn>4</m:mn> + <m:mo>,</m:mo> - <m:mo>,</m:mo> + <m:mn>4</m:mn> - <m:mn>3</m:mn> - </m:mrow> + <m:mo>,</m:mo> - <m:mo>)</m:mo> - </m:mrow> - </m:math> - </inlineequation> which could be easily ruled out - beforehand rather then creating and filtering them. Do we - really need three nested loops?</para> + <m:mn>3</m:mn> + </m:mrow> - <para>Also triples having identical values in different - order like <code>(40, 399, 401)</code> and <code>(40, 401, - 399)</code> are equivalent. For this reason we may consider - only triples being ordered by value.</para> + <m:mo>)</m:mo> + </m:mrow> + </m:math> + </inlineequation> which could be easily ruled out + beforehand rather then creating and filtering + them.</para> + + <para>Do we really need three nested loops?</para> + </listitem> + + <listitem> + <para>Triples having identical values in different order + like <code>(40, 399, 401)</code> and <code>(40, 401, + 399)</code> are equivalent. For this reason we may + consider only triples being ordered by value.</para> + </listitem> + </orderedlist> - <para>So the problem can be stated to find the set of all + <para>The problem can thus be stated to find the set of all triples <inlineequation> <m:math display="inline"> <m:mrow> @@ -5721,7 +5730,7 @@ try (final Scanner scan = new Scanner(System.in)) { </m:mrow> </m:mrow> </m:math> - </inlineequation> which simultaneously obey:</para> + </inlineequation> simultaneously obeying:</para> <orderedlist> <listitem xml:id="sw1PhytagoreanTripletCondition1"> @@ -6223,9 +6232,10 @@ for (int a = 1; a <= sum / 3; a++) { } }</programlisting> - <para>This yields the same result as before but execution is - (again) ways faster paying off when looking for larger values - of <code language="java">sum</code>.</para> + <para>We've thus reduced three nested loops into just one + getting exactly the same result as before. Obviously execution + is (again) ways faster paying off when looking for larger + values of <code language="java">sum</code>.</para> </answer> </qandaentry> </qandadiv>