Letters associated with prime numbers

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!

• 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 Oct 17 '19 at 12:50
• Perhaps include test cases beyond Z? – HyperNeutrino Oct 17 '19 at 13:38
• related; 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 Oct 17 '19 at 14:43
• Suggested test cases of big numbers so you can verify the letter wrapping works correctly. – Veskah Oct 17 '19 at 17:13
• Add the test case 123 -> za. Several current answers get it wrong. – benrg Oct 17 '19 at 21:40

Python 3,2622362091961791149392 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!

• Also, make sure to check out the Python tips thread. – AdmBorkBork Oct 17 '19 at 15:13
• You 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 Oct 17 '19 at 15:31
• 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 Oct 17 '19 at 16:43
• 114 bytes with the same approach Try it online! – mypetlion Oct 17 '19 at 18:04
• 93 bytes by using a better prime algorithm: Try It Online! – dingledooper Oct 18 '19 at 0:45

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
])             //


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

• For input 123 the output should be za but this code currently prints AZA. – benrg Oct 17 '19 at 21:52
• Ah it's bijective base decompression, I guess it has to be ÆNḃ26ịØA then :( – Jonathan Allan Oct 17 '19 at 22:10
• ...and that'll be exactly the same as the other Jelly entry becomes. – Jonathan Allan Oct 17 '19 at 22:13

Wolfram Language (Mathematica), 53 bytes

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


Try it online!

Ruby-rprime, 484746 43 bytes

-5 bytes from GB.

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


Try it online!

• Use take instead of first for -1 byte. – G B Oct 18 '19 at 6:13
• And: get nth element of [?a..?a*n] instead of iterating, save 10 bytes. – G B Oct 18 '19 at 6:14
• @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 Oct 18 '19 at 22:35
• You don't need r, actually, you could just use [-1] – G B Oct 25 '19 at 7:35
• @GB you are right and I'm amazed that I didn't actually connect the dots. – Value Ink Oct 25 '19 at 8:24

PHP, 69 bytes

for($n=1,$a=a;$argn||!print$a;$m||--$argn,$a++)for($m=$n++;$n%$m--;);  Try it online! • The guy who decided that $s="z";echo++\$s; should give "aa" is either a genius or insane. – Arnauld Oct 17 '19 at 17:05
• @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 Oct 17 '19 at 17:29

Java (JDK), 170163158157 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

• hey you can save some bytes Try it online!. – Delta Oct 24 '19 at 13:04
• You can also look her to improve your anwser Code Golf in java – Delta Oct 24 '19 at 13:04
• 130 bytes – ceilingcat Oct 27 '19 at 0:51

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..

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)


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;};

• I realize now this is basically redundant to the answer by Batscha2k – Disco Mike Oct 25 '19 at 21:14
• 148 bytes – ceilingcat Oct 26 '19 at 3:31

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!

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])