37
\$\begingroup\$

Given a natural number \$n\$, return the \$n\$-th Leyland number.

Leyland Number

Leyland numbers are positive integers \$k\$ of the form

$$k = x^y + y^x$$

Where \$x\$ and \$y\$ are integers strictly greater than 1.

They are enumerated in ascending order.

EDIT: @DigitalTrauma suggested I include following "definition":

Imagine we throw \$x^y+y^x\$ in a bag for all possible values of \$x\$ and \$y\$, and avoid throwing in duplicates. Then we sort that bag. The sorted bag is our sequence.

Details

You may use 0 or 1 based indexing, whatever suits you best.

Your program must be able to output at least all Leyland numbers less than the maximum of signed 32-bit integers. (The last Leyland number below this limit is 1996813914, at index 82.)

Test cases

The first few terms are following:

8, 17, 32, 54, 57, 100, 145, 177, 320, 368, 512, 593, 945, 1124

A076980 in OEIS, except for the first entry. Note that because of that additional first entry, the indices on OEIS are shifted by one.

More can be found in the OEIS b-file

\$\endgroup\$
  • \$\begingroup\$ They are enumerated in ascending order I'm not really sure what this means. Could you provide a list of x and y? \$\endgroup\$ – James Jun 15 '16 at 13:26
  • \$\begingroup\$ @DrGreenEggsandIronMan That means, 8 is before 17, not the other way round. \$\endgroup\$ – Leaky Nun Jun 15 '16 at 13:29
  • 3
    \$\begingroup\$ @DrGreenEggsandIronMan Imagine we throw x^y+y^x in a bag for all possible values of x and y, and avoid thrwoing in duplicates. Then we sort that bag. The sorted bag is our sequence. \$\endgroup\$ – flawr Jun 15 '16 at 13:42
  • 11
    \$\begingroup\$ Very large bag you have there \$\endgroup\$ – Luis Mendo Jun 15 '16 at 18:35
  • 3
    \$\begingroup\$ @LuisMendo Ask @​HenriLéonLebesgue and he is going to tell you that this bag is basically nothing. \$\endgroup\$ – flawr Jun 15 '16 at 18:58

31 Answers 31

11
\$\begingroup\$

MATL, 16 15 13 bytes

Q:Qt!^t!+uSG)

Output is 1-based.

Try it online!

Explanation

Q    % Take input n. Add 1
:Q   % Range [2 ... n+1]. This is enough to be used as x and y
t!   % Duplicate and transpose
^    % Power, element-wise with broadcast. Gives 2D, square array with x^y
     % for all pairs of x and y
t!   % Duplicate and transpose. Gives square array with y^x
+    % Add, element-wise
u    % Keep unique elements. This linearizes (flattens) the 2D array
S    % Sort
G)   % Get the n-th entry. Implicitly display
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ In Matlab unique sorts the elements. Doesn't it in MATL, too? \$\endgroup\$ – pajonk Jun 15 '16 at 17:20
  • 1
    \$\begingroup\$ @pajonk MATL uses the 'stable' flag for unique by default as that is the more typical usage. \$\endgroup\$ – Suever Jun 15 '16 at 18:15
  • \$\begingroup\$ @Suever Ok, thanks for clarifying. \$\endgroup\$ – pajonk Jun 15 '16 at 18:52
  • 1
    \$\begingroup\$ I feel like we use the t!^ (where ^ can be replaced by +, -, or any number of operators) motif a lot. What if we made & mean 1 input for some of those where for a vector it has that behavior? \$\endgroup\$ – Suever Jun 15 '16 at 18:56
  • \$\begingroup\$ @Suever That's a great idea! I've done some research with your script; see the chat \$\endgroup\$ – Luis Mendo Jun 15 '16 at 20:43
6
\$\begingroup\$

Java 10, 225 221 219 216 206 204 193 192 130 129 bytes

n->{var S=new java.util.TreeSet();for(int i=56,t;i<840;)S.add(Math.pow(i/28,t=i%28+2)+Math.pow(t,i++/28));return S.toArray()[n];}

0-indexed

-2 bytes (221 → 219) saved by replacing 1996813915 with (1L<<31) thanks to @LeakyNun.
-3 bytes (219 → 216) thanks to @LeakyNun and @Frozn with something I forgot myself..
-10 bytes (216 → 206) by changing Java 7 to 8.
-2 bytes (206 → 204) by replacing ArrayList with Vector thanks to @TAsk.
-11 bytes (204 → 193) by removing s<(1L<<31)&, since the question states "at least all Leyland numbers less than the maximum of signed 32-bit integers".
-1 byte (193 → 192) by changing Vector to Stack.
-62 bytes (192 → 130) by switching to Java 10 and using var; removing the Collection.sort, import java.util.*;, if-statement and temp-long s by using a java.util.TreeSet (which is a set of unique values and sorts automatically); and using Double and Object return-type instead of Long so we can remove the (int)(...) cast and <Long>.
-1 byte (130 → 129) thanks to @ceilingcat by changing the two nested [2,30] loops to a single [56,840] loop.

Try it here

Explanation:

n->{                             // Method with integer parameter and Object return-type
  var S=new java.util.TreeSet(); //  Create a sorted Set, initially empty
  for(int i=56,t;i<840)          //  Loop `i` in the range [56,840]
    t.add(                       //   Add to the Set:
      Math.pow(i/28,             //    `i` integer-divided by 28
               t=i%28+2)         //    to the power `i` modulo-28 + 2
      +Math.pow(t,               //    And add `i` modulo-28 + 2
                i++/28));        //    to the power `i` integer-divided by 28
                                 //    (and increase `i` by 1 afterwards with `i++`)
  return S.toArray()             //  Convert the sorted Set to an Object-array
           [n];}                 //  And return the value at the input-integer index
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 10/10 for using java \$\endgroup\$ – Leaky Nun Jun 15 '16 at 14:17
  • 1
    \$\begingroup\$ Quick golfs: import java.util.*;long c(int n){List<Long>t=new ArrayList();for(int i=2,j;i<25;i++)for(j=2;j<25;j++){long s=(long)(Math.pow(i,j)+Math.pow(j,i));if(s<(1L<<31)&!t.contains(s))t.add(s);}Collections.sort(t);return t.get(n);} \$\endgroup\$ – Leaky Nun Jun 15 '16 at 14:26
  • 1
    \$\begingroup\$ The for loop variable declaration. \$\endgroup\$ – Leaky Nun Jun 15 '16 at 14:31
  • 1
    \$\begingroup\$ How about for (int i = 1, j; ++i < 30;) and for (j = 1; ++j < 30;) \$\endgroup\$ – Frozn Jun 16 '16 at 10:56
  • 1
    \$\begingroup\$ Largely beaten, after you beat me ;-) \$\endgroup\$ – Olivier Grégoire Jul 12 at 22:10
5
\$\begingroup\$

Haskell, 52 bytes

r=[2..31]
([k|k<-[0..],elem k[x^y+y^x|x<-r,y<-r]]!!)

Really inefficient. Tests each natural number for being a Leyland number, making an infinite list of those that are. Given an input, takes that index element of the list. Uses that only x,y up to 31 need to be checked for 32 bit integers.

Same length with filter:

r=[2..31]
(filter(`elem`[x^y+y^x|x<-r,y<-r])[0..]!!)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In hindsight such an obvious solution, I like it a lot! \$\endgroup\$ – flawr Jun 15 '16 at 18:52
4
\$\begingroup\$

Pyth, 17 bytes

0-indexed.

@{Sms^M_Bd^}2+2Q2

Try it online! (Please, keep it at 100.)

How it works

@{Sms^M_Bd^}2+2Q2
@{Sms^M_Bd^}2+2Q2Q  implicit filling. Input:Q

           }2+2Q    Yield the array [2,3,4,...,Q+2]
          ^     2   Cartesian square: yield all the
                    pairs formed by the above array.
   m     d          Map the following to all of those pairs (e.g. [2,3]):
       _B               Create [[2,3],[3,2]]
     ^M                 Reduce by exponent to each array:
                        create [8,9]
    s                   Sum:   17     (Leyland number generated)
  S                 Sort the generated numbers
 {                  Remove duplicate
@                Q  Find the Q-th element.

Slower version

1-indexed.

e.ffqZs^M_BT^}2Z2

Try it online! (Please, keep it at 3.)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Would it help to create an array of powers [[4,8,...][9,27,...]] and add it to its transpose? \$\endgroup\$ – Neil Jun 15 '16 at 14:05
  • \$\begingroup\$ @Neil I don't think so. It would be helpful in Jelly, but not in Pyth. Pyth does not automatically vectorize. \$\endgroup\$ – Leaky Nun Jun 15 '16 at 14:12
  • \$\begingroup\$ Also helps in MATL, it seems. \$\endgroup\$ – Neil Jun 15 '16 at 19:44
  • \$\begingroup\$ Why do you keep the slower version? \$\endgroup\$ – Erik the Outgolfer Jun 16 '16 at 14:18
4
\$\begingroup\$

MATLAB, 58 bytes

1-indexed

n=input('');[A B]=ndgrid(2:n+9);k=A.^B;x=unique(k'+k);x(n)

unique in MATLAB flattens and sorts the matrix.


Thanks for help to @FryAmTheEggman and @flawr.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Husk, 13 12 bytes

!uΞ´Ṫ§+^`^tN

Try it online!

Tied with MATL, with a similar method.

-1 byte from Zgarb!

Explanation

!uΞ´Ṫ§+^`^tN
           N list of all natural numbers
          t  tail: remove 1
   ´         double: f x = f x x
    Ṫ        cartesian product with the following function
     §       fork: f g h x y = f (g x y) (h x y)
       ^     x^y
      +      plus
        `    reverse: f x y = f y x
        `^   y^x
  Ξ          merge all results together
 u           uniquify
!            take element at input index
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ The 12-byte code Q:Qt!^&+uSG) would work in recent versions of MATL, but I prefer to keep the original code, which runs in versions predating the challenge :-) \$\endgroup\$ – Luis Mendo Oct 8 at 14:18
  • 1
    \$\begingroup\$ @LuisMendo Husk hasn't been updated since around 2017, so I guess we're on an even playing field. \$\endgroup\$ – Razetime Oct 8 at 14:23
3
\$\begingroup\$

05AB1E, 20 19 bytes

0-indexed

ÝÌ2ãvyÂ`ms`m+}){Ù¹è

Explained

ÝÌ                     # range(2,N+2)
  2ã                   # get all pairs of numbers in the range
    v                  # for each pair
     yÂ`ms`m+          # push x^y+y^x
             }         # end loop
              ){Ù      # wrap to list, sort and remove duplicates
                 ¹è    # get Nth element of list

Try it online

Saved 1 byte thanks to @Adnan

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Very nice! One tip, ÝÌ is short for >L>. \$\endgroup\$ – Adnan Jun 15 '16 at 15:50
  • \$\begingroup\$ @Adnan: Thanks! I can't belive I didn't think of that :P \$\endgroup\$ – Emigna Jun 15 '16 at 15:54
  • \$\begingroup\$ ê is sorted_uniquified, if that existed when this was asked. \$\endgroup\$ – Magic Octopus Urn Nov 1 '16 at 16:36
  • \$\begingroup\$ @carusocomputing: It was bugged until quite recently I'm afraid. \$\endgroup\$ – Emigna Nov 1 '16 at 18:07
  • \$\begingroup\$ I know these builtins weren't available back then, but can be â; vy...}) can be ε...}; and can be ê for -4 bytes. \$\endgroup\$ – Kevin Cruijssen Oct 7 at 8:41
3
\$\begingroup\$

Mathematica, 60 48 40 bytes

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

Uses one-based indexing. Union is used by applying it between each row of the 2D matrix created by the Array. There, Union will flatten the 2D matrix into a list while also removing any duplicates and placing the values in sorted order.

Saved 8 bytes thanks to @LLlAMnYP.

Usage

Example

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ {#+1,#+1} isn't necessary, can be left as {#,#} and {2,2} can be replaced with simply 2. \$\endgroup\$ – LLlAMnYP Jun 17 '16 at 11:20
  • \$\begingroup\$ @LLlAMnYP Thanks! Didn't know that Array would expand the third argument. \$\endgroup\$ – miles Jun 17 '16 at 11:42
  • \$\begingroup\$ Neither did I but I decided to try it anyway and it worked :) \$\endgroup\$ – LLlAMnYP Jun 17 '16 at 11:44
2
\$\begingroup\$

Jelly, 14 bytes

2 bytes thanks to Dennis.

R‘*€¹$+Z$FṢQị@

Try it online! (Takes ~ 1s for 82 for me) (O(n^2) time)

Original 16-byte answer

2r30*€¹$+Z$FṢQị@

Try it online! (Takes < 1s for me) (Constant time)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ R‘*€¹$+Z$FṢQị@ is faster, shorter and has no artificial upper bound. \$\endgroup\$ – Dennis Jun 15 '16 at 15:31
  • \$\begingroup\$ @Dennis and beats my answer :-P \$\endgroup\$ – Luis Mendo Jun 15 '16 at 15:31
  • \$\begingroup\$ @Dennis I don't get it. How come it is faster than the second one. \$\endgroup\$ – Leaky Nun Jun 15 '16 at 15:32
  • \$\begingroup\$ It isn't faster than the second one. The execution time is too short to get an accurate measurement. \$\endgroup\$ – Dennis Jun 15 '16 at 15:34
  • \$\begingroup\$ Now 13 bytes :-P \$\endgroup\$ – Luis Mendo Jun 15 '16 at 15:51
2
\$\begingroup\$

Bash + GNU utilities, 63

printf %s\\n x={2..32}\;y={2..32}\;x^y+y^x|bc|sort -nu|sed $1!d

1-based indexing. It looks like this is pretty much the same approach as @TimmyD's answer. Instead of nested loops, bash brace expansion is used to generate arithmetic expressions that are piped to bc for evaluation.

Ideone.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Perl 6,  60 58  56 bytes

{sort(keys bag [X[&({$^a**$^b+$b**$a})]] (2..$_+2)xx 2)[$_]}
{sort(keys set [X[&({$^a**$^b+$b**$a})]] (2..$_+2)xx 2)[$_]}
{sort(unique [X[&({$^a**$^b+$b**$a})]] (2..$_+2)xx 2)[$_]}
{squish(sort [X[&({$^a**$^b+$b**$a})]] (2..$_+2)xx 2)[$_]}
{squish(sort [X[&({$^a**$^b+$b**$a})]] (2..31)xx 2)[$_]}
{squish(sort [X[&({$^a**$^b+$b**$a})]] 2..31,2..31)[$_]}

Test:

#! /usr/bin/env perl6
use v6.c;

my &Leyland = {squish(sort [X[&({$^a**$^b+$b**$a})]] 2..31,2..31)[$_]}

say ^14 .map: &Leyland;
time-this {Leyland 81};

sub time-this (&code) {
  my $start = now;
  my $value = code();
  printf "takes %.3f seconds to come up with $value\n", now - $start;
}
(8 17 32 54 57 100 145 177 320 368 512 593 945 1124)
takes 0.107 seconds to come up with 1996813914

Explanation:

{
  squish( # remove repeated values
    sort
      [X[&( # cross reduce with:
        { $^a ** $^b + $b ** $a }
      )]]
        ( 2 .. $_+2 ) # 「Range.new(2,$_+2)」 (inclusive range)
        xx 2          # repeat list
  )[$_]
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Can't you remove the spaces between sort [ and ] 2..31? \$\endgroup\$ – Erik the Outgolfer Jun 16 '16 at 14:10
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ That would turn it from a subroutine call sort([... to an array access of a term sort[.... A similar thing happens with the other space. \$\endgroup\$ – Brad Gilbert b2gills Jun 16 '16 at 16:41
  • \$\begingroup\$ I guess there you have implemented this half-pseudo code map { $^a ** $^b + $b ** $a }, 2..5,5...2 But, how could you think of constructing it that way at the first place? The cross reduce with a closure is particularly tricky. Could you give some sample invocations of that closure? \$\endgroup\$ – Lars Malmsteen Oct 10 at 20:00
  • \$\begingroup\$ @LarsMalmsteen There are a lot of things working together there. I don't think that this is a good place to discuss this. If you post your questions to the perl6-users mailing list, I and everyone else on there would be more than happy to help you understand. I think the reason I used reduce [] was to reduce the number of spaces and parens. \$\endgroup\$ – Brad Gilbert b2gills Oct 11 at 14:11
  • \$\begingroup\$ Shave another 2 chars: say {squish(sort 2..31 X[&({$^a**$^b+$b**$a})]2..31)[$_]} \$\endgroup\$ – raiph Oct 11 at 20:29
2
\$\begingroup\$

F#, 117, 104

Welp, it's shorter than my C# answer at least.

Saved 13 bytes thanks to Reed Copsey in the F# chatroom.

let f n=[for x in 2I..32I do for y in 2I..32I->x**(int y)+y**(int x)]|>Seq.sort|>Seq.distinct|>Seq.nth n
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

PowerShell v2+, 84 73 68 bytes

(2..30|%{2..($x=$_)|%{"$x*"*$_+'1+'+"$_*"*$x+1|iex}}|sort)[$args[0]]

Saved 11 bytes thanks to @Neil ... saved additional 5 bytes by reorganizing how the iex expression is evaluated.

Naïve method, we simply double-for loop from x=2..30 and y=2..x. Each loop we put x^y + y^x on the pipeline. The 30 was chosen experimentally to ensure that we covered all cases less than 2^31-1 ;-). We pipe those to Sort-Object to order them ascending. Output is zero-indexed based on the input $args[0].

Yes, there are a lot of extraneous entries generated here -- this algorithm actually generates 435 Leyland numbers -- but things above index 81 are not guaranteed to be accurate and in order (there may be some that are skipped).

Examples

PS C:\Tools\Scripts\golfing> .\leyland-numbers.ps1 54
14352282

PS C:\Tools\Scripts\golfing> .\leyland-numbers.ps1 33
178478

PS C:\Tools\Scripts\golfing> .\leyland-numbers.ps1 77
1073792449
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

R, 58 54 bytes

1-indexed. Eliminated 4 bytes by using pryr::r instead of function.

unique(sort(outer(2:99,2:9,pryr::f(x^y+y^x))))[scan()]

Explanation

For all numbers from 2 to 99, and 2 to 9,

                  2:99,2:9

apply the function x^y+y^x. This generates a 98x8 matrix.

            outer(2:99,2:9,pryr::f(x^y+y^x))

Sort this matrix (coercing it to a vector):

       sort(outer(2:99,2:9,pryr::f(x^y+y^x)))

Remove all non-unique values:

unique(sort(outer(2:99,2:9,pryr::f(x^y+y^x))))

Read n from stdin, and fetch the nth number from the list:

unique(sort(outer(2:99,2:9,pryr::f(x^y+y^x))))[scan()]
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Firefox 42-57), 94 bytes

n=>[for(x of Array(32).keys())for(y of Array(x+1).keys())if(y>1)x**y+y**x].sort((x,y)=>x-y)[n]

Needs Firefox 42 because it uses both array comprehensions and exponentiation ([for(..of..)] and **).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Shouldn't you just mark it as ES7? \$\endgroup\$ – mbomb007 Jun 15 '16 at 18:48
  • \$\begingroup\$ @mbomb007 I don't think [for...of] made it to ES7. \$\endgroup\$ – Neil Jun 15 '16 at 19:41
  • \$\begingroup\$ It's part of ES6 \$\endgroup\$ – mbomb007 Jun 15 '16 at 19:58
  • \$\begingroup\$ No, that's for(..of..), not [for(..of..)]. \$\endgroup\$ – Neil Jun 15 '16 at 20:09
  • \$\begingroup\$ Ah, okay. (Non-standard. Do not use.) lol \$\endgroup\$ – mbomb007 Jun 15 '16 at 20:20
1
\$\begingroup\$

Haskell, 99 98 96 95 94 bytes

It is probably easily outgolfed, but that was the best I was able to come up with.

import Data.List
f n|n<2=[]|n>1=sort.nub$f(n-1)++[x^n+n^x|x<-[2..n]]
g n=(f.toInteger$n+3)!!n
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ import Data.List f n|w<-[2..toEnum$n+3]=(sort$nub[x^y+y^x|x<-w,y<-w])!!n Do you know why toInteger/toEnum is needed? \$\endgroup\$ – Damien Jun 15 '16 at 16:20
  • \$\begingroup\$ Wow, this is crazy=) Feel free to add it as your own answer, as it is qutie different from mine! If we omit toInteger in my solution we'll have an overflow using int, because we iterate way higher (to n+3 instead of n) when working with the list. Otherwise we'd need to hardcode the first four terms or so. What exactly does toEnum do in your solution? \$\endgroup\$ – flawr Jun 15 '16 at 16:38
  • \$\begingroup\$ OK, that's because of (!!) operator which binds n to an Int. Since n is supposed to be under 82, w can be replaced by [2..99] for example and f=(sort(nub[x^y+y^x|x<-[2..99],y<-[2..x]])!!) . toEnum converts an Int to an Enum, and Integer is an instance of Enum class so toEnum here converts n+3 to an Integer. \$\endgroup\$ – Damien Jun 15 '16 at 16:42
1
\$\begingroup\$

Python 3, 76 69 bytes

r=range(2,32);f=lambda n:sorted({x**y+y**x for x in r for y in r})[n]

0-indexed.

https://repl.it/C2SA

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ It’s okay to just write your answer as r=range(2,32) lambda n:sorted(…)[n] \$\endgroup\$ – Lynn Jun 15 '16 at 16:19
1
\$\begingroup\$

C#, 141, 127 bytes.

Oh c#, you are such a long language.

n=>(from x in Enumerable.Range(2,32)from y in Enumerable.Range(2,32)select Math.Pow(x,y)+Math.Pow(y,x)).Distinct().ToList()[n];

This is a lambda that needs to be assigned to delegate double del(int n); to be run, as such:

delegate double del(int n);
del f=n=>(from x in Enumerable.Range(2,32)from y in Enumerable.Range(2,32)select Math.Pow(x,y)+Math.Pow(y,x)).OrderBy(q=>q).Distinct().ToList()[n];
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Still shorter than Java. \$\endgroup\$ – flawr Jun 15 '16 at 19:06
  • \$\begingroup\$ @flawr Wooooooo? \$\endgroup\$ – Morgan Thrapp Jun 15 '16 at 19:07
  • \$\begingroup\$ I know nothing about C#, but couldn't you save Enumerable.Range( to a variable/function/iterator/whatever with a shorter name for reuisng? \$\endgroup\$ – flawr Jun 15 '16 at 19:10
  • \$\begingroup\$ I could, but then I would need to include a class and type defs, which ends up costing me a ton. \$\endgroup\$ – Morgan Thrapp Jun 15 '16 at 19:10
1
\$\begingroup\$

SQL (PostgreSQL 9.4), 171 bytes

Done as a prepared statement. Generate a couple of series 2 - 99, cross join them and do the equation. Densely rank the results to index them and select the first result that has the rank of the integer input.

prepare l(int)as select s from(select dense_rank()over(order by s)r,s from(select x^y+y^x from generate_series(2,99)x(x),generate_series(2,99)y(y))c(s))d where r=$1limit 1

Executed as follows

execute l(82)
s
-----------------
1996813914

This ended up running a lot quicker than I expected

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

J, 29 bytes

<:{[:/:~@~.@,[:(^+^~)"0/~2+i.

Uses one-based indexing. Conversion from my Mathematica solution.

The true secret here is that I have :(^+^~) on my side.

Usage

   f =: <:{[:/:~@~.@,[:(^+^~)"0/~2+i.
   f 7
145
   (,.f"0) >: i. 10  NB. Extra commands for formatting
 1   8
 2  17
 3  32
 4  54
 5  57
 6 100
 7 145
 8 177
 9 320
10 368

Explanation

<:{[:/:~@~.@,[:(^+^~)"0/~2+i.  Input: n
                         2+i.  Step one
                     "0/~      Step two
              :(^+^~)          ???
<:{[:/:~@~.@,[                 Profit

More seriously,

<:{[:/:~@~.@,[:(^+^~)"0/~2+i.  Input: n
                           i.  Create the range [0, 1, ..., n-1]
                         2+    Add 2 to each
               (^+^~)"0        Create a dyad (2 argument function) with inputs x, y
                               and returns x^y + y^x
             [:        /~      Use that function to create a table using the previous range
   [:       ,                  Flatten the table into a list
         ~.@                   Take its distinct values only
     /:~@                      Sort it in ascending order
<:                             Decrement n (since J is zero-indexed)
  {                            Select the value at index n-1 from the list and return
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ ... Profit :D \$\endgroup\$ – flawr Jun 17 '16 at 19:19
1
\$\begingroup\$

Swift 3, 138 bytes

import Glibc;func l(n:Int)->Int{let r=stride(from:2.0,to:50,by:1);return Int(Set(r.flatMap{x in r.map{pow(x,$0)+pow($0,x)}}).sorted()[n])}

Ungolfed code

Try it here

import Glibc
func l(n: Int) -> Int {
    // Create a Double sequence from 2 to 50 (because pow requires Double)
    let r = stride(from: 2.0, to: 50.0, by: 1.0)

    return Int(Set(r.flatMap {
        x in r.map {
            pow(x, $0) + pow($0, x)
        }
    }).sorted()[n])
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Nice first answer, but it'd be better if you could explain what's going on. \$\endgroup\$ – clismique Jun 16 '16 at 9:08
1
\$\begingroup\$

Axiom 148 bytes

w(n)==(v:List INT:=[];for i in 2..31 repeat for j in i..31 repeat(a:=i^j+j^i;if a>1996813914 then break;v:=cons(a,v));v:=sort v;~index?(n,v)=>0;v.n)

some example

w(n)==
 v:List INT:=[];for i in 2..31 repeat for j in i..31 repeat
        (a:=i^j+j^i;if a>1996813914 then break;v:=cons(a,v));v:=sort v;~index?(n,v)=>0
 v.n
 (2) -> [w(i)  for i in 0..85]
    Compiling function w with type NonNegativeInteger -> Integer

    (2)
    [0, 8, 17, 32, 54, 57, 100, 145, 177, 320, 368, 512, 593, 945, 1124, 1649,
     2169, 2530, 4240, 5392, 6250, 7073, 8361, 16580, 18785, 20412, 23401,
     32993, 60049, 65792, 69632, 93312, 94932, 131361, 178478, 262468, 268705,
     397585, 423393, 524649, 533169, 1048976, 1058576, 1596520, 1647086,
     1941760, 2012174, 2097593, 4194788, 4208945, 4785713, 7861953, 8389137,
     9865625, 10609137, 14352282, 16777792, 16797952, 33554432, 33555057,
     43050817, 45136576, 48989176, 61466176, 67109540, 67137425, 129145076,
     134218457, 177264449, 244389457, 268436240, 268473872, 292475249,
     364568617, 387426321, 536871753, 774840978, 1073742724, 1073792449,
     1162268326, 1173741824, 1221074418, 1996813914, 0, 0, 0]

Type: List Integer

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 70 + 1 (-p) = 71 bytes

for$x(2..32){$r{$x**$_+$_**$x}++for$x..32}$_=(sort{$a<=>$b}keys%r)[$_]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 62 58 bytes

->n{a,*b=c=2;c+=1while(b<<a**c+c**a;c<32||32>c=a+=1);b[n]}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 136 bytes

n->java.util.stream.IntStream.range(0,900).map(l->l+=Math.pow(l%30+2,l/30+2)+Math.pow(l/30+2,l%30+2)-l).sorted().distinct().toArray()[n]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 28 bytes

^₂g{∧{Ċ≥₁>ᵛ1&≜↔;?^ᵐ+}}ᶠ⁽o∋↙?

Try it online!

How it works

^₂g{∧{Ċ≥₁>ᵛ1&≜↔;?^ᵐ+}}ᶠ⁽o∋↙?
^₂g                           7 -> [49]
   {∧{              }}ᶠ⁽      find first 49 outputs whose …
      Ċ≥₁>ᵛ1                  inputs are of form [X,Y],
       ≥₁                     are ordered
         >ᵛ1                  and strictly greater than 1
            &≜                force labels on input
              ↔;?             [2,3] -> [[3,2],[2,3]]
                 ^ᵐ                 -> [9, 8]
                   +                -> 17
                        o∋↙?  order outputs and take the Nth element
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 12 bytes

‘€p`*U$§QṢị@

Try it online!

Similar algorithm to Leaky Nun's Jelly answer but with a few improvements

How it works

‘€p`*U$§QṢị@ - Main link. Takes n on the left
 €           - Yield [1, 2, ..., n]
‘            - Increment each to [2, 3, ..., n+1]
  p`         - Take the Cartesian product with itself, yielding all pairs [[2, 2], [2, 3], ..., [n+1, n+1]]
      $      - Run the previous two commands over each pair, [x, y]:
     U       -   Reverse the pair to yield [y, x]
    *        -   Raise to the power to yield [x*y, y*x]
       §     - Take the sum of each, yielding [8, 17, ..., 2(n+1)*(n+1)]]
        Q    - Deduplicate
         Ṣ   - Sort in ascending order
          ị@ - Take the nth element in this list
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

J, 38 31 bytes

0-indexed.

[{[:(#~~:)@/:~@,/[:(+|:)[:^/~2+i.@>:@]
((#~~:)/:~,/(+|:)^/~2+i.29x){~[

Usage

>> f =: ((#~~:)/:~,/(+|:)^/~2+i.29x){~[
>> f 81
<< 1996813914
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 129->116 bytes

I know there is a shorter python 3 answer, but I still wanted to contribute my solution.

t=[]
for k in range(100):a,b,q=k//10,k%10,a**b+b**a;f=lambda q:0if q in t else t.append(q);f(q)
print(sorted(t)[7:])

This was the best way that I could to think of to handle going through all values for x and all values for y. If anyone can golf my approach it would be appreciated

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Make t a set instead of a list, and replace the last for statements with a plain t.add(q). \$\endgroup\$ – Cristian Ciupitu Jun 17 '16 at 0:37
0
\$\begingroup\$

APL (Dyalog), 27 bytes

{d⊃⍨⍵⊃⍋d←∪,∘.(*⍨+*)⍨1↓⍳1+⍵}

Try it online!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.