13
\$\begingroup\$

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!

\$\endgroup\$
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ – caird coinheringaahing Oct 17 at 12:50
  • 9
    \$\begingroup\$ Perhaps include test cases beyond Z? \$\endgroup\$ – HyperNeutrino Oct 17 at 13:38
  • 6
    \$\begingroup\$ 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. \$\endgroup\$ – Giuseppe Oct 17 at 14:43
  • 1
    \$\begingroup\$ Suggested test cases of big numbers so you can verify the letter wrapping works correctly. \$\endgroup\$ – Veskah Oct 17 at 17:13
  • 9
    \$\begingroup\$ Add the test case 123 -> za. Several current answers get it wrong. \$\endgroup\$ – benrg Oct 17 at 21:40

11 Answers 11

8
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ Also, make sure to check out the Python tips thread. \$\endgroup\$ – AdmBorkBork Oct 17 at 15:13
  • 1
    \$\begingroup\$ You might want to give your answer a comb over for extra spaces, I can see 5 spaces right now that are not neccessary. \$\endgroup\$ – Wheat Wizard Oct 17 at 15:31
  • 1
    \$\begingroup\$ It's not required but Try it online! is a cool site that counts bytes, runs code, and can generate a CG&CC post \$\endgroup\$ – Veskah Oct 17 at 16:43
  • 1
    \$\begingroup\$ 114 bytes with the same approach Try it online! \$\endgroup\$ – mypetlion Oct 17 at 18:04
  • 1
    \$\begingroup\$ 93 bytes by using a better prime algorithm: Try It Online! \$\endgroup\$ – dingledooper Oct 18 at 0:45
4
\$\begingroup\$

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
      ])             //
\$\endgroup\$
4
\$\begingroup\$

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

Wolfram Language (Mathematica), 53 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ruby -rprime, 48 47 46 43 bytes

-5 bytes from GB.

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

Try it online!

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

PHP, 69 bytes

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

Try it online!

\$\endgroup\$
  • 3
    \$\begingroup\$ The guy who decided that $s="z";echo++$s; should give "aa" is either a genius or insane. \$\endgroup\$ – Arnauld Oct 17 at 17:05
  • 2
    \$\begingroup\$ @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! \$\endgroup\$ – Night2 Oct 17 at 17:29
2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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;};
\$\endgroup\$
  • \$\begingroup\$ I realize now this is basically redundant to the answer by Batscha2k \$\endgroup\$ – Disco Mike Oct 25 at 21:14
  • \$\begingroup\$ 148 bytes \$\endgroup\$ – ceilingcat Oct 26 at 3:31
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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])
\$\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.