[gnumeric] Numtheory: minor speed-up



commit fc7f6934e06f899c816ae3b906c0bdcef633ff67
Author: Morten Welinder <terra gnome org>
Date:   Mon Nov 27 08:24:45 2017 -0500

    Numtheory: minor speed-up

 plugins/fn-numtheory/ChangeLog   |    5 +++++
 plugins/fn-numtheory/numtheory.c |   10 ++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
---
diff --git a/plugins/fn-numtheory/ChangeLog b/plugins/fn-numtheory/ChangeLog
index 771a88d..0f558d8 100644
--- a/plugins/fn-numtheory/ChangeLog
+++ b/plugins/fn-numtheory/ChangeLog
@@ -1,3 +1,8 @@
+2017-11-27  Morten Welinder  <terra gnome org>
+
+       * numtheory.c (ithprime): For each new candidate we at most need
+       to consider one extra prime divisor.
+
 2017-11-23  Morten Welinder  <terra gnome org>
 
        * numtheory.c (compute_nt_pi): When searching for an upper bound,
diff --git a/plugins/fn-numtheory/numtheory.c b/plugins/fn-numtheory/numtheory.c
index 76dd445..2d8258b 100644
--- a/plugins/fn-numtheory/numtheory.c
+++ b/plugins/fn-numtheory/numtheory.c
@@ -75,8 +75,7 @@ ithprime (int i, guint64 *res)
                return TRUE;
 
        if ((guint)i > computed) {
-               static guint candidate = 3;
-               static guint jlim = 1;
+               static guint candidate, jlim;
 
                if ((guint)i > allocated) {
                        allocated = MAX ((guint)i, 2 * allocated + 100);
@@ -84,7 +83,9 @@ ithprime (int i, guint64 *res)
                        prime_table = g_renew (guint, prime_table, allocated);
                        if (computed == 0) {
                                prime_table[computed++] = 2;
-                               prime_table[computed++] = 3;
+                               candidate = prime_table[computed++] = 3;
+                               jlim = 1;
+                               g_assert (candidate < prime_table[jlim] * prime_table[jlim]);
                        }
                }
 
@@ -94,7 +95,8 @@ ithprime (int i, guint64 *res)
 
                        candidate += 2;  /* Skip even candidates.  */
 
-                       while (candidate >= prime_table[jlim] * prime_table[jlim])
+                       // We at most need to consider one extra candidate
+                       if (candidate >= prime_table[jlim] * prime_table[jlim])
                                jlim++;
 
                        for (j = 1; j < jlim; j++) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]