17
\$\begingroup\$

You are to create a program which, when given a positive integer \$n\$, outputs a second program. This second program, when run, must take a second positive integer \$x\$ and output one of two distinct values depending on whether \$x\$ is divisible by \$n\$. Those values must be consistent for a given \$n\$ but can be different values for different \$n\$.

However, for each value of \$n\$, the outputted program may only contain bytes whose code points are divisible by \$n\$. You may use any pre-existing code page to score and restrict your answer, but the code pages used for both programs must be the same. In addition, both programs must be in the same language (including versions. Python 3 \$\ne\$ Python 2). Your score is the highest consecutive value of \$n\$ your program works for. That is, your program must work for \$n = 1, 2, 3\$ and \$4\$ in order to have a score of \$4\$. All solutions must work for \$n = 1\$ and \$n = 2\$ at a minimum. The program with the highest \$n\$ wins.

\$\endgroup\$
13
  • 6
    \$\begingroup\$ Sandbox. It was pointed out that Lenguage is able to get an infinitely high score, so I would appreciate people avoiding trying to play that "loophole" - it's not original, nor especially funny, in my opinion. \$\endgroup\$ Dec 1, 2019 at 22:43
  • 4
    \$\begingroup\$ >>> ''.join(chr(i) for i in range(32,127,2)) ' "$&(*,.02468:<>@BDFHJLNPRTVXZ\\^bdfhjlnprtvxz|~'` so I can't even print -- why don't I like restricted-source? ) \$\endgroup\$ Dec 1, 2019 at 23:38
  • 3
    \$\begingroup\$ In what form can the output be -- two distinct values for yes/no? Truthy/falsy? Does this have to be consistent between programs? \$\endgroup\$
    – Doorknob
    Dec 2, 2019 at 3:22
  • 1
    \$\begingroup\$ @Doorknob Two distinct values for yes/no. They must be consistent when run with the same x and n but can be different values for different n \$\endgroup\$ Dec 2, 2019 at 7:38
  • 4
    \$\begingroup\$ I would suggest allowing us to simply list the various 2nd programs for different values of n, since that it is where the crux of the challenge lies. Otherwise, the 1st program will probably often look like if n==1 cat(program for n=1) else if n==2 cat(program for n=2) else if n==3... \$\endgroup\$ Dec 2, 2019 at 8:50

3 Answers 3

19
\$\begingroup\$

05AB1E, n=13, 189 bytes

'Ì6×ÐI" ÿB`0¢ "D"₁h44₁44÷÷÷ÝÐÃм₁hhhÐÃ44u4÷÷÷₁h44₁44÷÷÷ǝ÷ƶê4₁÷÷ 1 Èÿ°À4ô¤¬ xÒÍÍ<_Zÿ11*Ò88w8*н¡Ë18*Ë xh`H°` ₂6₂6₂6$--«üuÆ-«üuÆ-«üuÆΘ ÒæP<<<<<<<<<<n(Zd XXšXXÜýвXšXš¥Æ≠≠ ÿÿð¨и0Ì<0ǝÀ0ÿäÀ`∍`Ā"#Iè

Try it online! or validate codepoint divisibility.

n=1: 1 simply returns 1, since all integers are divisible by 1.

n=2: È is the built-in for that, and its codepoint is even.

n=3 TIO:

3B        # convert input to base 3
  `       # get the last digit
   0¢     # count occurences of 0 in that digit

n=4 TIO:

°         # 10**input
 À        # rotate left
  4ô      # split in chunks of 4 digits
    ¤     # get the last chunk
     ¬    # get the first digit of that chunk

n=5 TIO:

x         # double the input
 Ò        # prime factorization
  ÍÍ<     # subtract 5 from each prime
     _    # equal to 0?
      Z   # maximum

n=6 works identically to n=3.

n=7 TIO:

11*               # multiply the input by 11
   Ò              # prime factorization
    88w8*н        # first digit of 88*8, namely 7
          ¡       # split the prime factorization on 7
           Ë      # are all sublists equal?
            18*   # multiply by 18
               Ë  # are all digits equal?

n=8 TIO:

x         # double the input
 h        # convert to hex
  `       # get the last digit
   H      # convert from hex
    °     # 10**x
     `    # get the last digit

n=9 TIO:

₂           # push 26
 6          # push 6
  $         # push 1 and input
   -        # subtract: 1 - input
    -       # subtract: 6 - (1 - input) = 5 + input
     «      # concatenate: "26" + (5 + input)
      üu    # for each pair of digit, uppercase the second one
            # (used to convert to a list of digits, but drops the first digit)
        Æ   # reduce by subtraction: 6 - sum(digits(5 + input))
            # the above sequence is repeated twice, yielding 6 - sum(digits(sum(digits(sum(digits(5 + input))))))
         Θ  # is it equal to 1?

Note that this algorithm doesn’t work for arbitrary large numbers (but it still works up to 10^10000000000, which is good enough).

n=10 TIO:

Ò                  # prime factorization of the input
 æ                 # power set
  P                # product of each subset
   <<<<<<<<<<      # subtract 10 from each
             n     # square each
              (    # negate each
               Z   # maximum
                d  # is it >= 0?

Divisibility by 10 is usually the easiest, but unfortunately there’s no compliant way to get the last digit.

n=11 TIO:

X                  # push 1
 Xš                # prepend 1: [1, 1]
   XXÜ             # 1, with trailing 1s trimmed off (aka empty string)
      ý            # join: 11
       в           # convert input to base 11
        Xš         # prepend 1
          Xš       # prepend 1
            ¥      # deltas
             Æ     # reduce by subtraction
              ≠≠   # is it equal to 1?

n=12 TIO:

ÌÌÌÌÌÌÌÌÌÌÌÌ           # add 24 to the input
 ð                     # space character        
  ¨                    # drop the last character (empty string)
   и                   # make a list of input empty strings
    0Ì<0ǝ              # replace the first element with 1
     À                 # rotate left
      0ÌÌÌÌÌÌä         # split in 12 parts of approximately equal length
                       # (if the input isn’t divisible by 12, the first sublists will be longer)
       À               # rotate left
        `              # dump all to the stack
         ∍             # cycle the before-last sublist to match the length of the last sublist
          `            # get the last element
           Ā           # truthify

n=13 ([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]): TIO

₁h44₁44÷÷÷               # hex(256) / (44 / (256 / 44)) = 12
 Ý                       # range [0..12]
  ÐÃ                     # duplicate (well, triplicate then pop the third copy)
    м                    # remove (leaves a list of 13 empty strings)
     ₁hhhÐÃ44u4÷÷÷       # 13
      ₁h44₁44÷÷÷         # 12
       ǝ                 # set the element at index 12 to 13
        ÷                # integer division
                         # dividing by the empty string yields the numerator,
                         # so the result is 12x the input, followed by input / 13
         ƶ               # multiply each element by its index
                         # the last element is now equal to input iff 13 divides input
          ê              # sorted uniquify
           4₁÷÷          # divide by 0 (sets all elements to 0)
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Divisibility by 13 (I've added some comments to explain what's going on). That took a while.. >.> But now you can continue to the (hopefully much easier) 14. :D \$\endgroup\$ Dec 3, 2019 at 14:46
  • \$\begingroup\$ I was right now editing my answer to n=13. \$\endgroup\$
    – Grimmy
    Dec 3, 2019 at 14:47
  • \$\begingroup\$ Dang.. :( And here I was proud of finally finding this, lol. (Ignore the typos in my explanation and that trailing ÷.. Too late to edit my comment above now.) I'm curious about your n=13 in that case. \$\endgroup\$ Dec 3, 2019 at 14:54
  • 1
    \$\begingroup\$ Oh, nice approach as well! And I indeed had the feeling my push 13 could be done shorter, but didn't bothered to look further after I found it. Finding the 13 wasn't the problem, it was somehow using it to check divisibility. :) The ƶ certainly has a key-role with the bytes that are left. Anyway, I won't be bothering to look at the n=14 characters unless your answer is unchanged for multiple days, since as with answering answers in general, you're sometimes faster, and always shorter. ;p \$\endgroup\$ Dec 3, 2019 at 15:44
  • 1
    \$\begingroup\$ @pppery codegolf.stackexchange.com/questions/196556/… \$\endgroup\$
    – Grimmy
    Feb 26, 2020 at 7:00
5
\$\begingroup\$

Japt, 108 bytes, n = 6

g[P"1""v""¥r3""ì4 ¬t(´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D  <(°D,°D""s7-2




ë2,-1
Z<7-2-2-2""NÌÀNÌr6"]

Try it

1 (1, no value)

1       //Output 1

2 (1, 0)

v       //Builtin, is divisible by 2

3 (true, false)

¥       //Input equals
 r3     //Input rounded to nearest multiple of 3

4 (true, false)

ì4                                                         //Convert to base-4 digits
   ¬                                                       //Concatenate to string
      ´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D,´D            //--D 14 times, D = -1 now
    t(                                                     //Take -1 element of the string
                                                 <         //Is that less than
                                                  (°D,°D   //++D 2 times (1)

5 (true, false)

s7-2       //Convert to base-5 string




ë2,-1      //Get every 2nd element of the string, starting from the last index, save in variable Z 
Z<7-2-2-2  //Is Z less than 1?

6 (false, true)

NÌ       //Last element of input array
  À      //Doesn't equal
   NÌr6  //Last element of input array rounded to nearest multiple of 6      

Sadly, I think it's not possible to get n=7. There is not a single parentheses character, and there is no way to access the input variable U besides ¡ (maps each element in array/string to undefined by default, returns NaN if caller is number) and Ë (maps each element in string/array to iteself, returns NaN if caller is number). And since we don't have } or à (used to close functions), we cannot use ¡ or Ë effectively at all.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 103 bytes, n= 9

“1“d2Ṫ“3ḍ“d4 0ị⁼0”⁾d«;“2_7ƊȦ”¤ṭ“HHHHHN6ƭ€0ị<0”ṭ“~“8¡~“1߬?”j”‘¤ṭ“00000008 8ƭ⁸¡”ṭ”‘x4¤”~;$;“6¡~ñ-H?”¤ṭ⁸ị

Try it online!

Return values:

n div not
1 1    -
2 0   1
3 1   0
4 1   0
5 1   0
6 1   0
7 1   error
8 8   0
9 -1  error

Explanation

n=1

1 | 1

n=2

d2  | divmod 2
  Ṫ | tail

n=3

3ḍ | divisible by 3

n=4

d4      | divmod 4
   0ị   | last
     ⁼0 =0

n=5

d    Ɗ  | divmod following as a monad:
 «2     | - min of input and 2
   _7   | - minus 7
      Ȧ | each and all

n=6

HHHHHN6ƭ€ | For each number 1..z, half five out of every six numbers and negate the sixth 0ị | Last <0 | <0

n=7

~          | Bitwise not (effectively -1-z)
 ‘8¡       | Increment by 1 eight times
    ~      | Bitwise not
     ‘     | Increment by 1
      1߬? | If non-zero, call this link again. Otherwise return 1

n=8

00000008 8ƭ⁸¡ | Loop z times; for 7 out of 8 iterations, set value to 0 and for the eighth, set to 8

n=9

~            | Bitwise not
 ‘‘‘         | Increment by 1 three times
    ‘6¡      | Increment by 1 six times
       ~ñ-H? | If non-zero, call this link again. Otherwise return -1

Note (as pointed out by @Grimmy) that n=7 and n=9 will fail on larger inputs (>14651 and 14202 respectively) because of the recursion limits in Jelly/Python.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.