Skip to content
Snippets Groups Projects
Commit 611a9ce1 authored by Goik Martin's avatar Goik Martin
Browse files

Minor fixes related to prime number computation

parent bb7cb0c3
No related branches found
No related tags found
No related merge requests found
......@@ -133,8 +133,8 @@
isPrime(final long candidate)</methodname> method classifies
prime numbers as such and whether this message is able to
tell non-primes as well. We achieve this by defining a
boolean array having indexes ranging from 0 to 100. We
then:</para>
boolean array having indexes ranging from 0 to 100
(inclusive). We then:</para>
<itemizedlist>
<listitem>
......@@ -159,7 +159,7 @@
31, 37, 41, 43, 47, 53, 59, 29,
61, 67, 71, 73, 79, 83, 89, 97};
final boolean[] isPrime = new boolean[101]; //Testing 2,3,..,98,99,100
final boolean[] isPrime = new boolean[101]; //Testing 2, 3, 4, .., 99, 100
for (int i = 2; i &lt;= 100; i++) {
isPrime[i] = false;
}
......@@ -174,18 +174,18 @@
<listitem>
<para>Executing this test yields an error at index 49. This
is due to an implementation error. The for- loop had been
defined as:</para>
is due to the chosen limit <code>i * i &lt; candidate</code>
in:</para>
<programlisting language="java"> public static boolean isPrime(final long candidate) {
for (long i = 2; i * i &lt; candidate; i++) {
...</programlisting>
<para>This is wrong: Having <code>candidate == 49</code> the
last value of i to be considered will be 6. So the test for
the result of <code>49 % 7</code> will never be executed
thus returning true. We actually have to modify the loop's
limit slightly different:</para>
last value of i to be considered will be 6. So the required
test <code>49 % 7</code> will never be executed thus
returning true. We have to modify the loop's limit slightly
by using <code>i * i &lt;= candidate</code>:</para>
<programlisting language="java"> public static boolean isPrime(final long candidate) {
for (long i = 2; i * i <emphasis role="bold">&lt;=</emphasis> candidate; i++) {
......@@ -202,8 +202,7 @@
method allows to estimate the prime number computing
performance:</para>
<programlisting language="none">prime numbers found:664579
Elapsed time:14997</programlisting>
<programlisting language="none">Found 664579 prime numbers within 15.136 seconds.</programlisting>
</listitem>
</orderedlist>
</answer>
......@@ -238,8 +237,9 @@ Elapsed time:14997</programlisting>
<para>Learning from <link
xlink:href="http://en.wikipedia.org/wiki/Prime_factor">prime
factorization</link> it actually suffices just to test all
primes up to the already discussed square root limit:</para>
factorization</link> it actually suffices just to test only
prime values up to the already discussed square root
limit:</para>
<programlisting language="none">143 % 2 == 0 ? No.
143 % 3 == 0 ? No.
......@@ -247,13 +247,20 @@ Elapsed time:14997</programlisting>
143 % 7 == 0 ? No.
143 % 11 == 0 ? Yes ==&gt; 143 is not prime</programlisting>
<para>The tradeoff is even bigger for higher prime numbers. Thus
if we store all computed prime numbers without gaps they will
save us a lot of operations. You may want to reuse your unsorted
<classname
<para>The trade off is even bigger for higher prime number
values. Thus if we store computed prime numbers without gaps the
required computation time should decrease.</para>
<para>Storing prime numbers requires an array (or another type
of container). Since we do not know the number of prime numbers
below a given limit the required array size is not yet known
when starting the application. You may want to reuse your
unsorted <classname
xlink:href="Ref/api/P/Array/integerStoreUnbounded/de/hdm_stuttgart/mi/sd1/store/IntegerStore.html">IntegerStore</classname>
implementation. Implement the above algorithm and compare the
elapsed execution time.</para>
implementation to provide a dynamically growing array.</para>
<para>Implement the above algorithm and compare the elapsed
execution time.</para>
</question>
<answer>
......@@ -261,7 +268,7 @@ Elapsed time:14997</programlisting>
<para role="eclipse">Sd1/Prime/V2</para>
</annotation>
<para>This time we only need ~18% of the previous time:</para>
<para>This time we save ~80% of execution time:</para>
<programlisting language="none">prime numbers found:664578
Elapsed time:2639</programlisting>
......
......@@ -11,9 +11,11 @@ public class PrimeNumbers {
public static void main(String[] args) {
final long startTime = System.currentTimeMillis();
System.out.println("prime numbers found:" + findPrimeNumbers(10_000_000));
final long numberOfPrimes = findPrimeNumbers(10_000_000);
final long endTime = System.currentTimeMillis();
System.out.println("Elapsed time:" + (endTime - startTime));
System.out.println("Found " + numberOfPrimes + " prime numbers within " +
(endTime - startTime) / 1000. + " seconds.");
}
/**
......
......@@ -13,12 +13,15 @@ public class PrimeNumbers {
/**
* @param args unused
*/
public static void main(String[] args) {
public static void main(String[] args) {
final long startTime = System.currentTimeMillis();
System.out.println("prime numbers found:" + findPrimeNumbers(10_000_000));
final long numberOfPrimes = findPrimeNumbers(10_000_000);
final long endTime = System.currentTimeMillis();
System.out.println("Elapsed time:" + (endTime - startTime));
System.out.println("Found " + numberOfPrimes + " prime numbers within " +
(endTime - startTime) / 1000. + " seconds.");
}
/**
......@@ -35,8 +38,7 @@ public class PrimeNumbers {
public static long findPrimeNumbers(long limit) {
primes.addValue(2); // Add a seed: The first prime number
for (long i = 3; i <= limit; i++) {
if (isPrime(i)) {
}
isPrime(i);
}
return primes.getNumValues();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment