将给定数字表示为四个方格的总和

时间:2017-01-07 17:45:56

标签: algorithm math

我正在寻找一种算法,将给定数字表示为(最多)四个方格的总和。

实施例

120 = 8 2 + 6 2 + 4 2 + 2 2
6 = 0 2 + 1 2 + 1 2 + 2 2
20 = 4 2 + 2 2 + 0 2 + 0 2

我的方法

取平方根并重复这个以取下余数:

while (count != 4) {
    root = (int) Math.sqrt(N)
    N -= root * root
    count++
} 

但是当 N 为23时,即使有解决方案,这也会失败:

3 2 + 3 2 +2 2 + 1 2

问题

  1. 还有其他算法吗?

  2. 总是可以吗?

3 个答案:

答案 0 :(得分:11)

总是可以吗?

是的,Lagrange's four square theorem表示:

  

每个自然数可以表示为四个整数平方的总和。

已经通过多种方式证明了这一点。

算法

有一些更智能的算法,但我建议使用以下算法:

将数字分解为素因子。它们不一定是素数,但它们越小越好:所以素数最好。然后解决以下每个因素的任务,并将任意结果的4个方格与先前找到的4个方格与Euler's four-square identity组合。

(a 2 + b 2 + c 2 + d 2 ) (A 2 + B 2 + C 2 + D 2 )=
(aA + bB + cC + dD) 2 +
(aB-bA + cD-dC) 2 +
(aC - bD - cA + dB) 2 +
(aD + bC - cB - dA) 2

  1. 给定数字 n (上面提到的因素之一),获得不大于 n 的最大正方形,并查看是否 n 减去这个方格可以用Legendre's three-square theorem写成三个方格的总和:当且仅当这个数字不是以下形式时,它是可能的:

    4 (8B + 7)

    如果找不到合适的方格,请尝试下一个较小的方格......直到找到一个方格。它保证会有一个,大多数都会在几次重试中找到。

  2. 尝试以与步骤1中相同的方式查找实际的第二个平方项,但现在使用Fermat's theorem on sums of two squares测试其可行性,扩展意味着:

      

    如果 n 的所有素因子与3模4一致,则偶数指数出现,则 n 可表示为两个平方的和。反过来也有。

    如果找不到合适的方格,请尝试下一个较小的方格......直到找到一个方格。它保证会有一个。

  3. 现在我们在减去两个方格后有一个余数。尝试减去第三个方格,直到产生另一个方格,这意味着我们有一个解决方案。通过首先分解最大的平方除数可以改善该步骤。然后,当识别出两个平方项时,每个平方项再次乘以该平方除数的平方根。

  4. 这大致是个主意。为了找到素因子,有several solutions。下面我将使用Sieve of Eratosthenes

    这是JavaScript代码,因此您可以立即运行它 - 它将生成一个随机数作为输入并将其显示为四个方块的总和:

    function divisor(n, factor) {
        var divisor = 1;
        while (n % factor == 0) {
            n = n / factor;
            divisor = divisor * factor;
        }
        return divisor;
    }
    
    function getPrimesUntil(n) {
        // Prime sieve algorithm
        var range = Math.floor(Math.sqrt(n)) + 1;
        var isPrime = Array(n).fill(1);
        var primes = [2];
        for (var m = 3; m < range; m += 2) {
            if (isPrime[m]) {
                primes.push(m);
                for (var k = m * m; k <= n; k += m) {
                    isPrime[k] = 0;
                }
            }
        }
        for (var m = range; m <= n; m += 2) {
            if (isPrime[m]) primes.push(m);
        }
        return {
            primes: primes,
            factorize: function (n) {
                var p, count, primeFactors;
                // Trial division algorithm
                if (n < 2) return [];
                primeFactors = [];
                for (p of this.primes) {
                    count = 0;
                    while (n % p == 0) {
                        count++;
                        n /= p;
                    }
                    if (count) primeFactors.push({value: p, count: count});
                }
                if (n > 1) {
                    primeFactors.push({value: n, count: 1});
                }
                return primeFactors;
            }
        }
    }
    
    function squareTerms4(n) {
        var n1, n2, n3, n4, sq, sq1, sq2, sq3, sq4, primes, factors, f, f3, factors3, ok,
            res1, res2, res3, res4;
        primes = getPrimesUntil(n);
        factors = primes.factorize(n);
        res1 = n > 0 ? 1 : 0;
        res2 = res3 = res4 = 0;
        for (f of factors) { // For each of the factors:
            n1 = f.value;
            // 1. Find a suitable first square
            for (sq1 = Math.floor(Math.sqrt(n1)); sq1>0; sq1--) {
                n2 = n1 - sq1*sq1;
                // A number can be written as a sum of three squares
                // <==> it is NOT of the form 4^a(8b+7)
                if ( (n2 / divisor(n2, 4)) % 8 !== 7 ) break; // found a possibility
            }
            // 2. Find a suitable second square
            for (sq2 = Math.floor(Math.sqrt(n2)); sq2>0; sq2--) {
                n3 = n2 - sq2*sq2;
                // A number can be written as a sum of two squares
                // <==> all its prime factors of the form 4a+3 have an even exponent
                factors3 = primes.factorize(n3);
                ok = true;
                for (f3 of factors3) {
                    ok = (f3.value % 4 != 3) || (f3.count % 2 == 0);
                    if (!ok) break;
                }
                if (ok) break;
            }
            // To save time: extract the largest square divisor from the previous factorisation:
            sq = 1;
            for (f3 of factors3) {
                sq *= Math.pow(f3.value, (f3.count - f3.count % 2) / 2); 
                f3.count = f3.count % 2;
            }
            n3 /= sq*sq;
            // 3. Find a suitable third square
            sq4 = 0;
            // b. Find square for the remaining value:
            for (sq3 = Math.floor(Math.sqrt(n3)); sq3>0; sq3--) {
                n4 = n3 - sq3*sq3;
                // See if this yields a sum of two squares:
                sq4 = Math.floor(Math.sqrt(n4));
                if (n4 == sq4*sq4) break; // YES!
            }
            // Incorporate the square divisor back into the step-3 result:
            sq3 *= sq;
            sq4 *= sq;
            // 4. Merge this quadruple of squares with any previous 
            // quadruple we had, using the Euler square identity:
            while (f.count--) {
                [res1, res2, res3, res4] = [
                    Math.abs(res1*sq1 + res2*sq2 + res3*sq3 + res4*sq4),
                    Math.abs(res1*sq2 - res2*sq1 + res3*sq4 - res4*sq3),
                    Math.abs(res1*sq3 - res2*sq4 - res3*sq1 + res4*sq2),
                    Math.abs(res1*sq4 + res2*sq3 - res3*sq2 - res4*sq1)
                ];
            }
        }
        // Return the 4 squares in descending order (for convenience):
        return [res1, res2, res3, res4].sort( (a,b) => b-a );
    }
    
    // Produce the result for some random input number
    var n = Math.floor(Math.random() * 1000000);
    var solution = squareTerms4(n);
    // Perform the sum of squares to see it is correct:
    var check = solution.reduce( (a,b) => a+b*b, 0 );
    if (check !== n) throw "FAILURE: difference " + n + " - " + check;
    // Print the result
    console.log(n + ' = ' + solution.map( x => x+'²' ).join(' + '));

    The article by by Michael Barr on the subject可能代表了一种更具时间效率的方法,但文本更多的是作为证明而不是算法。但是,如果您需要更高的时间效率,可以考虑使用更高效的因子分解算法。

答案 1 :(得分:2)

它始终是可能的 - 它是数论中的一个定理,叫做#34;拉格朗日的四方定理。&#34;

要有效地解决它:论文 Randomized algorithms in number theory (Rabin, Shallit) 提供了一个在预期的O((log n)^ 2)时间内运行的方法。

这里有关于实施的有趣讨论:https://math.stackexchange.com/questions/483101/rabin-and-shallit-algorithm

通过Wikipedia:Langrange's four square theorem找到。

答案 2 :(得分:0)

这是解决方案,简单4循环

max = square_root(N)
for(int i=0;i<=max;i++)
  for(int j=0;j<=max;j++)
     for(int k=0;k<=max;k++)
          for(int l=0;l<=max;l++)
              if(i*i+j*j+k*k+l*l==N){
                        found
                       break;
                }

所以你可以测试任何数字。如果总和超过则可以在两个循环后使用中断条件然后将其中断。

相关问题