范围取整数的序列


16

考虑一个三角形,其中Ñ个行(1-索引)是第一阵列Ñ的正整数幂Ñ。这是前几行:

N | 三角形

1 | 1个
2 | 2 4
3 | 3 9 27
4 | 4 16 64 256
5 | 5 25 125 625 3125
...

现在,如果将这些能力合并为一个序列,我们将获得OEIS A075363:

1, 2, 4, 3, 9, 27, 4, 16, 64, 256, 5, 25, 125, 625, 3125, 6, 36, 216, 1296, 7776, 46656 ...

给定整数N,您的任务是返回此序列的第N个项。您可以选择0或1索引。

测试用例

1索引:

N->输出

1-> 1
2-> 2
3-> 4
5-> 9
10-> 256
12-> 25
15-> 3125

0索引:

N->输出

0-> 1
1-> 2
2-> 4
4-> 9
9-> 256
11-> 25
14-> 3125

请注意,默认情况下,这些漏洞是禁止的。这是,因此赢得每种语言中最短的有效提交!


我认为测试用例中存在一些错误:在1索引中10应该是256;在1索引中应该是256。在0索引9应为256
— 盖伦诺夫

我们可以有尾随空格吗?
— Stan Strum,

@StanStrum是的。
— Xcoder先生17年

Answers:



7

外壳,7个字节

!ṁṠM^ḣN

在线尝试!

1索引

说明:

      N   Get the list of all natural numbers
 ṁ        Map over each n in that list and then concatenate
  Ṡ  ḣ      Create the range [1,n] then ...
   M^       raise n to the power of each
!         Index into that sequence



4

APL(Dyalog),15 13 10字节

@Adám节省了3个字节

⊢⊃∘∊⍳*⍳¨∘⍳

在线尝试!

怎么样?

⍳¨∘⍳ -为输入范围内的每个数字创建一个范围

⍳* -将输入范围内的每个数字提高到相应的幂

∊ -展平

⊢⊃ -选择第n个元素


在线尝试使用该代码似乎只有17个字节,并且似乎没有接受标量整数参数或产生单个整数输出
— Graham

1
@Graham Online,将代码分配给了一个变量f(因此f←还有2个字节,这里不计算在内),并且有一个测试工具将结果从返回1到10。
— 越野选手埃里克(Erik the Outgolfer)'17年

当然,您必须计算所有字节,包括任何此类分配,以及根据问题规范接受输入并根据规范输出所需的字节。我的APL回答提示输入屏幕。
— Graham

@Graham这是dyalog APL dfns。它不需要将分配应用于任何输入,TIO插座仅用于舒适观看
— Uriel

在此基础上,我可以假设在运行APL之前可以在工作区中分配n,从而节省7个字节,我不确定竞争对手是否会接受。
— 格雷厄姆


3

Haskell,30 28字节

多亏了xnor,节省了2个字节。

([n^m|n<-[1..],m<-[1..n]]!!)

在线尝试!

0索引


1
使用list comp实际上要短一些[n^i|n<-[1..],i<-[1..n]]。
— xnor

哦,我正在将其与>>=表格进行比较...
— H.PWiz

3

玛特,9字节

:t!^RXzG)

索引基于1。在线尝试!或验证所有测试用例。

说明

以输入5为例。

:     % Implcit input n. Push range [1 2 ... n]
      % STACK: [1 2 3 4 5]
t!^   % Matrix of all pair-wise powers
      % STACK: [1    2    3    4    5;
                1    4    9   16   25;
                1    8   27   64  125;
                1   16   81  256  625;
                1   32  243 1024 3125]
R     % Upper triangular matrix
      % STACK: [1    2    3    4    5;
                0    4    9   16   25;
                0    0   27   64  125;
                0    0    0  256  625;
                0    0    0    0 3125]
Xz    % Nonzeros. Reads values in column-major order
      % STACK: [1; 2; 4; 3; 9; ...; 625; 3125]
G)    % Get n-th entry (1-based). Implcit display
      % STACK: 9

3

APL(Dyalog),14 12字节

{⍵⌷∊*∘⍳⍨¨⍳⍵}

在线尝试!

使用1索引

与一起保存了2个字节↑,/ → ∊,摘自Graham的答案

请注意,在测试链接中,代码需要额外的f←,但这并不是我们的规则所计算的。


∘与配合使用非常巧妙⍨。
— 亚当

@Adám谢谢:-)
— H.PWiz

{⍵⌷∊*∘⍳⍨¨⍳⍵}→交通⊢⌷∘∊((*∘⍳)⍨¨⍳)→交通⊢⌷∘∊(⍳(*∘⍳)¨⍳)→交通⊢⌷∘∊⍳*∘⍳¨⍳
— 亚当

我会发布...如果我有任何想法的话
— H.PWiz

⊢对于默认功能,⍵对于dfn。该∘之间⌷和∊被需要的,因为∊被称为monadically,所以它是指数入伍。我们改变f⍨⍳为⍳ f ⍳避免调用F(*∘⍳¨monadically)(只要⍨和¨是相邻的,他们可能会互换位置)。
— 亚当


2

05AB1E,9个字节

ƒNDLm`}I@

在线尝试!

说明

1个索引。

ƒ           # for N in range [0 ... input]
 N          # push N
  DL        # push range [1 ... N]
    m       # raise N to the power of each in [1 ... N]
     `      # flatten to stack
      }     # end loop
       I@   # get the element at index (input)

列表而不是循环的替代解决方案

ÝεDLm}˜sè

1

Perl 6,29个字节

{({|($++X**1..$++)}...*)[$_]}

测试一下

展开:

{  # bare block lambda with implicit parameter 「$_」

  (  # generate the sequence

    {  # code block used to generate each value in the sequence

      |(         # slip the values into the outer sequence

        $++      # post-incremented anonymous state value
        X**      # cross using &infix:«**»
        1 .. $++ # from 1 to post-incremented anonymous state value

      )
    }

    ...          # keep generating values until

    *            # never stop

  )[ $_ ]        # index into the sequence (0-based)
}






0

APL + WIN,23个字节

(∊n↑¨⊂[2]n∘.*n←⍳n)[n←⎕]

说明:

[n←⎕] prompts for screen input and selects the nth element of the concatenated vector
see below

n←⍳n creates a vector of 1 to n

∘.* outer product with exponentiation as the operator

⊂[2] enclose each row of the resulting array as an element of a nested array

∊n↑¨ take 1 to n elements from the 1 to nth row of the matrix and concatenate into a vector

可以使用哪种APL语言?
— 暴民埃里克(Erik the Outgolfer)'17年

它是用APL + WIN编写的。我会在以后的任何答案中都明确说明这一点
— 格雷厄姆(Graham)

我∊从您的答复中取而代之↑,/。我不知道该功能。谢谢
— H.PWiz

@EriktheOutgolfer我认为这将适用于任何现代APL。
— 阿达姆(Adám)'17



0

Clojure 51字节

0索引,例如input 9返回256.0。

#(nth(for[i(range)j(range i)](Math/pow i(inc j)))%)


0

Pyt,39 37字节

1索引

←000`ŕŕ⁺ĐĐř^Đ04Ș↔+⇹Ł-Đ↔3Ș0>łŕ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.