13
\$\begingroup\$

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.

\$\endgroup\$
  • 6
    \$\begingroup\$ Does Your program must work for all primes, given enough time and memory. mean I cannot use, e.g, int in C? \$\endgroup\$ – Dennis Nov 3 '16 at 5:18
21
\$\begingroup\$

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ This 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? \$\endgroup\$ – Value Ink Nov 3 '16 at 4:17
  • 6
    \$\begingroup\$ What... how? That is simply way too quick. \$\endgroup\$ – Qwerp-Derp Nov 3 '16 at 4:23
  • \$\begingroup\$ @ValueInk I've edited my answer. \$\endgroup\$ – Dennis Nov 3 '16 at 4:34
21
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ what on earth!? \$\endgroup\$ – downrep_nation Nov 3 '16 at 11:02
  • 3
    \$\begingroup\$ Do Not Resuscitate \$\endgroup\$ – Digital Trauma Nov 3 '16 at 15:42

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.