n番目の完璧な力を見つけましょう!


16

完全な力はいくつかの形a**bであり、どこa>0b>1

たとえば、125はとして表現できるため、完全なパワー5**3です。

ゴール

あなたの仕事はn、正の整数が与えられたときに、-thの完全なべき乗を見つけるプログラム/関数を書くこと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

合理的な量。
リーキー修道女

1ビットのデータ型のみを使用する言語はどうですか?
ghosts_in_the_code

1
@ Agawa001はい、それはもはや面白くない標準的な抜け穴です。
flawr

回答:


8

ゼリー、11 バイト

µÆE;¬g/’µ#Ṫ

オンラインでお試しください!

バックグラウンド

すべての正の整数kが最初の累乗の積として一意に因数分解することができるm個の素数、すなわち、K = P 1 α 1 ⋯P M α M、ここでα M > 0

我々は持っているBB> 1の整数、いくつかの正のための)場合にのみbは、すべての指数の除数であるα jは

したがって、整数> 1がk完全パワーある場合にのみGCD(α 1、⋯、α M)≠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を見たことはありません。私はそれを使用する方法がまったくわかりません。
リーキー修道女

素因数分解に関係する完全な力の定義の素晴らしい使用。説明にこのアルゴリズムを含めることができますか?
リーキー修道女

@KennyLauできました。
デニス

21 ^ 2がその因数分解に最初または3番目の素数をどのように含むかがわかりません。「すべての正の整数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)

Octaveにはndgrid(Matlabには必要ありません)=)が必要ないことに再び気付きました。



3

セージ(バージョン6.4、おそらく他も):64 63

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

n完全なパワーを返すラムダ関数を作成します。最初のn^2整数内にあるという事実に依存しています。(1+n^2が必要ですn=1,20+kビットはに変換するint(k)ために必要Integer(k)です。)

バイトオフxrange-> range、ありがとうデニス。

ちょうど面白い事実:0幸いなことに、それ1はリストの最初の要素であり、0番目ではないので、Sageの標準では完璧な力です:)


だから、これは主要な力の部分を除いてPythonですか?
CalculatorFeline

@CatsAreFluffyそしてis_perfect_power()
yo



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であるかどうかを確認した後、プログラムはn4から順番に各数値をチェックして、それが完全な力であるかどうかを確認します。これを行うには、から始めa=b=2ます。の場合a^b == n、完全な力が見つかったので、見つけるために残っている完全な力の数を減らします-正しい数が既に見つかっている場合は、出力します。

の場合a^b < nbインクリメントされます。の場合a^b > naインクリメントされます。次に、の場合a == n、それがn完全なパワーではないことがわかったので、インクリメントn、リセットa、およびしbます。


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番目の要素を取得することにより機能します。


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.