26
\$\begingroup\$

My robot for generating Mad Libs-style challenges has gone rogue! To defeat it, I need you to write code that can solve all the challenges it can crank out.

Your input is three selections for the blanks A, B, C, each from (1,2,3), specifying the sequence challenge to solve, and the number \$n\$ that's the input to the challenge.

Challenges

Given a positive integer \$n\$, output (A) ________

A:
(1) the \$n\$'th
(2) whether \$n\$ is a
(3) the smallest number above \$n\$ that's a

positive (B) _______

B:
(1) Fibonacci number
(2) triangular number
(3) multiple of 3

whose base-10 representation (C) _______

C:
(1) starts and ends with the same digit.
(2) is in sorted order.
(3) doesn't have any 1's.

Example

Given selections (A, B, C) = (3, 1, 1) and n=13, the challenge is "Given a positive integer \$n\$, output (A3) the smallest number above \$n\$ that's a positive (B1) Fibonacci number whose base-10 representation (C1) starts and ends with the same digit." With input n=13, the Fibonacci numbers bigger than \$13\$ start out as \$21, 34, 55, 89, ...\$, and the first one starting and ending with the same digit is is 55. So you should output 55.

Details

  • You may take the fill-in-the-blank selections one-indexed (1, 2, 3) or zero-indexed (0, 1, 2).
  • Note that we're restricting the sequences to positive values, so none of them contain zero. They start as
    • Fibonacci: \$1, 1, 2, 3, ...\$
    • Triangular: \$1, 3, 6, 10, ...\$
    • Multiple of 3: \$3, 6, 9, 12, ...\$
  • For choice A1, outputting the \$n\$'th element, \$n\$ is one-indexed. So, the n=1 Fibonacci number is 1, the n=2 Fibonacci number is also 1, n=3 gives 2, and so on.
  • For deciding whether n is in the sequence (A2), you may use any two consistent outputs -style.
  • You may assume that each sequence has an infinite number of terms meeting each condition C, and not worry about what happens with machine overflows and the like.
  • For C2, digits in sorted order can have "ties", such as 377.

Test cases

Here are all outputs for every combination with n=10, as (A,B,C), n: result. For these, we use one-indexed selections, and for A2 we use 1/0 for True/False

(1, 1, 1), 10: 165580141
(1, 1, 2), 10: 89
(1, 1, 3), 10: 987
(1, 2, 1), 10: 1431
(1, 2, 2), 10: 78
(1, 2, 3), 10: 276
(1, 3, 1), 10: 222
(1, 3, 2), 10: 36
(1, 3, 3), 10: 42
(2, 1, 1), 10: 0
(2, 1, 2), 10: 0
(2, 1, 3), 10: 0
(2, 2, 1), 10: 0
(2, 2, 2), 10: 0
(2, 2, 3), 10: 0
(2, 3, 1), 10: 0
(2, 3, 2), 10: 0
(2, 3, 3), 10: 0
(3, 1, 1), 10: 55
(3, 1, 2), 10: 13
(3, 1, 3), 10: 34
(3, 2, 1), 10: 55
(3, 2, 2), 10: 15
(3, 2, 3), 10: 28
(3, 3, 1), 10: 33
(3, 3, 2), 10: 12
(3, 3, 3), 10: 24

The first 10 outputs, n=1 to n=10, for each challenge combination are:

(1, 1, 1): 1, 1, 2, 3, 5, 8, 55, 17711, 63245986, 165580141,
(1, 1, 2): 1, 1, 2, 3, 5, 8, 13, 34, 55, 89,
(1, 1, 3): 2, 3, 5, 8, 34, 55, 89, 233, 377, 987,
(1, 2, 1): 1, 3, 6, 55, 66, 171, 595, 666, 1081, 1431,
(1, 2, 2): 1, 3, 6, 15, 28, 36, 45, 55, 66, 78,
(1, 2, 3): 3, 6, 28, 36, 45, 55, 66, 78, 253, 276,
(1, 3, 1): 3, 6, 9, 33, 66, 99, 111, 141, 171, 222,
(1, 3, 2): 3, 6, 9, 12, 15, 18, 24, 27, 33, 36,
(1, 3, 3): 3, 6, 9, 24, 27, 30, 33, 36, 39, 42,
(2, 1, 1): 1, 1, 1, 0, 1, 0, 0, 1, 0, 0,
(2, 1, 2): 1, 1, 1, 0, 1, 0, 0, 1, 0, 0,
(2, 1, 3): 0, 1, 1, 0, 1, 0, 0, 1, 0, 0,
(2, 2, 1): 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
(2, 2, 2): 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
(2, 2, 3): 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
(2, 3, 1): 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
(2, 3, 2): 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
(2, 3, 3): 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
(3, 1, 1): 2, 3, 5, 5, 8, 8, 8, 55, 55, 55,
(3, 1, 2): 2, 3, 5, 5, 8, 8, 8, 13, 13, 13,
(3, 1, 3): 2, 3, 5, 5, 8, 8, 8, 34, 34, 34,
(3, 2, 1): 3, 3, 6, 6, 6, 55, 55, 55, 55, 55,
(3, 2, 2): 3, 3, 6, 6, 6, 15, 15, 15, 15, 15,
(3, 2, 3): 3, 3, 6, 6, 6, 28, 28, 28, 28, 28,
(3, 3, 1): 3, 3, 6, 6, 6, 9, 9, 9, 33, 33,
(3, 3, 2): 3, 3, 6, 6, 6, 9, 9, 9, 12, 12,
(3, 3, 3): 3, 3, 6, 6, 6, 9, 9, 9, 24, 24,
\$\endgroup\$
  • \$\begingroup\$ In condition A2, must the two outputs be the same for all 9 values of (B, C)? Or is it OK if we output (say) 0 and 1 for Fibonacci sequences, but 2 and 4 for triangular numbers? \$\endgroup\$ – Robin Ryder Mar 7 at 21:17
  • \$\begingroup\$ @RobinRyder I'll say they can be different for each one, since conceptually each set of conditions makes for its own challenge. \$\endgroup\$ – xnor Mar 7 at 21:19
4
\$\begingroup\$

JavaScript (ES6),  158 ... 145  144 bytes

Takes input as (a,b,c,n), with \$a\$, \$b\$ and \$c\$ 0-indexed.

(a,b,c,n)=>(g=x=>[(d=[...x+''],i+=C=![d[0]-x%10,d+''>d.sort(),/1/.test(x)][c])>n,x>=n,x>n&C][a]?a-1?x:C&x==n:g([y,x+y,x+3][b],y+=b||x))(i=0,y=1)

Try it online!

Commented

(a, b, c, n) => (              // given the challenge parameters ...
  g = x =>                     // g is a recursive function taking the current value x
    [                          // this is the array for A
      (                        //   a = 0:
        d = [...x + ''],       //     d[] = array of digits in x
        i += C =               //     increment i if C is true
          ![                   //     set C if the condition is false:
            d[0] - x % 10,     //       c = 0: the 1st and the last digits are not equal
            d + '' > d.sort(), //       c = 1: the digits are not in sorted order
            /1/.test(x)        //       c = 2: there's a '1' in x
          ][c]                 //     pick the relevant condition for C
      ) > n,                   //     exit if i is greater than n
      x >= n,                  //   a = 1: exit if x is greater than or equal to n
      x > n & C                //   a = 2: exit if x is greater than n and C is true
    ][a]                       // pick the relevant condition for A
    ?                          // if we're done:
      a - 1 ? x                //   if a is not equal to 1, return x
            : C & x == n       //   otherwise, test whether C is true and x is equal to n
    :                          // else:
      g(                       //   do a recursive call:
        [                      //     update x:
          y,                   //       b = 0: x = y (Fibonacci)
          x + y,               //       b = 1: x += y (triangular numbers)
          x + 3                //       b = 2: x += 3 (multiples of 3)
        ][b],                  //     pick the relevant update for B
        y += b || x            //     if b = 0, add x to y (for Fibonacci)
                               //     otherwise, add b to y (we actually just need to
                               //     add 1 for triangular numbers; for multiples of 3,
                               //     y is ignored anyway)
      )                        //   end of recursive call
)(i = 0, y = 1)                // initial call to g with x = i = 0 and y = 1
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Python 3, 426 \$\cdots\$ 204 181 bytes

Saved 9 bytes thanks to RGS!!!

def f(a,b,c,n):
 j=i=l=x=0;y=1
 while(i<n,l<n,l<=n)[a]:
  j+=1;x,y=y,x+y;p=(x,j*-~j//2,3*j)[b];s=str(p)
  if(s[0]==s[-1],[*s]==sorted(s),not'1'in s)[c]:l=p;i+=1
 return(l,l==n)[a%2]

Try it online!

\$(a,b,c)\$ are zero-indexed and ...whether \$n\$ is a... returns True/False.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can save some bytes by doing the middle test on your if like I'm doing \$\endgroup\$ – RGS Mar 8 at 19:27
  • 1
    \$\begingroup\$ @RGS Much better comparing two lists, thanks! :-) See you've taken the sequence gen to the next level, nice one! :D \$\endgroup\$ – Noodle9 Mar 8 at 20:37
2
\$\begingroup\$

Wolfram Language (Mathematica), 245 bytes

Input {a,b,c,n}
True/False for A(2)

(d=#4;a=#;c=#3;n={#&@@(i=IntegerDigits)@#==#~Mod~10&,OrderedQ@i@#&,i@#~FreeQ~1&};{t=v=1;While[v<=d,While[!n[[c]][#@t++]];v++];#[t-1],!FreeQ[#[w=Range[3d]],d]&&n[[c]]@d,#&@@Select[Select[#@w,#>d&],n[[c]]]}[[a]]&[{Fibonacci,#(#+1)/2&,3#&}[[#2]]])&

Try it online!

Here are all test cases for n=1 to 10

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

R, 199 bytes

f=function(A,B,C,n){u=c(sum(1:B),1+2*(B>2))
while(sum(v<-grepl(c("^(.)(.*\\1)?$","^1*2*3*4*5*6*7*8*9*$","^[^1]*$")[C],u))<n+2)u=c(u[1]+c(u[2],sum(u^0)+1,3)[B],u)
w=u[v]
c(w[3],n%in%w,min(w[w>n]))[A]}

Try it online!

Conditions C1-C3 are checked via regex.

In all cases, it computes the first n+2 terms verifying the condition; this is always enough. They are stored in reverse in w. Then:

  • for A1, take w[3]
  • for A2, check whether n appears in w
  • for A3, take the min of the elements of w which are greater than n.

Details of the regexes:

  • C1: ^(.)(.*\\1)?$: get first digit with ^(.), then find either the end of the number (correspond to a 1-digit number) or any sequence followed by the same digit again
  • C2: ^1*2*3*4*5*6*7*8*9*$: any number of 1s, then any number of 2s, then... then any number of 9s
  • C3: ^[^1]*$ only digits which are not 1

w is built as the elements of sequence u verifying the regex, where u is the Fibonacci/Triangular/multiples of 3 sequence, built iteratively by adding the appropriate increment at each step.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 58 bytes

ị"“ÆḞ“+1c2“×3”,“.ịEƊ“ṢƑ“ċ1¬$”,“ṖṪ“e@“>Ƈ”ż“¶1ÇD“Ɗ⁸+1¤#Ç”FvḢ

Try it online!

A dyadic link taking the choices in the order B, C, A as the left argument and n as the right argument. Builds a program from component parts and then executes it. The presence of two ones at the start of the Fibonacci sequence added an extra four characters!

Explanation

ị"          | Index into the following list of strings, zipped with the first argument (so the first choice picks from the first list of strings, the second from the second and the third from the third):

First set of choices

“ÆḞ         | - Fibonacci sequence
“+1c2       | - Triangular numbers
“×3”,       | - Multiply by 3

Second set of choices

“.ịEƊ       | - Extract last and first terms and check if equal
“ṢƑ         | - Check whether invariant when sorted
“ċ1¬$”,     | - Count 1s and then logical not

Third set of choices

“ṖṪ         | - Remove last then take last remaining
“e@         | - Exists (with arguments reversed)
“>Ƈ”        | - Keep those greater than

Fixed bits of generated program

ż           | Zip with strings representing the following
“¶1ÇD       | - Newline, then 1 followed by call of previous link followed by D convert to decimal digits
“Ɗ⁸+1¤#Ç”   | - Last three as a monad called to find n+1 matching terms

Code to execute the generated program

F           | Flatten
 v          | Call as Jelly code using n as argument
  Ḣ         | Head (needed when A is 3)

Example generated code for [1,1,1]:

ÆḞ
1ÇD.ịEƊƊ⁸+1¤#ÇṖṪ
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Python 2, 162 bytes

Inputs A, B, C are 0-indexed.

a,b,c,n=input();
i=0;x=k=1
while[n,i<n,i<=n][a]|1-x:i,k=[k,k+i,i+3][b],k+(b or i);s=`i`;x=[s[0]==s[-1],sorted(s)==list(s),not'1'in s][c];n-=x>a
print[i,i==n][a%2]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 49 44 bytes

∞3*∞Åf∞ηO)³èʒ1å≠y¬Å¿yD{Q)Iè}D¹²i<èë²<i.ië›Ïн

Input is 1-based in the order \$n,A,B,C\$, and for A2 it'll output \$0\$/\$1\$ for truthy/falsey respectively.

-5 bytes thanks to @Grimmy.

Try it online or verify all test cases.

Explanation:

∞           # Push an infinite list of positive integers: [1,2,3,...]
 3*         # Multiply each by 3
∞           # Push an infinite list of positive integers again
 Åf         # Get the (0-based) n'th Fibonacci of each
∞           # Push an infinite list of positive integers again
 η          # Get all prefixes
  O         # And sum each prefix
   )        # Wrap all infinite lists into a list
    ³è      # And index the third input (B) into it (0-based and with wraparound)
ʒ           # Filter this list by:
 1å         #  Check if the current value contains a 1
   ≠        #  And invert the boolean
 y          #  Push the current value again
  ¬         #  Push its head (without popping)
   Å¿       #  And check if the value ends with this head
 y          #  Push the current value again
  D{        #  Create a copy, and sort its digits
    Q       #  Check if both are equal
     )      #  Wrap all three checks into a list
      Iè    #  And index the next fourth input (C) into it (0-based and with wraparound)
}D          # After the filter: duplicate the resulting filtered infinite list
  ¹         # Push the first input (n)
   ²i       # If the second input (A) is exactly 1:
     <      #  Decrease `n` by 1 to make it 0-based
      è     #  And use it to index into the filtered infinite list
    ë²<i    # Else-if the second input (A) is 2:
        .i  #  Check if input `n` is in the filtered infinite list
    ë       # Else (second input (A) is 3):
     ›      #  Check for each value whether `n` is larger than it
      Ï     #  Only keep the values at the truthy indices
            #  (hence the duplicate after the filter)
       н    #  And only keep its first value
            # (after which the result is output implicitly)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Oh, you beat me to it (: Here's what I had (order n, A, B, C). Those are remarkably different, there's likely something to gain by merging the two. \$\endgroup\$ – Grimmy Mar 9 at 12:57
  • \$\begingroup\$ @Grimmy I had infinite lists at first as well, but the å screwed that over, hence the use of žm now. I also use 0-based lists, with 1-based input, so it'll ignore the leading 0s. But I think we can indeed save some bytes by using parts from both our programs. :) \$\endgroup\$ – Kevin Cruijssen Mar 9 at 13:01
  • \$\begingroup\$ Whoops, you're right, I need to use .i instead of å. Still comes out as 44 by stealing your ¬Å¿: TIO. \$\endgroup\$ – Grimmy Mar 9 at 13:03
  • \$\begingroup\$ @Grimmy Oh, didn't knew about .i! That's pretty cool. Was actually working on a test suite with all test cases, which is failing a bit, so will combine our programs first instead. \$\endgroup\$ – Kevin Cruijssen Mar 9 at 13:14
  • \$\begingroup\$ @Grimmy Your λ+} failed due to the implicit input, so I use ∞Åf instead. I've also kept the 1-based indexing, since it didn't matter for the byte-count anyway. Thanks for -5 though. :) \$\endgroup\$ – Kevin Cruijssen Mar 9 at 13:28
1
\$\begingroup\$

Python 3, 182 173 bytes

def f(a,b,c,n):
 u=i=l=0;v=b&2|1
 while(i<n,l<n,l<=n)[a]:
  u,v=v,[u+v,2*v-u+1,v+3][b];s=str(u)
  if(s[0]==s[-1],[*s]==sorted(s),not'1'in s)[c]:l=u;i+=1
 return(l,l==n)[a%2]

Try it online!

The skeleton was borrowed from Noodle9's Python answer, but I'm building the sequence of values in a different way.

Thanks to @xnor for saving me 9 bytes.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I like how you're building the sequences iteratively. A golf: the initial variable assignments can be u=i=l=0;v=b&2|1. \$\endgroup\$ – xnor Mar 8 at 18:36
1
\$\begingroup\$

Bash + GNU utilities, 277 bytes

b=(d*5*d4+dvd*-r4-dvd*-* 8*1+dvd*- d3/3*-)
c=('^(.)(.*\1)?$' ^`seq -s* 9`*$ -v\ 1)
f()(egrep -q ${c[$3]}<<<$5&&dc<<<"$5 ${b[$2]}d/p"||echo 1)
case $1 in
0)for((n=1;k<$4;n++,k+=!`f $* $n`+($n$2$3<102))){ :;};$[--n];;1)n=`f $* $4`;;2)for((n=$4+1;`f $* $n`;n++)){ :;}
esac
echo $n

Try it online!

Input is passed in the arguments, in the order A B C n. The values A, B, and C are 0-based.

Output is on stdout. For A=1, the output is 0 for truthy and 1 for falsey.

There is lots of spurious output on stderr, which is allowed under standard golfing rules, but if you want to try it out yourself, I recommend a command like:

madlibs 0 2 1 5 2>/dev/null

so that your output doesn't have stderr output included in it.


The TIO link shows all 27 combinations of A, B, and C, and all values of n from 1 to 6. (TIO times out if I use larger values for n, but I've tested more on my own computer.)

By the way, this script requires GNU utilities. In particular, it won't work on OS X (and probably other BSD systems), because seq is different there.


Some comments on the code:

b is an array of three dc scripts for testing whether a number is a Fibonacci number, a triangular number, or a multiple of 3, respectively. (Each dc script outputs 0 for true and 1 for false.)

c is an array of three regexes which match an input which starts and ends with the same digit, or is in sorted order, or doesn't have any 1s, respectively. The regex for being in sorted order is computed using seq, not given explicitly, because the code is shorter that way.

f is a function that tests to see if input $5 satisfies the requirements with B=$2 and C=$3. It prints 0 for true, and 1 for false. (This works by calling the appropriate dc script from b and matching against the appropriate regex from c.)

The case statement computes the desired answer by calling f as needed to test whether the appropriate numbers have the required properties.

| improve this answer | |
\$\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.