Skip to content

Commit

Permalink
Perform trial division for the first 180 primes on isprime function i…
Browse files Browse the repository at this point in the history
…f argument has more than one limb
  • Loading branch information
Dario Alpern committed May 6, 2020
1 parent 6eb6209 commit bdb489f
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 101 deletions.
6 changes: 3 additions & 3 deletions CONTFRAC.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions CUAD.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions CUADMOD.HTM

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ConsoleApplication1.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>11*modinv(2,7)</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>11</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
8 changes: 4 additions & 4 deletions ECM.HTM

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions ECMC.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions FACTPOL.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions FCUBES.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions FRACCONT.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions FSQUARES.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions GAUSIANO.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions GAUSSIAN.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions POLFACT.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions QUAD.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions QUADMOD.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions SUMCUAD.HTM

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions SUMCUBOS.HTM

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions bignbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
#include "factor.h"
#include "expression.h"
#include "skiptest.h"

static BigInteger Temp, Temp2, Temp3, Base, Power, expon;
static char ProcessExpon[MAX_LEN*BITS_PER_GROUP + 1000];
static char primes[MAX_LEN*BITS_PER_GROUP + 1000];
Expand All @@ -33,6 +34,7 @@ extern int q[MAX_LEN];
extern limb TestNbr[MAX_LEN];
extern limb MontgomeryMultR1[MAX_LEN];
int groupLen = 6;
static int smallPrimes[SMALL_PRIMES_ARRLEN];
#ifdef __EMSCRIPTEN__
int percentageBPSW;
#endif
Expand Down Expand Up @@ -1521,6 +1523,33 @@ static void Halve(limb *pValue)
}
}

void initializeSmallPrimes(int* pSmallPrimes)
{
int ctr, P;
if (*pSmallPrimes != 0)
{
return;
}
P = 3;
*pSmallPrimes++ = 2;
for (ctr = 1; ctr < SMALL_PRIMES_ARRLEN; ctr++)
{ // Loop that fills the SmallPrime array.
int Q;
*pSmallPrimes++ = P; /* Store prime */
do
{
P += 2;
for (Q = 3; Q * Q <= P; Q += 2)
{ /* Check if P is prime */
if (P % Q == 0)
{
break; /* Composite */
}
}
} while (Q * Q <= P);
}
}

// BPSW primality test:
// 1) If the input number is 2-SPRP composite, indicate composite and go out.
// 2) If number is perfect square, indicate it is composite and go out.
Expand Down Expand Up @@ -1561,6 +1590,22 @@ int BpswPrimalityTest(/*@in@*/BigInteger *pValue)
{
return 1; // Number is even and different from 2. Indicate composite.
}
if (nbrLimbs > 1)
{ // Check whether it is divisible by small number.
int primeIndex;
initializeSmallPrimes(smallPrimes);
for (primeIndex = 0; primeIndex < 180; primeIndex += 3)
{
int primeProd = smallPrimes[primeIndex] * smallPrimes[primeIndex+1] * smallPrimes[primeIndex+2];
int remainder = getRemainder(pValue, primeProd);
if (remainder % smallPrimes[primeIndex] == 0 ||
remainder % smallPrimes[primeIndex + 1] == 0 ||
remainder % smallPrimes[primeIndex + 2] == 0)
{
return 1; // Number is divisible by small number. Indicate composite.
}
}
}
#ifdef __EMSCRIPTEN__
#ifdef FACTORIZATION_APP
StepECM = 3; // Show progress (in percentage) of BPSW primality test.
Expand Down
8 changes: 4 additions & 4 deletions dilog.htm

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
#define MAXIMUM_OPERATOR 18

#define COMPUTE_NEXT_PRIME_SIEVE_SIZE 2000
#define SMALL_PRIMES_ARRLEN 1229 // Number of primes less than 10000.

static limb comprStackValues[COMPR_STACK_SIZE];
static int comprStackOffset[PAREN_STACK_SIZE];
Expand Down Expand Up @@ -1073,33 +1072,6 @@ static enum eExprErr func(char *expr, BigInteger *ExpressionResult,
return EXPR_OK;
}

static void initializeSmallPrimes(int* pSmallPrimes)
{
int ctr, P;
if (*pSmallPrimes != 0)
{
return;
}
P = 3;
*pSmallPrimes++ = 2;
for (ctr = 1; ctr < SMALL_PRIMES_ARRLEN; ctr++)
{ // Loop that fills the SmallPrime array.
int Q;
*pSmallPrimes++ = P; /* Store prime */
do
{
P += 2;
for (Q = 3; Q * Q <= P; Q += 2)
{ /* Check if P is prime */
if (P % Q == 0)
{
break; /* Composite */
}
}
} while (Q * Q <= P);
}
}

static void generateSieve(int* pSmallPrimes, char* sieve, BigInteger* pArgument, int isNext)
{
int ctr;
Expand Down
7 changes: 5 additions & 2 deletions expression.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef _EXPRESSION_H
#define _EXPRESSION_H

#define COPYRIGHT_SPANISH "Hecho por Darío Alpern. Actualizado el 4 de mayo de 2020."
#define COPYRIGHT_ENGLISH "Written by Dario Alpern. Last updated on 4 May 2020."
#define COPYRIGHT_SPANISH "Hecho por Darío Alpern. Actualizado el 6 de mayo de 2020."
#define COPYRIGHT_ENGLISH "Written by Dario Alpern. Last updated on 6 May 2020."

#ifdef __EMSCRIPTEN__
int stamp(void);
Expand Down Expand Up @@ -43,6 +43,8 @@ enum eExprErr
EXPR_OK = 0,
EXPR_NOT_FOUND,
};
#define SMALL_PRIMES_ARRLEN 1229 // Number of primes less than 10000.

#ifndef lang
extern int lang;
#endif
Expand All @@ -57,5 +59,6 @@ void partition(int val, BigInteger *pResult);
void factorial(BigInteger *result, int argument);
void primorial(BigInteger *result, int argument);
void textError(char * ptrOutput, enum eExprErr rc);
void initializeSmallPrimes(int* pSmallPrimes);
#else
#endif
8 changes: 4 additions & 4 deletions logdi.htm

Large diffs are not rendered by default.

0 comments on commit bdb489f

Please sign in to comment.