Nth term of Van Eck Sequence

41

3

Output the Nth term of the Van Eck Sequence.

Van Eck Sequence is defined as:

  • Starts with 0.
  • If the last term is the first occurrence of that term the next term is 0.
  • If the last term has occurred previously the next term is how many steps back was the most recent occurrence.

https://oeis.org/A181391

https://www.youtube.com/watch?v=etMJxB-igrc

https://www.youtube.com/watch?v=8VrnqRU7BVU

Sequence: 0,0,1,0,2,0,2,2,1,6,0,5,0,2,...

Tests:

Input | Output

  • 1 | 0
  • 8 | 2
  • 19 | 5
  • 27 | 9
  • 52 | 42
  • 64 | 0

EDIT

1 indexed is preferred, 0 indexed is acceptable; that might change some of the already submitted solutions.

Just the Nth term please.

Same (except for the seeing it already posted part), it seems code golfers and numberphile watchers have a decent overlap.

182764125216

Posted 2019-06-10T21:17:07.210

Reputation: 499

9Watched the numpherphile video at work and was going to post this when I got home. Curse you for getting there first. :P – Draco18s no longer trusts SE – 2019-06-10T21:27:09.310

17Does it have to be 1-indexed, or may we use 0-indexing? – Robin Ryder – 2019-06-10T22:02:06.147

6May we return or output the infinite sequence instead? – Jo King – 2019-06-11T02:10:27.033

2... or the first n terms? – Shaggy – 2019-06-11T15:11:39.883

@Draco18s Same, I came here to post it after seeing the Numberphile video, when I saw this. – Geza Kerecsenyi – 2019-06-11T16:52:40.820

@Draco18s The same thing happened to me. :P – VFDan – 2019-07-05T16:31:57.173

Answers

25

JavaScript (ES6),  46 41  37 bytes

n=>(g=p=>--n?g(g[p]-n|0,g[p]=n):p)(0)

Try it online!

How?

We don't need to store the full sequence. We only need to keep track of the last position of each integer that appears in the sequence. We use the underlying object of the recursive function \$g\$ for that purpose.

For a given term \$p\$, we don't need either to set \$g[p]\$ to its actual absolute position in the sequence because we're only interested in the distance with the current position. That's why we can just store the current value of the input \$n\$, which is used as a decrementing counter in the code.

Therefore, the distance is given by \$g[p]-n\$. Conveniently, this evaluates to NaN if this is the first occurrence of \$p\$, which can be easily turned into the expected \$0\$.

Commented

n => (             // n = input
  g = p =>         // g = recursive function taking p = previous term of the sequence
                   //     g is also used as an object to store the last position of
                   //     each integer found in the sequence
    --n ?          // decrement n; if it's not equal to 0:
      g(           //   do a recursive call:
        g[p] - n   //     subtract n from the last position of p
                   //     if g[p] is undefined, the above expression evaluates to NaN
        | 0,       //     in which case we coerce it to 0 instead
        g[p] = n   //     update g[p] to n
      )            //   end of recursive call
    :              // else:
      p            //   we've reached the requested term: stop recursion and return it
)(0)               // initial call to g with p = 0

Arnauld

Posted 2019-06-10T21:17:07.210

Reputation: 111 334

18

Python 3, 69 63 62 bytes

f=lambda n,l=0,*s:f(n-1,l in s and~s.index(l),l,*s)if n else-l

Try it online!

Note: as Erik the Outgolfer mentioned, this code works fine in Python 2 as well.

0-indexed (although, just to be utterly perverse, you can make it -1-indexed by changing if n to if~n :P)

Makes use of Python's gorgeous unpacking "star operator", to recursively build up the series, until n reaches zero.

The function builds up the series in the reverse order, to avoid having to reverse it for the search. Additionally, it actually stores the negations of all the elements, because converting them back at the end was free (else the - would have had to be a space) and it saves us a byte along the way, by using ~s.index(l) instead of -~s.index(l).

Could be 51 bytes if Python tuples had the same find functions strings do (returning -1 if not found, instead of raising an error), but no such luck...

ArBo

Posted 2019-06-10T21:17:07.210

Reputation: 1 416

3Actually, the "star operator" you're using isn't Python 3's unpacking operator, but rather the vararg operator which also exists in Python 2. – Erik the Outgolfer – 2019-06-10T22:56:22.723

3The first one is, but isn't the second one unpacking s for the recursive call? – ArBo – 2019-06-10T23:08:13.970

1I've tested it in Python 2 and it works. – Erik the Outgolfer – 2019-06-11T11:06:29.627

@EriktheOutgolfer hmm, but isn't the second use unpacking though? The function doesn't have to support varargs to use such syntax. – ArBo – 2019-06-11T12:43:05.820

@ArBo: It's no different than def func(f, *args): f(*args); unpacking inside function calls is valid py2. What's py3-only is unpacking inside list/dict comprehensions (i.e. [1, 2, *s]) or unpacking variables: a, *b = [1,2,3,4]. – Ehsan Kia – 2019-06-13T17:27:46.737

@EhsanKia Yes, I now know it's also Python 2. But it is an unpacking operator and not a varargs operator, since something like def f(a, b):pass and then t=(1,2); f(*t) works as well, without f having varargs. – ArBo – 2019-06-13T17:44:08.367

9

R, 62 bytes

function(n){while(sum(F|1)<n)F=c(match(F[1],F[-1],0),F)
+F[1]}

Try it online!

Builds the list in reverse; match returns the first index of F[1] (the previous value) in F[-1] (the remainder of the list), returning 0 if no match is found.

F is initialized to FALSE and is coerced to 0 on the first pass of the while loop.

Giuseppe

Posted 2019-06-10T21:17:07.210

Reputation: 21 077

2I'm kind of in awe of how good match is for this problem when you construct it this way. Really clean. – CriminallyVulgar – 2019-06-12T09:50:29.177

Does the plus on the second line do anything here? I assumed it fixed an edge case, but I can't find one for it. – CriminallyVulgar – 2019-06-12T09:56:33.353

1@CriminallyVulgar it should coerce F to 0 when n==1 else it would return FALSE. – Giuseppe – 2019-06-12T11:20:22.700

Ahh, I see. Makes sense, I was trying lots of ranges but not the single value. – CriminallyVulgar – 2019-06-12T12:20:47.877

9

Perl 6, 47 42 bytes

-5 bytes thanks to nwellnhof

{({+grep(@_[*-1],:k,[R,] @_)[1]}...*)[$_]}

Try it online!

Anonymous codeblock that outputs the 0-indexed element in the sequence.

Explanation:

{                                            } # Anonymous codeblock
 (                                      )[$_]  # Return the nth element
                                    ...*       # Of the infinite sequence
  {                            }  # Where each element is
    grep(        :k        )[1]   # The key of the second occurrence
         @_[*-1],                 # Of the most recent element
                   ,[R,] @_       # In the reversed sequence so far
   +     # And numify the Nil to 0 if the element is not found

Jo King

Posted 2019-06-10T21:17:07.210

Reputation: 38 234

8

Bourne shell, 102 bytes

until [ 0"$i" -eq $1 ];do i=$((${i:-0}+1)) a=${n:-0};eval 'n=$(($i-${m'$a:-$i'}))' m$a=$i;done;echo $a

try it online

jnfnt

Posted 2019-06-10T21:17:07.210

Reputation: 373

3Welcome to PPCG! – Arnauld – 2019-06-11T09:24:09.143

6

Stax, 10 9 bytes

é"▬π²"ô↕j

Run and debug it

If 0-based indexing is allowed:

Stax, 8 bytes

à┐æ8Å/[┤

Run and debug it

recursive

Posted 2019-06-10T21:17:07.210

Reputation: 8 616

6

J, 29 23 bytes

1{(,~#|1+}.i.{.)@]^:[&0

Try it online!

The real work is done in the iteration verb of the power verb ^:, which iterates as many times as the argument [, starting the iteration with the constant value 0 &0...

  • (#|1+}.i.{.) This is what iterates. Breaking it down...
  • }.i.{. Find the index of i. of the head of the list {. within the tail of the list }.. This will return a 0-based index, so if the current item is found 1 previous it will return 0. If it is not found, it will return the length of the list, ie, the length of the tail.
  • 1+ Add one to the value to correct for the 0-based indexing, since the Ven Eck's "how far back" is 1-based. Note that if it was not found, the value will now be the length of the full list.
  • #| Return the remainder of the value calculated in the previous step, when divided by the length of the full list. Note that this turns "not found" into 0, but leaves all other values unchanged.
  • ,~ Append the new value to the front of the list. We use the front rather than last merely for convenience.
  • 1{ return the 2nd item in the list, since we calculated one too many times because it's shorter that way.

Jonah

Posted 2019-06-10T21:17:07.210

Reputation: 8 729

6

Python, 51 bytes

f=lambda n,i=1:n>i and[f(n,i+1),i][f(n-1)==f(n+~i)]

Try it online!

Outputs False for 0. Implements the spec pretty literally, looking for the lowest positive integer i such that f(n-1)==f(n-i-1). If such a search leads to i>=n, the previous element hasn't appeared before and we produce 0.

Instead of doing something reasonable like storing earlier values in a list, the function just recomputes them recursively from scratch whenever they're needed, and sometimes when they're not needed. This makes the function run very slowly for inputs above 10 or so.

xnor

Posted 2019-06-10T21:17:07.210

Reputation: 115 687

5

APL (Dyalog Unicode), 19 17 bytesSBCS

Many thanks to ngn, Adám, Richard Park and H.PWiz for their help in writing and golfing this answer in The APL Orchard, a great place to learn APL and get APL help.

Edit: -2 bytes from Adám.

⊃(⊢,⍨≢|1∘↓⍳⊃)⍣⎕-1

Try it online!

Explanation

⊃(⊢,⍨≢|1∘↓⍳⊃)⍣⎕-1

                 -1  We initialize our array of results with -1.
 (           )⍣⎕    ⍣ repeats the train (in parentheses) our input, ⎕, times.
        1∘↓⍳⊃        We take the index of the head (our last element in the sequence).
                     To signify "element not found", this returns the length of the array.
      ≢|             We take our index modulo the length of the array.
                     This turns our "element not found" from the length of the array to 0.
  ⊢,⍨                And we prepend to our array.
⊃                    Finally, we return the first element of the array,
                     which is the most recently-generated.
                     This is the ⍵-th element of the Van Eck sequence.

Sherlock9

Posted 2019-06-10T21:17:07.210

Reputation: 11 664

4

Wolfram Language (Mathematica), 48 bytes

#<1||Last[#-1-Array[#0,#-2]~Position~#0[#-1],0]&

Try it online!

Nonzero values are returned as singleton lists.

attinat

Posted 2019-06-10T21:17:07.210

Reputation: 3 495

4

05AB1E, 8 bytes

F¯Rćk>Dˆ

Try it online or output the first \$n\$ values in the list.

Explanation:

F         # Loop the (implicit) input amount of times:
 ¯        #  Push the global array
  R       #  Reverse it
   ć      #  Extract the head; push the remainder and the head to the stack
    k     #  Get the 0-based index of the head in the remainder (-1 if not found)
     >    #  Increase it by 1 to make it 1-indexed (or 0 if not found)
      Dˆ  #  Add a copy to the global array
          # (after the loop, output the top of the stack implicitly as result,
          #  which is why we need the `D`/duplicate)

Kevin Cruijssen

Posted 2019-06-10T21:17:07.210

Reputation: 67 575

1That's a weird way to censor profanity! – negative seven – 2019-06-22T10:35:36.730

1@negativeseven Lol, took me a few minutes to know what you meant, but I guess you're referring to the F¯Rćk? ;) – Kevin Cruijssen – 2019-06-22T11:29:34.467

4

Java, 96 80 76 bytes

n->{int i,v=0,m[]=new int[n];for(;--n>0;m[v]=n,v=i<1?0:i-n)i=m[v];return v;}

Not obfuscated:

Function<Integer, Integer> vanEck =
n -> {

    int i;                  // i is the value of n when v was previously encountered
    int v = 0;              // v is the current element of vanEck sequence
    int[] m = new int[n];   // m[v] is the value of n when v was previously encountered

    while (--n > 0) {       // n is used as a decrementing counter

        i = m[v];
        m[v] = n;
        v = i == 0 ? 0 : i - n;
    }

    return v;
};

Achaaab

Posted 2019-06-10T21:17:07.210

Reputation: 41

2You should be able to remove a few bytes by changing the while loop to a for loop. – MegaTom – 2019-06-11T14:27:11.887

1Hello, you could golf more by inlining the declaration of int[] in the int declaration, and also use <1 instead of ==0. Example: int f(int n){int l[]=new int[n],i=0,j,v=0;while(++i<n){j=l[v];l[v]=i;v=j<1?0:i-j;}return v;} – Olivier Grégoire – 2019-06-12T08:40:23.017

2

And now a lambda, as well as the golf mentioned by @MegaTom for a total of 80 bytes: n->{int l[]=new int[n],i=0,j,v=0;for(;++i<n;l[v]=i,v=j<1?0:i-j)j=l[v];return v;}

– Olivier Grégoire – 2019-06-12T08:47:02.233

1

Finally, you can check for tips for golfing in Java.

– Olivier Grégoire – 2019-06-12T08:49:10.933

3

Charcoal, 23 bytes

≔⁰θF⊖N«≔⊕⌕⮌υθη⊞υθ≔ηθ»Iθ

Try it online! Link is to verbose version of code. Explanation:

≔⁰θ

Set the first term to 0.

F⊖N«

Loop n-1 times. (If 0-indexing is acceptable, the can be removed for a 1-byte saving.)

≔⊕⌕⮌υθη

The next term is the incremented index of the current term in the reversed list of previous terms.

⊞υθ

Add the current term to the list of previous terms.

≔ηθ

Set the current term to the next term.

»Iθ

Print the current term at the end of the loop.

Neil

Posted 2019-06-10T21:17:07.210

Reputation: 95 035

2

Jelly, 7 bytes

Ḷ߀ṚiḢ$

Try it online!

0-indexed.

Erik the Outgolfer

Posted 2019-06-10T21:17:07.210

Reputation: 38 134

2

Jelly, 8 bytes

ẎiḢ$;µ¡Ḣ

A monadic Link accepting a positive integer, \$n\$, which yields the \$n^{th}\$ term of the Van Eck Sequence.

Try it online!

How?

ẎiḢ$;µ¡Ḣ - Link: n
     µ¡  - repeat this monadic link n times - i.e. f(f(...f(n)...)):
         - (call the current argument L)
Ẏ        -   tighten (ensures we have a copy of L, so that Ḣ doesn't alter it)
   $     -   last two links as a monad:
  Ḣ      -     head (pop off & yield leftmost of the copy)
 i       -     first index (of that in the rest) or 0 if not found
    ;    -   concatenate with L
       Ḣ - head

Note that without the final we've actually collected [a(n), a(n-1), ..., a(2), a(1), n]

Jonathan Allan

Posted 2019-06-10T21:17:07.210

Reputation: 67 804

2

C (gcc), 63 bytes

f(n){n=g(n,--n);}g(n,i){n=n>0?f(--n)-f(i)?g(n,i)+!!g(n,i):1:0;}

Try it online!

0-indexed.

attinat

Posted 2019-06-10T21:17:07.210

Reputation: 3 495

2

Haskell, 68 67 66 bytes

Quite straightforward implementation (using 0 based indexing).

f n|all((/=f(n-1)).f)[0..n-2]=0|m<-n-1=[k|k<-[1..],f(m-k)==f m]!!0

Try it online!

flawr

Posted 2019-06-10T21:17:07.210

Reputation: 40 560

2

Haskell, 61 bytes

(([]#0)1!!)
(l#n)i=n:(((n,i):l)#maybe 0(i-)(lookup n l))(i+1)

0-based indexing.

Try it online!

nimi

Posted 2019-06-10T21:17:07.210

Reputation: 34 639

2

Japt -h, 11 bytes

@ÒZÔÅbX}hTo

Try it

Shaggy

Posted 2019-06-10T21:17:07.210

Reputation: 24 623

2

C# (Visual C# Interactive Compiler), 77 bytes

n=>{int i,v=0;for(var m=new int[n];--n>0;m[v]=n,v=i<1?0:i-n)i=m[v];return v;}

Try it online!

Pretty much a port of the Java answer at this point.

dana

Posted 2019-06-10T21:17:07.210

Reputation: 2 541

2

Python 3, 128 114 111 102 99 bytes

102 -> 99 bytes, thanks to Jonathan Frech

f=lambda n,i=1,l=[0]:f(n,i+1,l+[l[i-2::-1].index(l[-1])+1if l[-1]in l[:-1]else 0])if n>i else l[-1]

Try it online!

ruohola

Posted 2019-06-10T21:17:07.210

Reputation: 414

You can negate your condition and use - instead of != to save a byte. – Jonathan Frech – 2019-06-13T15:03:01.850

Also, since your golf appears to be side-effect-less, you can use lists instead of tuples. – Jonathan Frech – 2019-06-13T15:04:31.340

@JonathanFrech But if I have a list as default argument it will not work correctly for consecutive calls? – ruohola – 2019-06-13T15:05:49.787

Why should it not? – Jonathan Frech – 2019-06-13T15:07:47.540

Weird, I had a list as a default argument before and it didn't clear between function call, making the result wrong. – ruohola – 2019-06-13T15:11:35.650

Never mind, I used l.append() that time. – ruohola – 2019-06-13T15:13:13.043

1

Most likely because your previous script modified the list, i.e. was not side-effect-less: example.

– Jonathan Frech – 2019-06-13T15:15:14.357

1

Python 3, 112 bytes

a=[0]
for _ in a*int(input()):k=a[-1];a+=k in a[:-1]and[a[::-1].index(k)+~a[-2::-1].index(k)]or[0]
print(-a[-2])

Try it online!

-3 bytes thanks to mypetlion

HyperNeutrino

Posted 2019-06-10T21:17:07.210

Reputation: 26 575

Second line can become for _ in a*int(input()):k=a[-1];a+=k in a[:-1]and[a[::-1].index(k)+~a[-2::-1].index(k)]or[0] to save 3 bytes. – mypetlion – 2019-06-11T16:38:32.507

@mypetlion thanks – HyperNeutrino – 2019-06-11T16:52:50.120

1

Red, 106 95 bytes

func[n][b: copy[0]loop n[insert b either not find t: next b
b/1[0][-1 + index? find t b/1]]b/2]

Try it online!

Galen Ivanov

Posted 2019-06-10T21:17:07.210

Reputation: 13 815

1

Perl 5 (-p), 42 bytes

map{($\,$\{$\})=0|$\{$\};$_++for%\}1..$_}{

Try it online!

Grimmy

Posted 2019-06-10T21:17:07.210

Reputation: 12 521

1

CJam (15 bytes)

0a{_(#)\+}qi*0=

Online demo. This is a full program and 0-indexed.

Dissection

0a      e# Push the array [0]
{       e# Loop...
  _(#   e#   Copy the array, pop the first element, and find its index in the array
  )\+   e#   Increment and prepend
}qi*    e# ... n times, where n is read from stdin
0=      e# Take the first element of the array

Peter Taylor

Posted 2019-06-10T21:17:07.210

Reputation: 41 901

1

Pyth, 18 bytes

VQ=Y+?YhxtYhY0Y;hY

Try it online!

Builds up the sequence in reverse and prints the first element (last term of the sequence).

VQ                 # for N in range(Q) (Q=input)
  =Y+         Y    # Y.prepend(
        xtY        #   Y[1:].index(    )
           hY      #               Y[0]
       h           #                     +1
     ?Y      0     #                        if Y else 0)
               ;hY # end for loop and print Y[0]

ar4093

Posted 2019-06-10T21:17:07.210

Reputation: 531

0

Clojure, 69 bytes

#((fn f[i c t](if(= i 1)t(f(dec i)(assoc c t i)(-(or(c t)i)i))))%{}0)

Sadly a more functional approach seems to be longer.

NikoNyrh

Posted 2019-06-10T21:17:07.210

Reputation: 2 361

0

C++ (clang), 241 235 234 219 197 189 bytes

197 -> 189 bytes, thanks to ceilingcat

#import<bits/stdc++.h>
int f(int n,int i=0,std::vector<int>l={0}){return~-n>i?l.push_back(find(begin(l),end(l)-1,l[i])-end(l)+1?find(rbegin(l)+1,rend(l),l[i])-rbegin(l):0),f(n,i+1,l):l[i];}

Try it online!

ruohola

Posted 2019-06-10T21:17:07.210

Reputation: 414

0

DC, 94 91 90 bytes

Input is taken during the program. Save this to a file and then do "dc " to run. Definitely not the shortest, but I have fun with challenges like these in dc. Input is 1-based index, as preferred.

[st1si0swlbxltlwlu1-sulu0!=m]sm[dlt=qSsli1+siz0!=b0siLs]sb[0pq]sf[lisw2Q]sq?2-dsu1>f0dlmxp

Main control macro
[st                         ]sm   save top value as target
[  1si0sw                   ]sm   reset i to 1 and w to 0
[        lbx                ]sm   execute macro b to get next value in w
[           ltlw            ]sm   restore target to the stack and add w to the stack
[               lu1-su      ]sm   decrement the user inputted variable
[                     lu0!=m]sm   if the user inputted variable is not 0 recurse

Next value finder macro
[dlt=q                  ]sb     if the value on the stack is the target, quit
[     Ss                ]sb     save top value to s register
[       li1+si          ]sb     increment i register
[             z0!=b     ]sb     recurse if still more values            
[                  0si  ]sb     set i to 0 (will be saved to w if relevant)
[                     Ls]sb     move top value of s register to stack

[lisw2Q]sq   Load i, save it to w, and then quit this macro and the one that called it

[0pq]sf print 0 and quit the program
```

FlexEast

Posted 2019-06-10T21:17:07.210

Reputation: 31