/** * @function ShorsAlgorithm * @description Classical implementation of Shor's Algorithm. * @param {Integer} num - Find a non-trivial factor of this number. * @returns {Integer} - A non-trivial factor of num. * @see https://en.wikipedia.org/wiki/Shor%27s_algorithm * @see https://www.youtube.com/watch?v=lvTqbM5Dq4Q * * Shor's algorithm is a quantum algorithm for integer factorization. This * function implements a version of the algorithm which is computable using * a classical computer, but is not as efficient as the quantum algorithm. * * The algorithm basically consists of guessing a number g which may share * factors with our target number N, and then use Euclid's GCD algorithm to * find the common factor. * * The algorithm starts with a random guess for g, and then improves the * guess by using the fact that for two coprimes A and B, A^p = mB + 1. * For our purposes, this means that g^p = mN + 1. This mathematical * identity can be rearranged into (g^(p/2) + 1)(g^(p/2) - 1) = mN. * Provided that p/2 is an integer, and neither g^(p/2) + 1 nor g^(p/2) - 1 * are a multiple of N, either g^(p/2) + 1 or g^(p/2) - 1 must share a * factor with N, which can then be found