JAVASCRIPT CODE FOR FAST FERMAT INTEGER FAMILY FACTORING

Part Six: Basics of Integer Factoring

JB Johnson
Nerd For Tech
31 min readOct 26, 2021

--

Comparison of Integer Factoring Routines

Foreword

This essay provides the JavaScript Code for several of my favorite simple Integer Factoring Routines and the prototype for Enhanced Fermat and Fast Fermat Integer Family Factoring. This software is functional (to a degree) and is provided to demonstrate that the math involved in Integer Family Factoring can be incorporated into working software. Please be aware this software has not passed anything resembling a comprehensive test and may have (lots of) unresolved bugs. Whether you choose to run this software or not, it still exists for your education and study. So, enjoy!

General information on Integer Factorization Algorithms can be found at:

https://en.wikipedia.org/wiki/Category:Integer_factorization_algorithms

and

https://en.wikipedia.org/wiki/Integer_factorization

The basics of Integer Factoring Families are discussed in the following articles:

https://jbjo1956.medium.com/introduction-to-integer-factoring-families-dce2152fba81?source=friends_link&sk=382a4fef0eac9c47673c550db9560761

and

https://jbjo1956.medium.com/integer-number-families-the-next-generation-3f298d347b5c?source=friends_link&sk=1c3b14de8c950bf6588c4c114e60ac95

and

https://jbjo1956.medium.com/fast-fermat-integer-family-factoring-e2c277943727?source=friends_link&sk=a685a442c14a5fc7d8715a4d449a94cf

At this time, I will not be discussing the mathematical principles involved as they are more than fully presented in these articles. What I will provide is the JavaScript code I personally use for studying integer factoring and prime numbers. I will also provide instructions on how operate this software although I probably should apologize for any confusion that these instructions produce. (It’s not intentional… honest!)

Special thanks to D Howe for showing interest and support in my madness.

This article is part of the series called the Basics of Integer Factoring. If you view my About data, a list of all the other articles can be seen.

Update. The introduction of Anti-Keys can be found in these articles:

and

Chapter 2 — How To Install

Transfer the JavaScript code by highlighting the contents of the code block in Appendix 3 and performing a copy. Create a text file, i.e. “Something-or-other.txt” and paste into the text file. I call mine “Fast Fermat — Integer Family Factoring — JavaScript Code.” Now, rename the “.txt” file as “.html” and you are ready to perform various types of Integer Factoring. Just click on it and bring up the routine in your browser.

At this point there are several issues that should be addressed. First, there are trivial yet frustrating details that may need to be resolved. What if the display window is not the right size for your computer? If you are experienced with editing text files, then you will find the code TEXTAREA NAME=”TheReport” and change the values of ROWS and COLUMNS to values that work better for you. Remember, you may want to make a backup copy of the file before you do code surgery.

The “.html” is not a text file. How can I edit it? Rename the “.html” file as “.txt” and edit it. Then rename the “.txt” file back to “.html” and you are back in action.

What if I edited the program and it does not work right now? Switch to your backup copy or get a new version from the internet and delete the bad file.

Next, there are potentially serious problems. It is really easy to launch the software into a lengthy factoring loop, e.g. enter a number with 50 or more digits. (Pressing the [STOP] button will sometimes shutdown the process down, but it is not a guarantee.) Please familiarize yourself with how to shutdown rouge programs. Hint, search the internet for “how do I stop frozen programs?” If you do not mind waiting a couple of minutes, the browser may automatically give you an opportunity to stop. Probably the easiest thing to do is to just press the “X” on the tab, close the tab, and restart the program.

Finally, JavaScript requires being enabled on your browser or else nothing will work. Exactly how it gets turned off will probably be a mystery, but it will most likely be a consequence of multiple users (or cats) that use the computer. Once again, search the internet for “how do I enable JavaScript on my [name of browser] browser?”

Chapter 3 — How To Use

The JavaScript code has the main function of performing integer factoring. If you press the [RUN] button you will initiate a calculation routine. You may input a value of your choice or use the default value. If you want to create a compound number use the asterisk (*) to initiate multiplication of elements in the input. If you want to create a power of 2 + 1 trial, use the up arrow (^) followed by the exponent. Each time the input function runs, it will present a new default value. If you want a different default value, enter the plus sign (+). To repeat the last value, enter the minus sign (-). Use the [Cancel] button to stop the input process altogether.

Press the [HELP] button to get the help version that the program uses.

Press the [TEST] button to examine a range of numbers. In Test Mode, you can select the new default starting and ending value. Each time [RUN] executes, an updated default value will be presented. If you want to skip a default value, enter the plus sign (+). To repeat the last value, enter the minus sign (-). Use [Cancel] to stop the input process. Use [SETTINGS] options 2; 3; or 4 to exclude values that are undesired. For example turning off 2 and 3 will cause the test to only present prime and composite numbers.

Press the [RESTART] button to reload the program with the default settings. You can simply refresh the browser window as well.

Press the [EXIT] button to exit the program or just press the “X” on the tab to close the tab.

Press the [SETTINGS] button to bring up the settings menu.

Chapter 4 — Settings Options

Option D: Show Diagnostic Menu:

Allows viewing Working Data.

Option 0: Calculation Mode:

Allows changing the Factoring Method. (What is more boring than doing the same thing all the time?)

Option 1: Allow Big Integers:

Allows using numbers that can contain 100s of digits. This feature may not be available in all browsers. If available, this option can be reset during [RESTART].

Option 2: Allow Even Numbers:

Allows evaluation of numbers divisible by 2. If not active, [RUN] will reject these factors.

Option 3: Allow Numbers Divisible by Three:

Allows evaluation of numbers divisible by 3. If not active, [RUN] will reject these factors.

Option 4: Top Limit of Excluded Input Factors:

Excludes numbers divisible by values less than limit. [RUN] will reject these factors when encountered. When set to 1, all numbers are evaluated. Note that this feature can override Options 2 & 3. Use Diagnostic Menu to view All Excluded Input Factors.

Option 5: Running Test Mode:

Alternate manner to start and stop [TEST].

Option 6: Top Limit of PreCheck Factors:

PreCheck Factors force an incremental scan of input values prior to the regular factoring routines. When set to the value of 1, there is no precheck. For some factoring modes, there is a valid need to run a precheck because the factoring routines can not process some factors. When this occurs, a Special Value will override the default limit. Use Diagnostic Menu to view All PreCheck Factors.

Option 7: Use Automatic PreCheck:

Allows an Automated PreCheck that will reduce the factoring times for Fermat type routines.

Option 8: Scan for Square Roots:

Check for Square Roots at start of routine.

Option 9: Decompose All Factors:

Allows multiple searches to find all factors.

Option 10: Allow Generic Decomposition:

Reports all factors, including duplicates, if active. Otherwise, only reports all unique factors found. Not active when decomposition is turned off.

Option 11: Top Limit of Sieve Value:

Allows changing the size of the Factoring Sieve.

Option 12: Value of Overhead Factor:

Allows changing the size of the Overhead Factor. Some calculations may be too large to handle. If available, Big Integers will solve this problem, otherwise the Overhead Factor stops run-time errors. (Actually, I am not sure anything is using this anymore…)

Option 13: Value of Safe Trial Limit:

Allows changing the size of the Safe Trial Limit. This is the safe limit for the input value. Values above the limit may need a long time to run. Some calculations might even take years to run. Exceeding the Limit will generate a warning. You may always continue. If you change your mind, use the [STOP] button to escape the routine.

Option 14: Number of Iterations to Display:

Limits how many lines of output are displayed. It is not uncommon to have millions of iterations. Values above a few hundred can cause serious congestion.

Option 15: Number of Displays after Restarts:

Limits how many lines of output are displayed when decomposing all factors starts a new iteration.

Option 16: Seconds between Refresh Events:

The browsers I use do not like continuously running code. After a few minutes, a monitor will stop the execution. The routine can be continued, but it may kick out again. I do not think turning the monitor off is a good idea, so I use time loops that stop before the monitor acts. Then after a brief pause, the time loops restart. Changing this value may or may not speed up execution.

Chapter 5 — Functionality

When the primary function [RUN] executes, the user is required to input the value of an integer to exam. We will use the integer 4742594831699 (9901x479001599) for an example. It can be input as either 4742594831699 or 9901*479001599 depending on whether you want to solve the factorization or whether you want to validate the routine. The data output will have several extraneous lines which are mostly related to diagnostic functions, i.e. I do not trust the routines to work right and I want them to “show their work.” This is your routine now, so feel free to reduce the output as you see fit.

The actual factoring information is displayed at the bottom of each Loop. Several Loops may be required if the Decompose All Factors is active.

The summary data line may be either:

Translates as value 4742594831699 factors to 9901 times 479001599 in 237328001 total iterations requiring 550.634 seconds of total time. If running Enhanced Fermat or Fast Fermat, the “A” or “B” indicates whether this is the first Path or the second Path.

Abbreviations:

(A) If in mode Calculate Square Root, the output has only one Loop.

In this case we get two integer values, the square root above and the square root below the value we are factoring. The reason for this is that the actual square root is a real number, not an integer:

If you have the Big Integer function turned on you can try something a little more challenging: 1234567890123456789*1234567890123456789.

(B) If in mode Factoring Using Sieve, the output has 3 Loops. The first loop factors 4742594831699, the second Loop factors the intermediate factor 9901, and the third Loop factors the intermediate factor 479001599.

Technically this type of factoring is called Sieve of Eratosthenes. More information can be found at:

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

(C) If in mode Brute Force Factoring, the output again has 3 Loops. The first loop factors 4742594831699, the second Loop factors the intermediate factor 9901, and the third Loop factors the intermediate factor 479001599.

Technically this type of factoring is called Trial Division. More information can be found at:

https://en.wikipedia.org/wiki/Trial_division

(D) If in mode Classic Fermat Factoring, the output again has 3 Loops. The first loop factors 4742594831699, the second Loop factors the intermediate factor 9901, and the third Loop factors the intermediate factor 479001599.

Technically this type of factoring is called Fermat’s Factorization Method. More information can be found at:

https://en.wikipedia.org/wiki/Fermat's_factorization_method

(E) If in mode Enhanced Fermat with Family Factoring, the output now has 2 Paths and 6 Loops. The first path and loop can not factor 4742594831699, the second Path runs 5 Loops that factor the intermediate factors 9901 and 479001599 taking both pathways for each.

I do not have a reference for this type of factoring aside from my own essays. If there is documentation on-line where someone has already worked out this algorithm, please advise me. I will be happy to include proper references. (I will be equally happy to quit writing and simply read how this works. This is a tedious process to document!)

(F) If in mode Fast Fermat with Family Factoring, the output is again 2 Paths and 6 Loops. The first path and loop can not factor 4742594831699, the second Path runs 5 Loops that factor the intermediate factors 9901 and 479001599 taking both pathways for each.

Again, I do not have a reference for this type of factoring aside from my own essays.

For those of you that actually read this stuff, you will notice that the Fast Fermat is slower than the Factoring Using Sieve or the Brute Force Factoring. This is because when factoring numbers that have a small factor, e.g. 5x610943414095673, the value 5 will be an early trial in the non-Fermat procedures. The Fermat procedure works best when there is a small difference between the factors, e.g. 892618297x928088933.

Thus, for this example the Fast Fermat is much faster, .259 seconds compared to 39.177 seconds. If the concern is having an all around fast routine, it will require a hybrid factoring routine that uses both types of factoring. Turning on the Use Automatic PreCheck will do this in a limited capacity, but be aware the precheck has an arbitrary upper limit because the code is not optimized for this. Those that want a challenge are free to implement their own version.

Chapter 6 — Final Notes

One last note before I have to listen to horror stories of folks transcribing information from the report window or resorting to screen shots. Place the cursor in the report window and right click. Select the [Select All] and left click. The window should become highlighted with a background color like blue. Right click in the report window again. Select the [Copy] and left click. Create a text file, e.g. “Fast Fermat (4742594831699 = 9901 x 479001599).txt” and paste into the text file and save the file.

I expect that most people using this tool will have substantially faster processing times than I have posted here. I have an older PC.

As always, I hope that this essay and code provide someone some beneficial inspiration in their journey into the world of mathematics.

Appendix 1 — Standard Definitions and Equations

  • ^… — Raise to the Power of
  • […] — Square Root Function
  • An — Factor of Base Root Value: An ≥ √[N]
  • N’ — Integer Prime Number: N’ = N’×1 ≠ N
  • N — Composite Integer Factorization: N = Xn×Yn ≠ N’
  • N — Fermat’s Factorization: N = An² − Bn² = (An − Bn)(An + Bn)
  • N — Series Factorization: N = Si + 6 Sn = (Sa+ 6Sx)(Sb+ 6Sy)
  • Si — Series Identifier for Composite N: Si = 5 or 7
  • Sa — Series Identifier for Xn: Sa = 5 or 7
  • Sb — Series Identifier for Yn: Sb = 5 or 7
  • Xn — Smaller Integer Factor of N: Xn = An − Bn where 1 < Xn ≤ Yn
  • Yn — Larger Integer Factor of N: Yn = An + Bn where Yn ≥ Xn > 1

Appendix 2 — Abbreviations

Appendix 3 — Source Code Listing

--

--

JB Johnson
Nerd For Tech

I am a science and technology junky and this is my place where I can share my ideas.