gpt4 book ai didi

algorithm - 将给定数字表示为四个平方和

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:03:48 26 4
gpt4 key购买 nike

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

例子

120 = 82 + 62 + 42 + 22
6 = 02 + 12 + 12 + 22
20 = 42 + 22 + 02+ 02

我的方法

取平方根并对余数重复此操作:

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

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

32 + 32+ 22 + 12

问题

  1. 还有其他算法可以做到这一点吗?

  2. 总是可行吗?

最佳答案

###总是可能的?

是的,Lagrange's four square theorem指出:

every natural number can be represented as the sum of four integer squares.

已经从几个方面证明了这一点。

###算法

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

将数字分解为质因数。它们不一定是质数,但它们越小越好:所以质数是最好的。然后解决以下每个因素的任务,并将任何生成的 4 个方 block 与先前找到的 4 个方 block 与 Euler's four-square identity 组合起来。 .

(a2 + b2 + c2 + d2)(A2 + B2 + C2 + D2) =
(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 的三个平方和: 这是可能的,当且仅当此数字不是以下形式时:

4a(8b+7)

If this square is not found suitable, try the next smaller one, ... until you find one. It guaranteed there will be one, and most are found within a few retries.
  1. 尝试以与步骤 1 相同的方式找到实际的第二个平方项,但现在使用 Fermat's theorem on sums of two squares 测试其可行性这在扩展意味着:

    if all the prime factors of n congruent to 3 modulo 4 occur to an even exponent, then n is expressible as a sum of two squares. The converse also holds.

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

  1. 现在我们在减去两个正方形后得到余数。尝试减去第三个方 block ,直到产生另一个方 block ,这意味着我们有一个解决方案。可以通过首先分解出最大的平方除数来改进此步骤。然后,当确定两个平方项时,每个平方项都可以再次乘以该平方除数的平方根。

大致思路是这样的。为了找到主要因素,有 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 + 1 - (range % 2); 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可能代表了一种更省时的方法,但文本更像是一种证明而不是一种算法。但是,如果您需要更高的时间效率,您可以考虑将其与更高效的因式分解算法一起考虑。

关于algorithm - 将给定数字表示为四个平方和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com