双二次数是另一个整数的四次幂的数字,例如: 3^4 = 3*3*3*3 = 81
给定一个整数作为输入,输出最接近的二次数。
这是前15个双平方:
1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625
这是代码高尔夫球,因此每种语言中的最少字节数获胜
这是OEIS A000583
n^4并且n在符号上交替。
2 x n²数字:
双二次数是另一个整数的四次幂的数字,例如: 3^4 = 3*3*3*3 = 81
给定一个整数作为输入,输出最接近的二次数。
这是前15个双平方:
1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625
这是代码高尔夫球,因此每种语言中的最少字节数获胜
这是OEIS A000583
n^4并且n在符号上交替。
2 x n²数字:
Answers:
lambda n:int((n**.5-.75)**.5+.5)**4
输出从(k − 1)4切换到k 4的值n满足√(√n− 3/4)+ 1/2 = k或n =((k − 1/2)2 + 3 / 4)2 =((k 2 - k + 1)2 =((k -1)4 + k 4 +1)/ 2,恰好是更接近k 4的第一个整数。
(适用于所有Ñ ≤4504699340341245 =(8192 4 + 8193 4 - 7)/ 2> 2 52,在这之后的浮点舍入开始打破它,即使它的工作原理数学上对所有Ñ。)
round如果您切换到Python 2(可将所有.5向上取整),则可以保存一个字节。
这一挑战需要基于卷积的方法。
@(n)sum(n>conv((1:n).^4,[1 1]/2))^4
该表达式(1:n).^4产生行向量[1 16 81 256 ... n^4]。
然后将此向量与卷积进行卷积[1 1]/2,等效于计算大小块的滑动平均值2。隐式地假定该向量用左右填充0。所以结果中的第一个值是0.5(隐式0和的平均值1),第二个为8.5(1和的平均值16),依此类推。
例如,对于n = 9的结果conv((1:n).^4,[1 1]/2)是
0.5 8.5 48.5 168.5 440.5 960.5 1848.5 3248.5 5328.5 3280.5
然后比较n>...得出
1 1 0 0 0 0 0 0 0 0 0
并sum(...)给予给予2。这意味着在双二次数之间(包括附加的中点),n正好超过了2中点0.5。最后,^4提出这个4产生结果,16。
功能monad ftw!
f n=snd.minimum$(abs.(n-)<$>)>>=zip$(^4)<$>[1..n]
说明:
(^4)<$>[1..n] -- creates a list of fourth powers
(abs.(n-)<$>)>>=zip -- creates a list of |n-(4th powers)| and
-- zips it with the 4th powers list
minimum -- finds the minimum
-- (only first tuple entry matters)
snd -- exctracts the second entry (the 4th power)
=INT((A1^.5-3/4)^.5+.5)^4
Excel将其更新为 =INT((A1^0.5-3/4)^0.5+0.5)^4
Excel.ActiveSheet对象中获取输入是它们从单元格中获取的事实上的标准A1
R*4ạÐṂ
一个单子链接,返回一个项目的列表,或者一个打印结果的完整程序(使用效率低的方法)。
R*4ạÐṂ - Link: number, n
R - range(n) -> [1,2,3,...,n]
*4 - raise to the fourth power -> [1,16,81,...,n**4]
ÐṂ - filter keep those (only ever one) minimal:
ạ - absolute difference (with n)
- if a full program: implicit print (one item lists print their content).
C ++,96个字节
int Q(int N){int i=1;while (pow(i,4)<N){i++;}if (pow(i,4)-N>N-pow(i-1,4)){i--;}return pow(i,4);}
完整版:
int Q(int N)
{
int i = 1;
while (pow(i, 4) < N)
{
i++;
}
if (pow(i, 4)-N > N-pow(i - 1, 4))
i--;
return pow(i,4);
}
x。谢谢!
n=scan();which.min(((1:n)^4-n)^2)^4并且输入进入TIO的页脚部分。
{p=q^4~p>:|~p-a>a-o|_Xo\_Xp]\o=p┘q=q+1
{ DO infinitely
p=q^4 Set p to q quad (q starts out as 1)
~p>:| IF p exceeds the input THEN
~p-a>a-o check the distance to p and to o (the last quad) and
|_Xo PRINT o, or
\_Xp PRINT p accordingly
] END IF
\o=p ELSE ( p <= input) store p in o to keep track of this quad
┘q=q+1 and raise q for the next iteration
namespace System.Linq{n=>new int[940].Select((_,i)=>i*i*i*i).OrderBy(i=>Math.Abs(i-n)).First()}
我们使用940作为设置值,因为任何较大的值都会溢出int。
完整/格式化版本:
namespace System.Linq
{
class P
{
static void Main()
{
Func<int, int> f = n => new int[940].Select((_, i) => i * i * i * i).OrderBy(i => Math.Abs(i - n)).First();
for (int i = 1; i <= Int32.MaxValue; ++i)
Console.WriteLine($"{i} = {f(i)}");
Console.ReadLine();
}
}
}