Java 8、211バイト
import java.util.*;n->{List l=new Stack();for(int a=2,b;a<132;a++)for(b=2;b<132;b++)if(p(a)*p(b)>0)l.add(Math.pow(a,b));Collections.sort(l);return l.get(n);}int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}
非常に非効率的な方法..基本的に2 2から999 999 132 132までのすべてのPPPを計算してリストに保存し、そのリストをソートしてから、n
から、そのリストから '番目のアイテムをます。
編集:999 999を使用して28,225アイテムのリストを作成する代わりに、132 132を使用して1,024アイテムのリストを作成します。これにより、パフォーマンスがかなり向上し、チャレンジではインデックス0から1,000までの入力をサポートする必要があると示されているため、完全に受け入れられます。(ただし、変更1e3
し132
てもバイト数には影響しません。)
説明:
ここで試してみてください。
import java.util.*; // Required import for List, Stack and Collections
n->{ // Method with integer as parameter and Object as return-type
List l=new Stack(); // List to store the PPPs in
for(int a=2,b;a<132;a++) // Loop (1) from 2 to 1,000 (exclusive)
for(b=2;b<132;b++) // Inner loop (2) from 2 to 1,000 (exclusive)
if(p(a)*p(b)>0) // If both `a` and `b` are primes:
l.add(Math.pow(a,b)); // Add the power of those two to the List
// End of loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)
Collections.sort(l); // Sort the filled List
return l.get(n); // Return the `n`'th item of the sorted List of PPPs
} // End of method
int p(int n){ // Separated method with integer as parameter and return-type
for(int i=2; // Index integer (starting at 2)
i<n; // Loop from 2 to `n` (exclusive)
n=n%i++<1? // If `n` is divisible by `i`:
0 // Change `n` to 0
: // Else:
n // Leave `n` the same
); // End of loop
return n; // Return `n` (which is now 0 if it wasn't a prime)
} // End of separated method