他要完成N个工作单元最少需要多少天?


10

一个人必须完成N工作单元;工作的性质是相同的。

为了控制工作,第一天他只完成一个工作单元

他希望庆祝工作的完成,因此他决定在最后一天完成一个工作单元

他只被允许完成xx+1x-1在一天的工作单位,在那里x是工作完成前一天的单位。

您的任务是创建一个程序或函数,以计算完成工作单元所需的最少天数N

样本输入和输出:

input -> output (corresponding work_per_day table)
-1    -> 0      []
0     -> 0      []
2     -> 2      [1,1]
3     -> 3      [1,1,1]
5     -> 4      [1,1,2,1] or [1,2,1,1]
9     -> 5      [1,2,3,2,1]
13    -> 7      [1,2,2,2,3,2,1]

输入可以通过STDIN或作为函数参数,或以任何适当的方式进行。

输出可以被打印或作为功能的结果,或以任何适当的方式打印。

这是。最短的解决方案获胜。


1
提示:此整数列表可能会有所帮助。
Leaky Nun

1
那么,既然肯尼证明输入有可能达到负功数,输入是否仅限于正整数?还是每天的工作量限制为零?
mbomb007 '16

1
您为什么接受Pyth的答案?我的果冻答案短了3个字节...
Dennis

嘿,@ Dennis我需要了解这种方法,而@Kenny Lau可以帮助我理解它。
HarshGiri '16

我是CodeGolf的新手,所以这里需要一些时间才能全面了解所有内容。
HarshGiri '16

Answers:


3

果冻,5个字节

×4’½Ḟ

这使用@LeakyNun方法的封闭形式。

在线尝试!

由于幸运的巧合,对于实数/复数,将重载为floor/ real。这是果冻中仅有的三个重载原子之一。

怎么运行的

×4’½Ḟ  Main link. Argument: n (integer)

×4     Compute 4n.
  ’    Decrement; yield 4n - 1.
   ½   Square root; yield sqrt(4n - 1).
       If n < 2, this produces an imaginary number.
    Ḟ  If sqrt(4n - 1) is real, round it down to the nearest integer.
       If sqrt(4n - 1) is complex, compute its real part (0).

1
一个并不简单……
Leaky Nun

1
“幸运的巧合”
Arcturus

4

Pyth,8个字节

tfg/*TT4

怎么运行的:

tfg/*TT4   Q is implicitly assigned to the input.
 f         test for T=1,2,3,... returning the first successful case
   /*TT4   whether T * T / 4
  g     Q  is greater than or equal to the input (second argument implied)
t          and subtract 1 from the first successful case

在线尝试!

用伪代码:

for(int T=1;;T++)
    if(T*T/4 >= Q)
        return T-1;

红利,22字节

“应该为-1返回7”

+tfg/*TT4?>Q0Q-2Q1*4g1

在线尝试!


3

JavaScript(ES2016),24个字节

由于@FlorentExponentiation Operator(目前仅在Firefox夜间版本或编译器中),因此以下是ES6变体的缩短版本。

n=>(n-1)**.5+(n+1)**.5|0

JavaScript(ES6),30个字节

n=>(s=Math.sqrt)(n-1)+s(n+1)|0

基于此顺序

f=n=>(s=Math.sqrt)(n-1)+s(n+1)|0

units.oninput = () => output.value = f(+units.value||0);
<label>Units: <input id="units" type="number" value="0" /></label>
<label>Days: <input id="output" type="number" value="0" disabled /></label>


在ES2016中甚至更短(26个字符):f=n=>(n-1)**.5+(n+1)**.5|0
Florent

@Florent Wow谢谢,没有意识到即将推出的幂运算符。
乔治·瑞斯

2

JavaScript, 32 31个字节

f=(q,t=1)=>q>t*t/4?f(q,t+1):t-1

取消程式码:

function f(q, t = 1) {
  return q > t * t / 4
    ? f(q, t + 1)
    : t - 1
}

它使用与Kenny Lau的anwser相同的算法,但是它被实现为递归闭包以节省一些字节。

用法:

f(-1)  // 0
f(0)   // 0
f(2)   // 2
f(3)   // 3
f(5)   // 4
f(9)   // 5
f(13)  // 7

REPL解决方案,23字节

for(t=1;t*t++/4<q;);t-2

预先q=运行代码段:

q=-1;for(t=1;t*t++/4<q;);t-2 // 0
q=9;for(t=1;t*t++/4<q;);t-2  // 5
q=13;for(t=1;t*t++/4<q;);t-2 // 7

它甚至使用与我的变量名称相同的变量名:)
Leaky Nun

可以通过打开保存一个字节>=<d:
漏尼姑

@KennyLau谢谢!自从我没有打高尔夫球以来已经有很长时间了。我有点生锈x)
佛罗伦萨(Florent

for(t=1;;)if(t*t++/4>=q)return t-1;只有36个字节:)
Leaky Nun

1
@KennyLau我添加了23个字节的解决方案:)
Florent

2

Python,28个字节

lambda n:max(4*n-1,0)**.5//1

输出浮点数。的max是那里给0用于n<=0同时避免负的平方根误差。


2

UGL30 25字节

i$+$+dc^l_u^^$*%/%_c=:_do

在线尝试!

不适用于负输入。

怎么运行的:

i$+$+dc^l_u^^$*%/%_c=:_do
i$+$+d                     #n = 4*input-1
      c                    #i=0
       ^l_     %/%_c=:_    #while      > n:
           ^^$*            #      i**2
          u                #                i = i+1
                       do  #print(i)

以前的30字节解决方案:

iuc^l_u^^$*cuuuu/%_u%/%_c=:_do

在线翻译在这里

不适用于负输入。

怎么运行的:

iuc^l_u^^$*cuuuu/%_u%/%_c=:_do
iuc                             #push input; inc; i=0;
   ^l_u             %/%_c=:_    #while        > input:
       ^^$*cuuuu/%_             #      i**2/4
                   u            #                      i = i+1
                            do  #print(i)

1

MATL,11个字节

E:t*4/G<f0)

与@KennyLau类似的算法,除了不是无限循环,我从1 ... 2n循环以节省一些字节。

在线尝试!

说明

    % Implicitly grab the input
E   % Double the input
:   % Create an array from 1...2n
t*  % Square each element
4/  % Divide each element by 4
G<  % Test if each element is less than G
f   % Get the indices of the TRUE elements in the array from the previous operation
0)  % Get the last index (the first index where T*T/4 >= n)
    % Implicitly display the result.

@LuisMendo感谢您指出这一点。更新!
Suever 2016年


0

Python,43个字节

f=lambda n,i=1:i-1if i*i>=n*4 else f(n,i+1)

1
可以使用<代替> =来保存一个字节
Leaky Nun

0

Java 8,30 24字节

n->(int)Math.sqrt(n*4-1)

在线尝试。

无需检查是否n大于0,因为Java的负输入Math.sqrt返回NaN,这0int我们已经用于正输入的转换成正比。


0

Ruby,30个字节

->n{n<1?0:((4*n-1)**0.5).to_i}

在线尝试!

.to_i代替来保存一个字节.floor

支持非肯定量的工作需要花费6个字节(n<1?0:)。

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.