将序列呈现为 2 个正方形的总和
Present Sequence as a sum of 2 squares
我正在解决 CodeWars task 并遇到了问题。
其中给出了一个数字数组,其长度是 4 的倍数,您需要将其可视化为 (x1^2 + x2^2
) * (x3^2 + x4^2
) .... * (xn^2 + xn+1^2
).
计算这个结果,找到2个数,平方和,给出初始序列的结果。
例如,给定一个数组 ( 2, 1, 3, 4):
(2^2 + 1^2) * (3^2 + 4^2) = 125;
2 个数,平方加起来等于 125,是 2 和 11 因为 4 + 121 = 125;
我编写了代码并且它适用于大多数示例,但是当我使用大数组时,例如
(3, 9, 8, 4, 6, 8, 7, 8, 4, 8, 5, 6, 6, 4, 4, 5)
结果我收到 (0,0);
我无法解决问题,请帮助我,如果你可以使用简体英语,因为我来自俄罗斯。
这是我的代码:
using System;
using System.Numerics;
using System.Collections.Generic;
public class ProdSeq
{
public static BigInteger[] solve(int[] arr)
{
bool simplified = false;
var result = new BigInteger[2];
var index = 0;
BigInteger sequenceSum = 1;
for (int i = 0; i < arr.Length - 1; i+=2)
sequenceSum *= arr[i] * arr[i] + arr[i + 1] * arr[i + 1];
if (sequenceSum >= 1000000)
{
sequenceSum /= 10000;
simplified = true;
}
var list = new List<BigInteger>();
for (BigInteger i = 0; i <= (BigInteger)Math.Sqrt((double)sequenceSum + 1); i++)
list.Add(BigInteger.Multiply(i, i));
for (int i = 0; i < list.Count; i++)
{
var second = sequenceSum - list[i];
index = list.BinarySearch(second);
if (index > -1)
{
if (simplified)
{
result[0] = (BigInteger)(Math.Sqrt((double)list[i]) * 100);
result[1] = (BigInteger)(Math.Sqrt((double)list[index]) * 100);
break;
}
result[0] = (BigInteger)(Math.Sqrt((double)list[i]));
result[1] = (BigInteger)(Math.Sqrt((double)list[index]));
break;
}
}
Console.WriteLine($"A: {result[0]} B: {result[1]}");
return result;
}
}
您的错误似乎来自这一行:
for (int i = 0; i <= (int)Math.Sqrt((double)sequenceSum + 1); i++)
list.Add(BigInteger.Multiply(i , i));
你必须使用Biginteger.Multiply方法
result = 302400 and 29092800
(我认为有很多解决方案)
我正在解决 CodeWars task 并遇到了问题。
其中给出了一个数字数组,其长度是 4 的倍数,您需要将其可视化为 (x1^2 + x2^2
) * (x3^2 + x4^2
) .... * (xn^2 + xn+1^2
).
计算这个结果,找到2个数,平方和,给出初始序列的结果。
例如,给定一个数组 ( 2, 1, 3, 4):
(2^2 + 1^2) * (3^2 + 4^2) = 125;
2 个数,平方加起来等于 125,是 2 和 11 因为 4 + 121 = 125;
我编写了代码并且它适用于大多数示例,但是当我使用大数组时,例如
(3, 9, 8, 4, 6, 8, 7, 8, 4, 8, 5, 6, 6, 4, 4, 5)
结果我收到 (0,0);
我无法解决问题,请帮助我,如果你可以使用简体英语,因为我来自俄罗斯。 这是我的代码:
using System;
using System.Numerics;
using System.Collections.Generic;
public class ProdSeq
{
public static BigInteger[] solve(int[] arr)
{
bool simplified = false;
var result = new BigInteger[2];
var index = 0;
BigInteger sequenceSum = 1;
for (int i = 0; i < arr.Length - 1; i+=2)
sequenceSum *= arr[i] * arr[i] + arr[i + 1] * arr[i + 1];
if (sequenceSum >= 1000000)
{
sequenceSum /= 10000;
simplified = true;
}
var list = new List<BigInteger>();
for (BigInteger i = 0; i <= (BigInteger)Math.Sqrt((double)sequenceSum + 1); i++)
list.Add(BigInteger.Multiply(i, i));
for (int i = 0; i < list.Count; i++)
{
var second = sequenceSum - list[i];
index = list.BinarySearch(second);
if (index > -1)
{
if (simplified)
{
result[0] = (BigInteger)(Math.Sqrt((double)list[i]) * 100);
result[1] = (BigInteger)(Math.Sqrt((double)list[index]) * 100);
break;
}
result[0] = (BigInteger)(Math.Sqrt((double)list[i]));
result[1] = (BigInteger)(Math.Sqrt((double)list[index]));
break;
}
}
Console.WriteLine($"A: {result[0]} B: {result[1]}");
return result;
}
}
您的错误似乎来自这一行:
for (int i = 0; i <= (int)Math.Sqrt((double)sequenceSum + 1); i++)
list.Add(BigInteger.Multiply(i , i));
你必须使用Biginteger.Multiply方法
result = 302400 and 29092800
(我认为有很多解决方案)