找到第n个完美的力量!


16

一个完美的力量是形式a**b,地点a>0和地点的数量b>1

例如,125是完美的功效,因为它可以表示为5**3

目标

n给定正整数,您的任务是编写找到第- 个完美幂的程序/函数n

眼镜

  • 第一个完美的力量是1(是1**2)。
  • 以任何合理的格式输入/输出。
  • 允许内置。

更多信息

计分

这是。以字节为单位的最短解决方案获胜。

测试用例

input  output
1      1
2      4
3      8
4      9
5      16
6      25
7      27
8      32
9      36
10     49

1
直到这个数字应该起作用?无穷?
ghosts_in_the_code

合理的数量。
Leaky Nun

只使用一位数据类型的语言呢?
ghosts_in_the_code

1
@ Agawa001是的,这是一个不再有趣的标准漏洞
flawr

Answers:


8

果冻,11 字节

µÆE;¬g/’µ#Ṫ

在线尝试!

背景

每一个正整数ķ可以唯一被因式分解为第一的幂的乘积素数,即ķ= P 1 α 1 ⋯p α ,其中α > 0

我们有一个bB> 1)的一些正整数一个当且仅当b是所有指数的除数α Ĵ

因此,一个整数K> 1是一个完美的功率,当且仅当满足gcd(α 1,⋯,α )≠1

怎么运行的

µÆE;¬g/’µ#Ṫ  Main link. No arguments.

µ            Make the chain monadic, setting the left argument to 0.
        µ#   Find the first n integers k, greater or equal to 0, for which the
             preceding chain returns a truthy value.
             In the absence of CLAs, n is read implicitly from STDIN.
 ÆE          Compute the exponents of the prime factorization of k.
   ;¬        Append the logical NOT of k, i.e., 0 if k > 0 and 1 otherwise.
             This maps 1 -> [0] and [0] -> [1].
     g/      Reduce the list of exponents by GCD.
             In particular, we achieved that 1 -> 0 and 0 -> 1.
       ’     Decrement; subtract 1 from the GCD.
             This maps 1 to 0 (falsy) and all other integers to a truthy value.
          Ṫ  Tail; extract the last k.

我根本没看过STDIN。我根本不知道如何使用它。
Leaky Nun

完美功率定义的很好使用与素因数分解有关。您可以在说明中包含此算法吗?
Leaky Nun

@KennyLau完成。
丹尼斯

我不明白21 ^ 2如何在其因式分解中包含第一个或第三个素数。您能否帮助我理解您的意思是“每个正整数k都可以作为前m个素数的幂乘积的唯一因式分解...在[指数]处a_n > 0?” 在我看来,在21 ^ 2的因式分解中,p = 2和p = 5的指数为零。
גלעדברקן

@גלעדברקן抱歉,应该是a_m> 0。先前的m-1个指数可能包含零。
丹尼斯

6

Mathematica,34个字节

(Union@@Array[#^#2#&,{#,#}])[[#]]&

生成一个n × n数组A ij = i 1+ j,将其展平,然后返回第n个元素。


3

CJam,16个字节

ri_),_2f+ff#:|$=

在这里测试。

说明

这使用了与LegionMammal的Mathematica答案类似的想法。

ri    e# Read input and convert to integer N.
_),   e# Duplicate, increment and turn into range [0 1 ... N].
_2f+  e# Duplicate and add two to each element to get [2 3 ... N+2].
ff#   e# Compute the outer product between both lists over exponentiation.
      e# This gives a bunch of perfect powers a^b for a ≥ 0, b > 1.
:|    e# Fold set union over the list, getting all unique powers generated this way.
$     e# Sort them.
=     e# Retrieve the N+1'th power (because input is 1-based, but CJam's array access
      e# is 0-based, which is why we included 0 in the list of perfect powers.

3

八度,57 31 30字节

@(n)unique((1:n)'.^(2:n+1))(n)

我只是再次注意到,八度不需要ndgrid(而Matlab则需要)=)



3

贤者(6.4版本,可能还有其他版本):64 63

lambda n:[k for k in range(1+n^2)if(0+k).is_perfect_power()][n]

创建一个lambda函数,该函数返回n完美的力量。我们依靠在第一个n^2整数中找到它的事实。(该1+n^2是必要的n=1,2。该0+k位是必要的转换int(k),以Integer(k)。)

字节关闭xrange->range,感谢丹尼斯。

只是一个有趣的事实:0幸运的1是,按照Sage的标准,它是完美的功能,因为那时它是列表的第一元素,而不是第0 :)


所以这是Python,除了主要力量部分?
CalculatorFeline

@CatsAreFluffy和is_perfect_power()


1

MATL,9个字节

:tQ!^uSG)

在线尝试

这是Flawr的Octave MATL解决方案的端口,使功率矩阵达到n^(n+1),并得到n-th。


1

朱莉娅,64 32字节

n->sort(∪([1:n]'.^[2:n+1]))[n]

这是一个接受整数并返回整数的匿名函数。要调用它,请将其分配给变量。

这里的想法与LegionMammal的Mathematica 答案相同:我们采用整数1到n与2到n + 1 的外积,将生成的矩阵按列折叠,取唯一元素,进行排序,得到第n 元素。

在线尝试!(包括所有测试用例)


1

JavaScript(ES6),87

n=>(b=>{for(l=[i=0,1];b<n*n;++b)for(v=b;v<n*n;)l[v*=b]=v;l.some(x=>n==i++?v=x:0)})(2)|v

少打高尔夫球

f=n=>{
  for(b=2, l=[0,1]; b < n*n; ++b)
    for(v = b; v < n*n;)
      l[v*=b] = v;
  i = 0;
  l.some(x => n == i++ ? v=x : 0);
  return v;
  // shorter alternative, but too much memory used even for small inputs
  // return l.filter(x=>x) [n-1];
}

测试

f=n=>(b=>{for(l=[i=0,1];b<n*n;++b)for(v=b;v<n*n;)l[v*=b]=v;l.some(x=>n==i++?v=x:0)})(2)|v

function test(){
  var v=+I.value
  O.textContent=f(v)
}
  
test()
<input type=number id=I value=10><button onclick='test()'>-></button>
<span id=O></span>


1

实际上是18个字节(无竞争)

;;u@ⁿr;`;√≈²=`M@░E

在线尝试!(由于需要更新,可能无法使用)

该解决方案是非竞争性的,因为E在发布此挑战后我修复了一个错误。

说明:

;;u@ⁿr;`;√≈²=`M@░E
;;u@ⁿr              push range(n**(n+1))
      ;`;√≈²=`M@░   filter: take if
        ;√≈²=         int(sqrt(x))**2 == x
                 E  get nth element

1

> <>,​​108个字节

:1)?v  >n;
$:@@\&31+2>2$:@@:@
:1=?\@$:@*@@1-
:~$~<.1b+1v!?(}:{:~~v?(}:{:v?=}:{
1-:&1=?v~~>~61.     >~1+b1.>&

该程序要求输入编号在运行之前出现在堆栈上。

将浪费的字节数减少到7花了很多时间!

在检查输入是否为之后1,程序n依次检查4中的每个数字,以查看其是否为完美的幂。它以开始a=b=2。如果a^b == n找到,则我们找到一个理想的幂,因此减少要寻找的理想幂的数量-如果我们已经找到了正确的幂,则输出。

如果a^b < nb则增加。如果a^b > na则增加。然后,如果a == n发现的话,这n并不是一个完美的选择,所以请递增n,重置ab


0

J,29个字节

基于@ LegionMammal978的方法

<:{[:/:~@~.[:,/[:(^/>:)~>:@i.

用法

   f =: <:{[:/:~@~.[:,/[:(^/>:)~>:@i.
   f " 0 (1 2 3 4 5 6 7 8 9 10)
1 4 8 9 16 25 27 32 36 49

说明

<:{[:/:~@~.[:,/[:(^/>:)~>:@i.
                           i.  Create range from 0 to n-1
                        >:     Increments each in that range, now is 1 to n
               [:              Cap, Ignores input n
                    >:         New range, increment from previous range to be 2 to n+1 now
                  ^/           Forms table using exponentation between 1..n and 2..n+1
             ,/                Flattens table to a list
         ~.                    Takes only distinct items
     /:~                       Sorts the list
<:                             Decrements the input n (since list is zero-based index)
  {                            Selects value from resulting list at index n-1

0

JavaScript(ES7),104个字节

n=>(a=[...Array(n)]).map(_=>a.every(_=>(p=i**++j)>n*n?0:r[p]=p,i+=j=1),r=[i=1])&&r.sort((a,b)=>a-b)[n-1]

通过计算不大于n²的所有幂,对结果列表进行排序并采用第n个元素来工作。


0

爪哇126

r->{int n,i,k;if(r==1)return r;for(n=i=2,r--;;){for(k=i*i;k<=n;k*=i)if(k==n){i=--r>0?++n:n;if(r<1)return n;}if(--i<2)i=++n;}}

使用递归会更短吗?
Leaky Nun

好主意,但是需要很多计划。
HopefulHelpful
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.