输入和下一个最高平方之间的值数


9

给定正平方数作为输入。输出输入和下一个最高平方之间的值数。

输入1

输出2

原因:数字2和3在1和4之间,下一个最高的正方形

输入4

输出4

原因:数字5、6、7、8在4到9之间


1
我们必须支持什么范围的输入值?
Martin Ender

16
我认为如果输入不必是正方形的话,这会更有趣。
xnor

1
@xnor后见之明,我绝对同意。
Shayne03

Answers:


8

果冻,2个字节

½Ḥ

在线尝试!

我的Mathematica答案的端口(取平方根,然后加倍)。这仅限于可以精确表示为浮点数的输入。如果存在问题,则三字节的解决方案ƽḤ适用于任意正方形(Dennis首先发布,然后删除了)。


1
哦,我错过了整个“输入将是平方”哦。
乔纳森·艾伦

1
@JonathanAllan我也是。IMO怪异的规格。
Digital Trauma

是否存在无法完全在浮点数中表示的正方形?
散布

@Christian当然,浮点数的大小是固定的,因此它们只能表示有限数量的值。
Martin Ender

@MartinEnder在这种情况下,考虑到Jelly对任意精度整数的支持以及该规范缺乏上限,我投票决定它应该支持所有有效输入。
分散

12

脑高射炮38,22个字节

{([[]](({})))}{}([]<>)

在线尝试!

我为这个答案感到非常自豪。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))

2
我的大脑确实被这项出色的工作吓坏了。
Magic Octopus Urn

9

Mathematica,8个字节

2Sqrt@#&

在线尝试!(使用数学。)

n 2(n + 1)2之间的差始终为2n + 1,但我们只希望它们之间的值(不包括两端)为2n

可以2#^.5&根据精度要求将其缩短。


1
2√#&怎么样?
chyanog





2

Brain-Flak,20字节

在这里大声疾呼DJMcMayhem的惊人(albiet稍长)答案

{({}()[({}()())])}{}

在线尝试!

说明

该代码通过以奇数递增从平方数开始递减计数的方式工作。由于每个平方都是连续奇数的总和,因此它将以n 1/2步长达到0 。这里的诀窍是我们实际上以偶数跟踪我们的步骤,并使用静态值()将其偏移为适当的奇数。由于答案是2n 1/2,所以这个偶数就是我们的答案。因此,当我们达到0时,我们删除了零,而我们的答案就坐在堆栈上。




1

果冻,7个字节

½‘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

顺便说一句,输入本身将始终是一个正方形。
Martin Ender

1
@JonathanAllan固定
散布

@MartinEnder我完全误解了挑战,然后……为了不复制您的答案(因为现在很明显为什么这样做了),我将保留这个答案。
散布




1

TI基本,3字节

2√(Ans

最简单的方法...



1

加++22 20字节

+?
_
S
+1
^2
-1
-G
O

在线尝试!

您想知道它是如何工作的吗?好吧,不要害怕!我是来教育你的!

+?   Add the input to x (the accumulator)
_    Store the input in the input list
S    Square root
+1   Add 1
^2   Square
-1   Subtract 1
-G   Subtract the input
O    Output as number

对于最初的QBIC回答,我有相同的逻辑,但方法更短
steenbergh

1

MATL(8 7字节)

我敢肯定,这可以大大减少(编辑:谢谢路易斯),但是一个幼稚的解决方案是:

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.

1
您可以替换2^U(并且在20.1.1版中有效,该版本是挑战时的最新版本,因此即使按照我们的旧标准,也可以使用该答案)
Luis Mendo

1
谢谢路易斯!令我惊讶的是,相对于MATL管理员,我的幼稚方法只浪费了1个字符。:)
DrQuarius



0

爱丽丝,10字节

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.


0

QBIC19 9字节

?sqr(:)*2

通过复制@MartinEnder的方法来保存一堆。

不幸的是,没有QBIC的TIO链接。

说明

?          PRINT
 sqr( )    The square root of
     :     the input
        *2 doubled



0

视网膜,21字节

.+
$*
(^1?|11\1)+
$1

在线尝试!说明:通过基于@MartinEnder的三角数求解器取数字的平方根来工作。匹配平方数后,$1是平方数与前一个平方数之间的差(以一进制表示)。我们想要下一个差异,但排他,仅是另外一个。为此,我们在中计算了空字符串的数量$1





By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.