Letters associated with prime numbers

13

If we assign each letter a respective integer, starting from 1, then a is 1, b is 2, c is 3, and so on. After z, the letters loop back around, but with a in front (aa, ab, ac). It then goes to ba, bb, bc... After this is completed, as you may have figured, another letter is added (aaa, aab, aac). "Prime letters" would be letters that are associated with a prime number. b would be the first prime letter, followed by c, e, g, et cetera.

The Challenge

Given an input n, find the nth "prime letter."

Examples

Input:

1

Output:

b

Input:

4

Output:

g

Input:

123

Output:

za

Scoring Criteria

This is code golf, so the shortest answer in bytes wins!

floof

Posted 2019-10-17T12:39:31.837

Reputation: 139

Basically, yes. – floof – 2019-10-17T12:42:23.230

5

Welcome to the site! This is a nice first question, but given the task, I believe it is a duplicate (although I can't find it just now). I'd recommend that for your next challenge you post it in the Sandbox first to receive feedback.

– caird coinheringaahing – 2019-10-17T12:50:46.843

9Perhaps include test cases beyond Z? – HyperNeutrino – 2019-10-17T13:38:38.120

6related; this is the inverse (letters->numbers). I think the numbers->letters exists somewhere (even if it's just in a bijective base-n question), and prime-related challenges have been done to death. Not saying this is a bad challenge, just that it's a composite of existing ones. – Giuseppe – 2019-10-17T14:43:24.450

1Suggested test cases of big numbers so you can verify the letter wrapping works correctly. – Veskah – 2019-10-17T17:13:22.373

9Add the test case 123 -> za. Several current answers get it wrong. – benrg – 2019-10-17T21:40:17.260

1How large can n be? – dan04 – 2019-10-18T02:58:06.947

Also related – Sara J – 2019-10-18T16:02:49.883

Answers

8

Python 3,262 236 209 196 179 114 93 92 97 bytes

def f(n):
 m=k=1;s=''
 while n:m*=k*k;k+=1;n-=m%k
 while k:s=chr(~-k%26+97)+s;k=~-k//26
 return s

Try it online!

Fixed a bug mentioned by @benrg about getting a wrong output for the input 123.

Thanks to:
- @AdmBorkBork for help me getting started and save a few bytes
- @Sriotchilism O'Zaic for saving me 6 bytes
- @mypetlion for saving me 65 bytes and bringing me under 150 bytes :)
- @dingledooper for saving me 21 bytes and bringing me under 100 bytes :D
- @Jitse for saving 1 byte


98 bytes

f=lambda n,i=1,p=1:n and-~f(n-p%i,i+1,p*i*i)
g=lambda n,s='':n and g(~-n//26,chr(~-n%26+97)+s)or s

Try it online!

Delta

Posted 2019-10-17T12:39:31.837

Reputation: 543

Welcome to Code Golf SE! I'm not a Python guy, but I know you can save a bunch of bytes by converting your indents to tabs instead of multiple spaces. – AdmBorkBork – 2019-10-17T15:10:15.667

Thanks for the tip :) – Delta – 2019-10-17T15:13:00.643

1

Also, make sure to check out the Python tips thread.

– AdmBorkBork – 2019-10-17T15:13:44.213

1You might want to give your answer a comb over for extra spaces, I can see 5 spaces right now that are not neccessary. – Post Rock Garf Hunter – 2019-10-17T15:31:34.147

@SriotchilismO'Zaic Thanks for the suggestion :) – Delta – 2019-10-17T15:45:13.340

1

It's not required but Try it online! is a cool site that counts bytes, runs code, and can generate a CG&CC post

– Veskah – 2019-10-17T16:43:51.980

You seem to have a small bug somewhere. I tried a large number: Try it online!

– Sanchises – 2019-10-17T16:50:16.947

@Veskah Thanks for the hint – Delta – 2019-10-17T16:50:35.893

1

114 bytes with the same approach Try it online!

– mypetlion – 2019-10-17T18:04:42.223

@mypetlion thank you, may i take your improvement :) – Delta – 2019-10-17T18:19:40.277

@Delta Sure. Any improvements posted in the comments are free for you to use. If somebody doesn't want you to use it, they will post it as their own answer. – mypetlion – 2019-10-17T18:38:54.157

1

93 bytes by using a better prime algorithm: Try It Online!

– dingledooper – 2019-10-18T00:45:18.797

@dingledooper Thnak you for the improvement – Delta – 2019-10-18T04:51:45.813

1

-1 byte by using a function instead: Try it online!

– Jitse – 2019-10-18T12:58:26.603

@Jitse Thank you :) – Delta – 2019-10-20T14:23:46.873

4

Jelly, 8 bytes

ÆNḃ26ịØa

A monadic Link which accepts a non-negative integer that yields a list of characters.

Try it online!

How?

ÆNḃ26ịØa - Link: integer, n
ÆN       - nth prime
   26    - literal 26
  ḃ      - bijective base
      Øa - literal list of characters = ['a', 'b', ..., 'z']
     ị   - index into

Jonathan Allan

Posted 2019-10-17T12:39:31.837

Reputation: 67 804

For input 123 the output should be za but this code currently prints AZA. – benrg – 2019-10-17T21:52:01.763

2Ah it's bijective base decompression, I guess it has to be ÆNḃ26ịØA then :( – Jonathan Allan – 2019-10-17T22:10:28.633

...and that'll be exactly the same as the other Jelly entry becomes. – Jonathan Allan – 2019-10-17T22:13:35.450

4

JavaScript (Node.js), 83 bytes

f=(n,k=0)=>n?f(n-(g=d=>~k%d--?g(d):!d)(++k),k):k<0?'':f(n,k/26-1)+Buffer([k%26+65])

Try it online!

Commented

f = (                // f is a recursive function taking:
  n,                 //   n = input
  k = 0              //   k = counter
) =>                 //
  n ?                // if n is not equal to 0:
                     //   == 1st pass: find the requested prime ==
    f(               //   do a recursive call:
      n - (          //     pass the updated value of n
        g = d =>     //     g is a recursive function which takes d = k
                     //     and tests the primality of k + 1:
          ~k % d-- ? //       if d is not a divisor of (k + 1):
                     //       (decrement d afterwards)
            g(d)     //         do recursive calls until it is
          :          //       else:
            !d       //         return true if d = 0
      )(++k),        //     increment k and invoke g
                     //     so we decrement n if k is prime
      k              //     pass k
    )                //   end of recursive call
  :                  // else:
                     //   == 2nd pass: convert the prime to a string ==
    k < 0 ?          //   if k is negative:
      ''             //     return an empty string and stop
    :                //   else:
      f(             //     do a recursive call:
        n,           //       pass n (which is now 0)
        k / 26 - 1   //       pass k / 26 - 1
      ) +            //     end of recursive call
      Buffer([       //     append the letter corresponding to
        k % 26 + 65  //     k mod 26
      ])             //

Arnauld

Posted 2019-10-17T12:39:31.837

Reputation: 111 334

4

Wolfram Language (Mathematica), 53 bytes

Flatten[Tuples[Alphabet[],#]&/@Range@4,1][[Prime@#]]&

Try it online!

J42161217

Posted 2019-10-17T12:39:31.837

Reputation: 15 931

3

Ruby -rprime, 48 47 46 43 bytes

-5 bytes from GB.

->n{(?A..?Z*n).take(Prime.take(n)[-1])[-1]}

Try it online!

Value Ink

Posted 2019-10-17T12:39:31.837

Reputation: 10 608

Use take instead of first for -1 byte. – G B – 2019-10-18T06:13:19.747

And: get nth element of [?a..?a*n] instead of iterating, save 10 bytes. – G B – 2019-10-18T06:14:18.450

@GB the second suggestion unfortunately doesn't work on inputs greater than 5 (both on TIO and my machine). Instead, it runs for a few mintues before it gives IndexError (index 268435456 too big) and then the memory overload from constructing the list seems to cause irb segfault as well. I'll use the first suggestion, though! – Value Ink – 2019-10-18T22:35:46.353

You don't need r, actually, you could just use [-1] – G B – 2019-10-25T07:35:16.493

@GB you are right and I'm amazed that I didn't actually connect the dots. – Value Ink – 2019-10-25T08:24:19.837

2

PHP, 69 bytes

for($n=1,$a=a;$argn||!print$a;$m||--$argn,$a++)for($m=$n++;$n%$m--;);

Try it online!

Night2

Posted 2019-10-17T12:39:31.837

Reputation: 5 484

3The guy who decided that $s="z";echo++$s; should give "aa" is either a genius or insane. – Arnauld – 2019-10-17T17:05:00.087

2

@Arnauld I have seen this in other languages too, but PHP contains about 2 decades of mysteries in it. See this: Try it online! and this: Try it online!

– Night2 – 2019-10-17T17:29:09.623

2

Java (JDK), 170 163 158 157 155 bytes

String p(int n){int c=0,i,v=1;String s="";while(c<n){v++;for(i=2;i<=v;i++)if(v%i<1)break;if(i==v)c++;}while(v>0){s=(char)(~-v%26+97)+s;v=~-v/26;}return s;}

Try it online!

Thanks to @Delta for saving me 14 bytes in total

Batscha2k

Posted 2019-10-17T12:39:31.837

Reputation: 21

hey you can save some bytes Try it online!.

– Delta – 2019-10-24T13:04:10.093

You can also look her to improve your anwser Code Golf in java

– Delta – 2019-10-24T13:04:50.753

1130 bytes – ceilingcat – 2019-10-27T00:51:05.780

1

05AB1E, 18 bytes

<Ø[₂‰˜0KZ27‹#}<Asè

Isn't there a convenient builtin to do this shorter?.. I have the feeling I should be able to use one of the base-conversion builtins here, but it isn't really working out.. Can without a doubt be golfed substantially, though..

Try it online or verify some more test cases.

Explanation:

<              # Decrease the (implicit) input-integer by 1 to make it 0-based
 Ø             # Get the 0-based n'th prime
  [            # Start an infinite loop:
   ₂‰          #  Take the divmod 26
     ˜         #  Flatten the resulting list
      0K       #  And remove any 0s
        Z      #  Get the maximum of the list (without popping)
         27‹   #  If it's smaller than 27:
            #  #   Stop the infinite loop
  }<           # After the loop: decrease all values by 1 to make it 0-based
    A          # Push the lowercase alphabet
     sè        # And index the list of integers into it
               # (after which the resulting character-list is output implicitly as result)

Kevin Cruijssen

Posted 2019-10-17T12:39:31.837

Reputation: 67 575

1

Java, 169 Bytes

isProbablePrime is only valid for 32 bit integers, but it is correct for every 32 bit integer, so this is a valid solution.

(int n)->{int x=-1;for(int i=0;i<n;){x++;if(BigInteger.valueOf(x).isProbablePrime(15))i++;}String r="";while(x>0){x--;int m=x%26;r=(char)(m+97)+r;x=(x-m)/26;}return r;};

Disco Mike

Posted 2019-10-17T12:39:31.837

Reputation: 19

I realize now this is basically redundant to the answer by Batscha2k – Disco Mike – 2019-10-25T21:14:00.477

148 bytes – ceilingcat – 2019-10-26T03:31:18.387

0

Icon, 135 bytes

procedure f(n)
k:=seq()&s:=0&0=k%(i:=2to k)&s+:=1&i=k&s=1&p:=k-1&n-:=1&n=0
t:="";until t[1:1]:=char(97+p%26)&p:=p/26-1&p<0;return t
end

Try it online!

Galen Ivanov

Posted 2019-10-17T12:39:31.837

Reputation: 13 815

0

Pyth, 27 bytes

VQ.VhZIP_b=ZbB;p*\a/Z26@GtZ

Explaination:

                            # Implicit Q = eval(input())
                            # Implicit Z = 0
VQ                          # for N in range(Q)
  .VhZ                      #     for b in infinite_range(Z+1)
      IP_b                  #         if is_prime(b)
          =Zb               #             Z = b
             B;             #             break
             p*\a/Z26       # print("a"*Z//26, end="")
                     @GtZ   # 
                            # Implicit print(G[Z-1])

famous1622

Posted 2019-10-17T12:39:31.837

Reputation: 451