Commit 61fe78f7 authored by Dr. Martin Goik's avatar Dr. Martin Goik

alternate calculation method

parent 1c94b441
......@@ -1165,8 +1165,8 @@ public class CircleAreaCalculator {
final int NUM_OF_DIGITS = 13; // The intended number of adjacent digits
for (int i = 0; i < input.length() - NUM_OF_DIGITS + 1; i++) {
final String fourDigitWord = input.substring(i, i + NUM_OF_DIGITS);
System.out.println(fourDigitWord);
final String thirteenDigitWord = input.substring(i, i + NUM_OF_DIGITS);
System.out.println(thirteenDigitWord);
}
}</programlisting>
......@@ -1210,6 +1210,13 @@ public class CircleAreaCalculator {
}
}</programlisting>
<para>Since digit representations '0' to '9' appear »gapless« in
strictly ascending order this may be simplified:</para>
<programlisting language="java">private static int getDigitValue (final char digit) {
return digit - '0';
}</programlisting>
<para>Next we must compute a given string's product of digits.
Taking the first value <code>7316717653133</code> from above we
are looking for 7 × 3 ×1 × 6 ×7 × 1 ×7 × 6 × 5 × 3 × 1 × 3 × 3 =
......@@ -1240,8 +1247,8 @@ public class CircleAreaCalculator {
xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">up
to 9,223,372,036,854,775,807</link>. This is much larger than our
<quote>worst case</quote> 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 ×
9 × 9 × 9 = 2,541,865,828,329 value. We thus change our data type
from <code language="java">int</code> to <code
9 × 9 × 9 = 2,541,865,828,329 value. We thus have to change our
data type from <code language="java">int</code> to <code
language="java">long</code>:</para>
<programlisting language="java">private static <emphasis
......@@ -1253,8 +1260,8 @@ public class CircleAreaCalculator {
return product;
}</programlisting>
<para>Assembling these pieces leaves us with the following <code
language="java">main(...)</code> method:</para>
<para>Assembling these pieces together leaves us with the
following <code language="java">main(...)</code> method:</para>
<programlisting language="java">public static void main(String[] args) {
......@@ -1279,7 +1286,8 @@ public class CircleAreaCalculator {
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725";
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450";
final int NUM_OF_DIGITS = 13; // The intended number of adjacent digits
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment