How to resolve the algorithm Van Eck sequence step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Van Eck sequence step by step in the Julia programming language

Table of Contents

Problem Statement

The sequence is generated by following this pseudo-code:

Using A: Using B: Using C: Using B: Using C: (zero last occurred two steps back - before the one) Using B: Using C: (two last occurred two steps back - before the zero) Using C: (two last occurred one step back) Using C: (one last appeared six steps back) ...

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Van Eck sequence step by step in the Julia programming language

The provided Julia code implements the Van Eck sequence, which is a sequence of integers where each number is determined by the last time that number appeared in the sequence.

Starting with a start value of 0, the sequence is generated by iterating through the numbers from 1 to N. For each number i, the code checks if it has already appeared in the sequence by using the findlast function. If the number has already appeared, the code assigns the difference between the current index i and the last index where the number appeared to the next element in the sequence. If the number has not appeared before, the code assigns the current index i to the next element in the sequence.

The first implementation uses a vector ret to store the sequence and a loop to iterate through the numbers. The second implementation uses a dictionary lastseen to store the last index where each number appeared and a loop to iterate through the numbers.

Both implementations print the first 10 elements of the sequence and the last 10 elements of the sequence with a start value of 1000.

Here's a breakdown of the code:

function vanecksequence(N, startval=0)

This is the main function that generates the Van Eck sequence. It takes two arguments: N, which is the length of the sequence to generate, and startval, which is the starting value of the sequence.

   ret = zeros(Int, N)
   ret[1] = startval

This initializes the return vector ret to a vector of zeros of length N and sets the first element of the vector to the startval.

   for i in 1:N-1

This loop iterates through the numbers from 1 to N-1.

       lastseen = findlast(x -> x == ret[i], ret[1:i-1])

This line uses the findlast function to find the last index where the current number ret[i] appeared in the sequence. The findlast function takes two arguments: a function and a vector. The function is used to test each element of the vector, and the function returns the index of the last element that satisfies the test. In this case, the function tests if each element of the vector is equal to the current number ret[i]. If the number has already appeared in the sequence, the findlast function returns the index of the last occurrence. If the number has not appeared before, the findlast function returns nothing.

       if lastseen != nothing
           ret[i + 1] = i - lastseen
       end

This conditional statement checks if the lastseen variable is not nothing, which means that the current number has already appeared in the sequence. If so, the code assigns the difference between the current index i and the last index where the number appeared to the next element in the sequence.

   end

This ends the loop.

   ret

This returns the generated sequence.

Source code in the julia programming language

function vanecksequence(N, startval=0)
    ret = zeros(Int, N)
    ret[1] = startval
    for i in 1:N-1
        lastseen = findlast(x -> x == ret[i], ret[1:i-1])
        if lastseen != nothing
            ret[i + 1] = i - lastseen
        end
    end
    ret
end

println(vanecksequence(10))
println(vanecksequence(1000)[991:1000])


function vanecksequence(N, startval=0)
    ret = zeros(Int, N)
    ret[1] = startval
    lastseen = Dict{Int, Int}()
    for i in 1:N-1
        if haskey(lastseen, ret[i])
            ret[i + 1] = i - lastseen[ret[i]]
        end
        lastseen[ret[i]] = i
    end
    ret
end


  

You may also check:How to resolve the algorithm Sum of squares step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Call a function step by step in the Racket programming language
You may also check:How to resolve the algorithm URL parser step by step in the Haskell programming language
You may also check:How to resolve the algorithm Check if a polygon overlaps with a rectangle step by step in the Wren programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Frink programming language