Primes with a twist

13

1

Note: this is heavily inspired by this question.

Task:

Your task is to generate the nth prime, when each character of your program is repeated n times in place.

Let's say your program is:

Derp

Every character in there is repeated once, so it should output 2.

When every character is duplicated in place, like so:

DDeerrpp

This should generate the 2nd prime, which is 3.

Specs:

  • Your program must accept no input, and output the respective prime.
  • Your program must work for all primes, given enough time and memory.
  • Leading and trailing newlines are fine.
  • The output must be in the default base of the language - if there is no default, any base is fine.

This is , so shortest code in bytes wins.

clismique

Posted 2016-11-03T03:44:12.623

Reputation: 6 600

6Does Your program must work for all primes, given enough time and memory. mean I cannot use, e.g, int in C? – Dennis – 2016-11-03T05:18:21.123

Answers

21

Jelly, 13 bytes

“Ŀo‘’FQỌµḟ;¹V

Try it online! or run the first ten programs.

Background

Jelly has several different types of string literals; all of them start with a . If the literal contains more than one , a string array is returned, and separates the strings from each other.

For example, “abc“def” yields ['abc', 'def'].

Depending on the last character of the literal (any of ”«»‘’, where « is currently unimplemented), one can choose between the different types of literals. For , we get the code points in Jelly's code page instead of the corresponding Unicode characters.

For example, “abc“def‘ yields [[97, 98, 99], [100, 101, 102]].

The literals in the first three programs correspond to the following code point arrays.

“Ŀo‘           -> [199, 111]
““ĿĿoo‘        -> [[], [199, 199, 111, 111]]
“““ĿĿĿooo‘     -> [[], [], [199, 199, 199, 111, 111, 111]]

How it works (n = 3)

“““ĿĿĿooo‘‘‘’’’FFFQQQỌỌỌµµµḟḟḟ;;;¹¹¹VVV  Main link. Implicit argument: 0

“““ĿĿĿooo‘                               Yield the 2D array
                                         [[], [], [199, 199, 199, 111, 111, 111]].
          ‘‘                             Increment twice, yielding
                                         [[], [], [201, 201, 201, 113, 113, 113]].
            ’’’                          Decrement thrice, yielding
                                         [[], [], [198, 198, 198, 110, 110, 110]].
               F                         Flatten, yielding
                                         [198, 198, 198, 110, 110, 110].
                FF                       Twice more. Does nothing.
                  Q                      Unique; yield [198, 110].
                   QQ                    Twice more. Does nothing.
                     Ọ                   Unordinal; convert the Unicode code points
                                         198 and 110 to 'Æ' and 'n'.
                      ỌỌ                 Twice more. Does nothing.
                        µµµ              Begin three monadic chains, all with
                                         argument s := "Æn".
                           ḟ             Filter-false; remove the characters of s
                                         from s, yielding "".
                            ḟḟ           Twice more. Does nothing.
                              ;;;¹       Concatenate with s three times, yielding
                                         "ÆnÆnÆn".
                                  ¹¹     Identity function. Does nothing.
                                    V    Eval the resulting Jelly code, i.e.,
                                         call the next-prime atom thrice, with
                                         initial implicit argument 0.
                                     VV  Eval two more times. This is a no-op
                                         on integers.

Dennis

Posted 2016-11-03T03:44:12.623

Reputation: 196 637

1This definitely is in need of an explanation... and depending on how it's laid out, it might be the only language that can do this? – Value Ink – 2016-11-03T04:17:51.550

6What... how? That is simply way too quick. – clismique – 2016-11-03T04:23:25.033

@ValueInk I've edited my answer. – Dennis – 2016-11-03T04:34:50.593

21

GS2 (commit 67fea47), 6 3 bytes

dnR

How it works

Like most commands, GS2‘s d is overloaded. Initially, there's an empty list (the input or the lack thereof) on the stack, so d computes its sum. After the first iteration, there's a 0 on the stack, and d computes its parity (also 0). Thus, no matter how many times we repeat d, the result will always be a single 0.

The next part is straightforward. The command n pops the top of the stack and pushes the next prime number. Thus, repeating n k times computes the k prime.

Finally, the command simply applies str to the top of the stack, which converts a number to its string representation and does no affect strings. Thus, no matter how many times we repeat R, the result will be the string representation of the k prime.

Dennis

Posted 2016-11-03T03:44:12.623

Reputation: 196 637

2what on earth!? – downrep_nation – 2016-11-03T11:02:25.060

3Do Not Resuscitate – Digital Trauma – 2016-11-03T15:42:51.393