보이지 않지만 공유 숫자는 없습니다!


28

도전

여기 PPCG에서는 시퀀스가 ​​마음에 듭니다 . 다른 재미도 있습니다.

의 정의하자 a(n)것으로 작은 양의 정수 X모든 같지 않음 a(k)( 0 < k < n)하고, a(n-1)그리고 X어떤 진수를 공유하지 않습니다.a(0) = 0

입력이 주어지면 n > 0, 그런 출력이 a(n)됩니다.

예를 들어, 입력 n = 13, 우리가 가지고 a(13) = 20있기 때문에, a(12) = 11그리고 20어떤 진수를 공유하지 않는 우리가 아직 보지 못한 정수 가장 작은 음이 아닌입니다 11.

순서

다음은 시작하기위한 첫 20 개의 용어입니다. OEIS의 시퀀스 A067581 입니다.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 11, 20, 13, 24, 15, 23, 14, 25

규칙

  • 입력 및 출력은 언어의 기본 정수 유형에 맞는 것으로 가정 할 수 있습니다.
  • 입력 및 출력은 편리한 형식으로 제공 될 수 있습니다 .
  • 예를 들어 여기에있는 0- 인덱스 또는 제출을위한 1- 인덱스를 선택할 수 있습니다. 당신이하고있는 것을 말하십시오.
  • 전체 프로그램 또는 기능이 허용됩니다. 함수 인 경우 출력하지 않고 출력을 반환 할 수 있습니다.
  • 가능하면 다른 사람들이 귀하의 코드를 시험해 볼 수 있도록 온라인 테스트 환경에 대한 링크를 포함하십시오!
  • 표준 허점 은 금지되어 있습니다.
  • 이것은 이므로 모든 일반적인 골프 규칙이 적용되며 가장 짧은 코드 (바이트)가 이깁니다.

입력으로 n > 1(또는 n ≥ 2) 얻을 수 있습니까 ? (1- 인덱싱)
— Outgolfer Erik

@EriktheOutgolfer 물론입니다. 괜찮습니다. 복사-붙여 넣기를 할 때 그 요점을 놓친 것 같습니다. 그것이 내 도전의 표준이기 때문입니다.
— AdmBorkBork

5
산포도는 확실히 좋은 :) 보인다
— flawr에게

답변:



7

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

This makes my head hurt! But, then I've barely glanced at any of the function methods in Japt.
— Shaggy

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.
— trlkly

@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.
— ETHproductions


3

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).

Try it online!


1

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


1

Husk, 18 bytes

!¡₁;0
ḟȯ¬V€d→⁰d-⁰N

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→⁰d-⁰N  Takes a list of existing elements, e.g. x = [0,1,...,10]
           N  The positive integers
         -⁰   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
      →⁰      the last element of x?
  ¬           Negate.
              Returns 22.

The first line is the main function:

!¡₁;0  Takes an integer k.
 ¡     Iterate adding new elements to the list
   ;0  [0]
  ₁    using the helper function,
!      take k'th element of result.

I've added Husk to the golfing language list; please let me know if I got any details wrong.
— ETHproductions

1

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.

Try it online!


0

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
]
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.