Skip to content

Commit

Permalink
Added Cunningham factoring support, including Aurifeuillian factoriza…
Browse files Browse the repository at this point in the history
…tion and retrieving factors from Web server
  • Loading branch information
alpertron committed Jan 1, 2017
1 parent 40b1fe9 commit 236cfcb
Show file tree
Hide file tree
Showing 17 changed files with 694 additions and 68 deletions.
4 changes: 2 additions & 2 deletions ConsoleApplication1.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001643000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000163423</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>20927902484106783612273926739453160362527437728623703270385749772858418967283908642445280836244059729054583455420959898929436431361178008664032378075583153913934702685203576143400536338012443636480379262017668896452308479037888217888995203019681763505021868120481527671211777014946532005541417320447</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerCommandArguments>9999999999999999999999999999999999999999999999999999733</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>20927902484106783612273926739453160362527437728623703270385749772858418967283908642445280836244059729054583455420959898929436431361178008664032378075583153913934702685203576143400536338012443636480379262017668896452308479037888217888995203019681763505021868120481527671211777014946532005541417320447</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
20 changes: 11 additions & 9 deletions ECMNEW.HTM
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<meta charset="utf-8" />
<meta name="description" content="Javascript application that factors integers using ECM and SIQS algorithms. Written by Dario Alpern." />
<link rel="alternate" hreflang="es" href="ECMC.HTM" />
<link rel="prefetch" href="ecmW0712.js" />
<link rel="stylesheet" type="text/css" href="ecm0912.css">
<link rel="prefetch" href="ecmW0003.js" />
<link rel="stylesheet" type="text/css" href="ecm0002.css">
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<title>Integer factorization calculator</title>
</head>
<body>
Expand Down Expand Up @@ -50,7 +51,7 @@ Contact
</nav>
<article>
<h1>Integer factorization calculator</h1>
<script async="async" type="text/javascript" src="ecm1012.js"></script>
<script async="async" type="text/javascript" src="ecm0003.js"></script>
<div class="pad">
<div id="a" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" itemref="b" class="inline">
<a href="ENGLISH.HTM" itemprop="url">
Expand Down Expand Up @@ -131,12 +132,13 @@ Contact
<h2>Configuration</h2>
<p>You can change settings for this application by pressing the Config button when a factorization is not in progress. A new window will pop up where you can select different settings:</p>
<ul>
<li>Digits per group: In order to improve readability, big numbers are separated by spaces forming groups of a fixed number of digits. With this input box, you can determine the number of digits in a group.</li>
<li>Batch mode: See explanation in next section.</li>
<li>Verbose mode: Not ready yet.</li>
<li>Pretty print: If this checkbox is set, the exponents are shown in superscripts and the multiplication sign is &quot; &times; &quot;. The application also shows the number of digits for numbers with more than 30 digits.
<li><strong>Digits per group</strong>: In order to improve readability, big numbers are separated by spaces forming groups of a fixed number of digits. With this input box, you can determine the number of digits in a group.</li>
<li><strong>Batch mode</strong>: See explanation in next section.</li>
<li><strong>Verbose mode</strong>: Not ready yet.</li>
<li><strong>Pretty print</strong>: If this checkbox is set, the exponents are shown in superscripts and the multiplication sign is &quot; &times; &quot;. The application also shows the number of digits for numbers with more than 30 digits.
If the checkbox is not set, the exponents are preceded by the exponentiation sign &quot; ^ &quot; and the multiplication is indicated by asterisks. Also the number of digits is never displayed. This mode eases copying the results to other mathematical programs.</li>
<li>Use Cunningham tables on server: Not ready yet.</li>
<li><strong>Use Cunningham tables on server</strong>: When selected, if the number to be factored has the form a<sup>b</sup> &pm; 1, the application will attempt to retrieve the known factors from the Web server.
In order to reduce the database, only factors with at least 14 digits are included, so the application will find the small factors. These factors comes from <a href="http://myfactors.mooo.com/">Jonathan Crombie list</a> and it includes 2674850 factors of Cunningham numbers.</li>
</ul>
<p>The configuration is saved in your device, so when you start again the Web browser, all settings remain the same.
<p>
Expand All @@ -158,7 +160,7 @@ The line to type is: <code>x=3;x=n(x);c-100;x-1</code>.</p>
<h2>Source code</h2>
<p>You can download the source of the current program and the old factorization applet from <a href="https://github.com/alpertron/calculators">GitHub</a>. Notice that the source code is in C language and you need the
<a href="https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html">Emscripten</a> environment in order to generate Javascript.</p>
<p>Written by Dario Alpern. Last updated 18 December 2016.</p>
<p>Written by Dario Alpern. Last updated 1 January 2017.</p>
</div>
<div id="result" aria-live="polite"></div>
<div id="status"></div>
Expand Down
38 changes: 32 additions & 6 deletions bignbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,45 @@ enum eExprErr BigIntRemainder(BigInteger *pDividend, BigInteger *pDivisor, BigIn
return EXPR_OK;
}

void expBigNbr(BigInteger *bignbr, double logar)
{
int mostSignificantLimb;
logar /= log(2);
bignbr->sign = SIGN_POSITIVE;
bignbr->nbrLimbs = (int)floor(logar / BITS_PER_GROUP);
mostSignificantLimb = (int)floor(exp((logar - BITS_PER_GROUP*bignbr->nbrLimbs) * log(2)) + 0.5);
if (mostSignificantLimb == LIMB_RANGE)
{
mostSignificantLimb = 1;
bignbr->nbrLimbs++;
}
memset(bignbr->limbs, 0, bignbr->nbrLimbs * sizeof(limb));
bignbr->limbs[bignbr->nbrLimbs].x = mostSignificantLimb;
bignbr->nbrLimbs++;
}

double logBigNbr(BigInteger *pBigNbr)
{
int nbrLimbs;
double logar;
int nbrLimbs, value;
double logar, sum;
nbrLimbs = pBigNbr->nbrLimbs;
if (nbrLimbs > 1)
if (nbrLimbs == 1)
{
logar = log((double)(pBigNbr->limbs[nbrLimbs - 2].x + (pBigNbr->limbs[nbrLimbs - 1].x << BITS_PER_GROUP))) +
(double)((nbrLimbs - 2)*BITS_PER_GROUP)*log(2);
logar = log((double)(pBigNbr->limbs[nbrLimbs - 1].x) + (double)(nbrLimbs - 1)*log((double)(1 << BITS_PER_GROUP)));
}
else
{
logar = log((double)(pBigNbr->limbs[nbrLimbs - 1].x) + (double)(nbrLimbs - 1)*log((double)(1 << BITS_PER_GROUP)));
value = pBigNbr->limbs[nbrLimbs - 2].x + (pBigNbr->limbs[nbrLimbs - 1].x << BITS_PER_GROUP);
sum = (double)((nbrLimbs - 2)*BITS_PER_GROUP)*log(2);
if (nbrLimbs == 2)
{
logar = log((double)value) + sum;
}
else
{
logar = log((double)value + (double)pBigNbr->limbs[nbrLimbs - 3].x / LIMB_RANGE) +
sum;
}
}
return logar;
}
Expand Down
1 change: 1 addition & 0 deletions bignbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void CompressLimbsBigInteger(/*@out@*/limb *ptrValues, /*@in@*/BigInteger *bigin
void NbrToLimbs(int nbr, /*@out@*/limb *limbs, int len);
void ComputeInversePower2(/*@in@*/limb *value, /*@out@*/limb *result, /*@out@*/limb *aux);
int BigNbrIsZero(limb *value);
void expBigNbr(BigInteger *pBigNbr, double logar);
double logBigNbr(BigInteger *pBigNbr);
double logLimbs(limb *pBigNbr, int nbrLimbs);

Expand Down
6 changes: 2 additions & 4 deletions dilog.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
#include "factor.h"

#ifdef __EMSCRIPTEN__
void databack(char *data);
int stamp(void);
extern int newStamp, oldStamp;
extern long long lModularMult;
#endif
Expand Down Expand Up @@ -181,7 +179,7 @@ void DiscreteLogarithm(void)
if (!TestBigNbrEqual(&LastModulus, &modulus))
{
CompressBigInteger(nbrToFactor, &modulus);
factor(nbrToFactor, factorsMod, astFactorsMod, NULL);
factor(&modulus, nbrToFactor, factorsMod, astFactorsMod, NULL);
NbrFactorsMod = astFactorsMod[0].multiplicity;
}
DiscreteLog.nbrLimbs = 1; // DiscreteLog <- 0
Expand All @@ -208,7 +206,7 @@ void DiscreteLogarithm(void)
groupOrder.limbs[0].x--;
showText("Computing discrete logarithm...");
CompressBigInteger(nbrToFactor, &groupOrder);
factor(nbrToFactor, factorsGO, astFactorsGO, NULL); // factor groupOrder.
factor(&groupOrder, nbrToFactor, factorsGO, astFactorsGO, NULL); // factor groupOrder.
NbrFactors = astFactorsGO[0].multiplicity;
UncompressBigInteger(ptrPrime, &mod);
logar.nbrLimbs = 1; // logar <- 0
Expand Down
9 changes: 5 additions & 4 deletions ecm.appcache
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
CACHE MANIFEST
# 2016-12-18
# 2017-01-01-1

CACHE:
/ECMNEW.HTM
/ecm1012.js
/ecmW0712.js
/ecm0912.css
/ecm0003.js
/ecmW0003.js
/ecm0002.css
/favicon.ico

NETWORK:
*
10 changes: 4 additions & 6 deletions ecmfront.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ char *ptrInputText;

#ifdef __EMSCRIPTEN__
extern int NextEC;
void databack(char *data);
extern double originalTenthSecond;
int tenths(void);
void GetDHMSt(char **pptrText, int tenths);
#endif

Expand Down Expand Up @@ -150,7 +148,7 @@ static void BatchFactorization(char *tofactorText, int doFactorization)
char *ptrNextBatchFactor = tofactorText;
char *ptrEndBatchFactor = tofactorText + strlen(tofactorText);
char *ptrCurrBatchFactor;
int counter;
int counter = 0;
char *ptrOutput = output;
char *NextExpr, *EndExpr, *FactorExpr;
char c;
Expand Down Expand Up @@ -258,7 +256,7 @@ static void BatchFactorization(char *tofactorText, int doFactorization)
{
NumberLength = tofactor.nbrLimbs;
CompressBigInteger(nbrToFactor, &tofactor);
factor(nbrToFactor, factorsMod, astFactorsMod, NULL);
factor(&tofactor, nbrToFactor, factorsMod, astFactorsMod, NULL);
}
SendFactorizationToOutput(rc, astFactorsMod, &ptrOutput, doFactorization);
if (evalExpression(NextExpr, counter, &result) != 0)
Expand All @@ -278,7 +276,7 @@ static void BatchFactorization(char *tofactorText, int doFactorization)
{
NumberLength = tofactor.nbrLimbs;
CompressBigInteger(nbrToFactor, &tofactor);
factor(nbrToFactor, factorsMod, astFactorsMod, NULL);
factor(&tofactor, nbrToFactor, factorsMod, astFactorsMod, NULL);
}
SendFactorizationToOutput(rc, astFactorsMod, &ptrOutput, doFactorization);
counter = 2;
Expand Down Expand Up @@ -469,7 +467,7 @@ void ecmFrontText(char *tofactorText, int doFactorization, char *knownFactors)
#ifdef __EMSCRIPTEN__
lModularMult = 0;
#endif
factor(nbrToFactor, factorsMod, astFactorsMod, knownFactors);
factor(&tofactor, nbrToFactor, factorsMod, astFactorsMod, knownFactors);
}
ptrOutput = output;
strcpy(output, "2<p>");
Expand Down
2 changes: 1 addition & 1 deletion ecmfront.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
@media screen and (max-width: 400px) { #smallheader { font-size:0.7em; } }
@media screen and (min-width: 400px) { #smallheader { font-size:1em; } }
body {font-family: arial; margin: 0; padding: 0;}
body {font-family: arial; margin: 0; padding: 0; background-color:#FFFFFF; color:#000000}
h1 {text-align:center;}
.lf {padding:0.2em; clear:both;}
.blue {color: #0000FF;}
Expand Down
2 changes: 1 addition & 1 deletion ecmfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function callWorker(param)
{
if (!worker)
{
worker = new Worker("ecmW0712.js");
worker = new Worker("ecmW0003.js");
worker.onmessage = function(e)
{ // First character of e.data is "1" for intermediate text
// and it is "2" for end of calculation.
Expand Down
10 changes: 8 additions & 2 deletions expression.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#ifndef _EXPRESSION_H
#define _EXPRESSION_H

#define COPYRIGHT_SPANISH "Hecho por Darío Alpern. Actualizado el 18 de diciembre de 2016."
#define COPYRIGHT_ENGLISH "Written by Dario Alpern. Last updated on 18 December 2016."
#define COPYRIGHT_SPANISH "Hecho por Darío Alpern. Actualizado el 1 de enero de 2017."
#define COPYRIGHT_ENGLISH "Written by Dario Alpern. Last updated on 1 January 2017."

#ifdef __EMSCRIPTEN__
double tenths(void);
void databack(char *data);
int stamp(void);
#endif

enum eExprErr
{
Expand Down
Loading

0 comments on commit 236cfcb

Please sign in to comment.