Answers:
½Ḥ
我的Mathematica答案的端口(取平方根,然后加倍)。这仅限于可以精确表示为浮点数的输入。如果存在问题,则三字节的解决方案ƽḤ适用于任意正方形(Dennis首先发布,然后删除了)。
{([[]](({})))}{}([]<>)
我为这个答案感到非常自豪。IMO,这是我最好的一次高飞。
正如许多其他用户指出的那样,答案只是sqrt(n)* 2。但是,计算脑筋的平方根是非常不平凡的。由于我们知道输入将始终是正方形,因此可以进行优化。所以我们写一个循环,减去
1, 3, 5, 7, 9...
从输入中,并跟踪它运行了多少次。一旦达到0,答案就是我们减去的最后一个数字减一。
最初,我将计数器推到另一个堆栈上。但是,我们可以通过增加堆栈高度来将主堆栈本身用作计数器。
#While TOS (top of stack, e.g. input) != 0:
{
#Push:
(
#The negative of the height of the stack (since we're subtracting)
[[]]
#Plus the TOS pushed twice. This is like incrementing a counter by two
(({}))
)
#Endwhile
}
#Pop one value off the main stack (or in other words, decrement our stack-counter)
{}
#And push the height of the stack onto the alternate stack
([]<>)
在python-y伪代码中,这基本上是以下算法:
l = [input]
while l[-1] != 0: #While the back of the list is nonzero
old_len = len(l)
l.append(l[-1])
l.append(l[-1] - old_len)
l.pop()
print(len(l))
在这里大声疾呼DJMcMayhem的惊人(albiet稍长)答案
{({}()[({}()())])}{}
该代码通过以奇数递增从平方数开始递减计数的方式工作。由于每个平方都是连续奇数的总和,因此它将以n 1/2步长达到0 。这里的诀窍是我们实际上以偶数跟踪我们的步骤,并使用静态值()将其偏移为适当的奇数。由于答案是2n 1/2,所以这个偶数就是我们的答案。因此,当我们达到0时,我们删除了零,而我们的答案就坐在堆栈上。
½‘R²Ṫ_‘
说明:
½‘R²Ṫ_ Input: 40
½ Square root 6.32455532...
‘ Increment 7.32455532...
R Range [1, 2, 3, 4, 5, 6, 7]
² Square [1, 4, 9, 16, 25, 36, 49]
Ṫ Tail 49
_‘ Subtract input+1 8
我敢肯定,这可以大大减少(编辑:谢谢路易斯),但是一个幼稚的解决方案是:
X^QUG-q
X^ % Take the square root of the input (an integer)
QU % Square the next integer to find the next square
G- % Subtract the input to find the difference
q % Decrement solution by 1 to count only "in between" values.
2/*<ER
o@i
同样,计算2 sqrt(n)。与标准解决方案相比,该布局节省了两个字节:
/o
\i@/2RE2*
代码细分,不包括IP重定向:
2 Push 2 for later.
i Read all input.
i Try reading more input, pushes "".
2 Push 2.
R Negate to get -2.
E Implicitly discard the empty string and convert the input to an integer.
Then take the square root of the input. E is usually exponentiation, but
negative exponents are fairly useless in a language that only understands
integers, so negative exponents are interpreted as roots instead.
* Multiply the square root by 2.
o Output the result.
@ Terminate the program.
n->2*Math.sqrt(n)
注意:这需要import java.util.function.*;进入IntFunction<T>Java 8或Java 9,但是java.util.function默认情况下该包是在JShell中导入的。