Challenge
Here at PPCG, we sure do like our sequences, so here's (削除) a fun (削除ここまで) another one.
Let's define a(n) as being the smallest non-negative integer X that is not equal to any a(k) (0 < k < n), and a(n-1) and X do not share any decimal digits. a(0) = 0
Given an input n > 0, output such a(n).
For example, for input n = 13, we have a(13) = 20, since a(12) = 11 and 20 is the smallest non-negative integer we haven't seen yet that doesn't share any decimal digits with 11.
Sequence
Here are the first 20 terms to get you started. This is sequence A067581 on OEIS.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 11, 20, 13, 24, 15, 23, 14, 25
Rules
- The input and output can be assumed to fit in your language's native integer type.
- The input and output can be given in any convenient format.
- You can choose to either 0-index, as I am here in my examples, or 1-index for your submission. Please state which you're doing.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
8 Answers 8
Python 2, 85 bytes
-1 byte thanks to Dead Possum
n=0,
exec"i=0\nwhile set(`i`)&set(`n[-1]`)or i in n:i+=1\nn+=i,;"*input()
print n[-1]
-
\$\begingroup\$
n=0,for -1 byte? \$\endgroup\$Dead Possum– Dead Possum2017年08月10日 15:55:18 +00:00Commented Aug 10, 2017 at 15:55
Japt, 18 bytes
@A{!ZøA «As oX}a}g
Test it online! I've just added the g feature used here, but it's something I've been meaning to add for a long time (and this pushed me over the edge, because my non-g solution was around 35 bytes).
Explanation
@ A{!ZøA « As oX}a}g
XYZ{A{!ZøA &&!As oX}a}gU
Implicit: U = input integer
{ }gU Starting with [0, 1], return the U'th item generated by
XYZ{ } this function: (X = previous item, Y = index, Z = full array)
A{ }a Return the smallest non-negative integer A where
!ZøA && Z does not contain A (A is not yet in the sequence), and
!As oX A.toString() does not contain any of the same chars as X.
Implicit: output result of last expression
-
\$\begingroup\$ This makes my head hurt! But, then I've barely glanced at any of the function methods in Japt. \$\endgroup\$Shaggy– Shaggy2017年08月10日 20:29:05 +00:00Commented Aug 10, 2017 at 20:29
-
\$\begingroup\$ Isn't the default rule that you can't add something to the language after the question is given? It would be trivial otherwise to just always create a new built-in that solves the challenge, making them all arbitrarily short. \$\endgroup\$trlkly– trlkly2017年08月11日 13:02:32 +00:00Commented Aug 11, 2017 at 13:02
-
\$\begingroup\$ @trlkly I believe it's allowed within good judgement. The built-in I added is much more general-purpose than just for this one answer. I think someone could theoretically add a built-in that solves the challenge entirely, but an answer like that would be sure to be received very poorly. \$\endgroup\$ETHproductions– ETHproductions2017年08月11日 13:53:53 +00:00Commented Aug 11, 2017 at 13:53
-
\$\begingroup\$ Mind if I add a test suite to your answer? \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年08月10日 16:40:40 +00:00Commented Aug 10, 2017 at 16:40
-
\$\begingroup\$ @Mr.Xcoder go ahead. \$\endgroup\$Leaky Nun– Leaky Nun2017年08月10日 16:55:59 +00:00Commented Aug 10, 2017 at 16:55
Haskell, 79 bytes
f 0=0
f x=[i|i<-[1..],all((/=i).f)[1..x-1],all(`notElem`show(f$x-1))$show i]!!0
The code is horribly inefficient. To calculate bigger values, i.e.> 12, add f x|x<11=x between the two lines (implemented a g in the TIO link).
JavaScript (ES6), 82 bytes
0-indexed.
f=(n,x=[1,p=0])=>n--?f(x[(g=k=>x[k]||(k+'').match(`[${p}]`)?g(k+1):p=k)(0)]=n,x):p
Demo
f=(n,x=[1,p=0])=>n--?f(x[(g=k=>x[k]||(k+'').match(`[${p}]`)?g(k+1):p=k)(0)]=n,x):p
for(n = 0; n < 50; n++) {
console.log('a(' + n + ') = ' + f(n))
}
Husk, 18 bytes
!¡1;0
ḟȯ¬V€d→0d-0N
A 1-indexed solution. Try it online!
Edit: fixed bug for +1 byte.
Explanation
Husk's built-in iteration function ¡ has many meanings.
Here, I'm using "construct infinite list by repeatedly appending new elements computed from the existing ones".
The second line is the helper function that computes a new element:
ḟȯ¬V€d→0d-0N Takes a list of existing elements, e.g. x = [0,1,...,10]
N The positive integers
-0 with elements of x removed: [11,12,13,...
ḟȯ Find an element n of this list that satisfies:
d Digits of n.
V Is any of them
€ an element of
d the digits of
→0 the last element of x?
¬ Negate.
Returns 22.
The first line is the main function:
!¡1;0 Takes an integer k.
¡ Iterate adding new elements to the list
;0 [0]
1 using the helper function,
! take k'th element of result.
-
\$\begingroup\$ I've added Husk to the golfing language list; please let me know if I got any details wrong. \$\endgroup\$ETHproductions– ETHproductions2017年08月11日 14:12:45 +00:00Commented Aug 11, 2017 at 14:12
Haskell, 78 bytes
n!k|r:_<-[j|j<-[1..],all(/=j)k,all(`notElem`show n)$show j]=n:r!(r:k)
(0![]!!)
It would be even more efficient if the second argument to ! would not be the list of seen numbers but of unseen numbers. But I can't do that without using more bytes.
Mathematica 115 Bytes
Still room to golf this down - and maybe use recursion (thus speeding it up).
(For[z={0};i=1,Length@z<#,
For[i=1,!FreeQ[z,i]||!DisjointQ@@IntegerDigits/@{l,i},i++];
z~AppendTo~i;l=Last@z;
];l)&
Original verbose code, with the same basic idea:
MakeSequenceA067581[n_]:=Module[{list={0}, innerCounter=1},
While[Length@list<n,
innerCounter=1;
(* inner loop *)While[Or[MemberQ[list,innerCounter],Intersection[IntegerDigits[Last@list],IntegerDigits[innerCounter]]!={}],innerCounter++];
AppendTo[list,innerCounter];
];
list
]
Explore related questions
See similar questions with these tags.
n > 1(orn ≥ 2) as input? (1-indexing) \$\endgroup\$