Note that in the following explanation list indices are assumed to start at one.

Definition of lucky numbers
Lucky numbers are positive integers that are formed by:

 Form a list of all the positive odd integers > 0




1
,
3
,
5
,
7
,
9
,
11
,
13
,
15
,
17
,
19
,
21
,
23
,
25
,
27
,
29
,
31
,
33
,
35
,
37
,
39...


{\displaystyle 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39...}


 Return the first number from the list (which is 1).
 (Loop begins here)
 Note then return the second number from the list (which is 3).
 Discard every third, (as noted), number from the list to form the new list




1
,
3
,
7
,
9
,
13
,
15
,
19
,
21
,
25
,
27
,
31
,
33
,
37
,
39
,
43
,
45
,
49
,
51
,
55
,
57...


{\displaystyle 1,3,7,9,13,15,19,21,25,27,31,33,37,39,43,45,49,51,55,57...}


 (Expanding the loop a few more times...)
 Note then return the third number from the list (which is 7).
 Discard every 7th, (as noted), number from the list to form the new list




1
,
3
,
7
,
9
,
13
,
15
,
21
,
25
,
27
,
31
,
33
,
37
,
43
,
45
,
49
,
51
,
55
,
57
,
63
,
67...


{\displaystyle 1,3,7,9,13,15,21,25,27,31,33,37,43,45,49,51,55,57,63,67...}


 Note then return the 4th number from the list (which is 9).
 Discard every 9th, (as noted), number from the list to form the new list




1
,
3
,
7
,
9
,
13
,
15
,
21
,
25
,
31
,
33
,
37
,
43
,
45
,
49
,
51
,
55
,
63
,
67
,
69
,
73...


{\displaystyle 1,3,7,9,13,15,21,25,31,33,37,43,45,49,51,55,63,67,69,73...}


 Take the 5th, i.e. 13. Remove every 13th.
 Take the 6th, i.e. 15. Remove every 15th.
 Take the 7th, i.e. 21. Remove every 21th.
 Take the 8th, i.e. 25. Remove every 25th.
 (Rule for the loop)
 Note the 



n


{\displaystyle n}

th, which is 



m


{\displaystyle m}

.
 Remove every 



m


{\displaystyle m}

th.
 Increment 



n


{\displaystyle n}

.
Definition of even lucky numbers
This follows the same rules as the definition of lucky numbers above except for the very first step:

 Form a list of all the positive even integers > 0




2
,
4
,
6
,
8
,
10
,
12
,
14
,
16
,
18
,
20
,
22
,
24
,
26
,
28
,
30
,
32
,
34
,
36
,
38
,
40...


{\displaystyle 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40...}


 Return the first number from the list (which is 2).
 (Loop begins here)
 Note then return the second number from the list (which is 4).
 Discard every 4th, (as noted), number from the list to form the new list




2
,
4
,
6
,
10
,
12
,
14
,
18
,
20
,
22
,
26
,
28
,
30
,
34
,
36
,
38
,
42
,
44
,
46
,
50
,
52...


{\displaystyle 2,4,6,10,12,14,18,20,22,26,28,30,34,36,38,42,44,46,50,52...}


 (Expanding the loop a few more times...)
 Note then return the third number from the list (which is 6).
 Discard every 6th, (as noted), number from the list to form the new list




2
,
4
,
6
,
10
,
12
,
18
,
20
,
22
,
26
,
28
,
34
,
36
,
38
,
42
,
44
,
50
,
52
,
54
,
58
,
60...


{\displaystyle 2,4,6,10,12,18,20,22,26,28,34,36,38,42,44,50,52,54,58,60...}


 Take the 4th, i.e. 10. Remove every 10th.
 Take the 5th, i.e. 12. Remove every 12th.
 (Rule for the loop)
 Note the 



n


{\displaystyle n}

th, which is 



m


{\displaystyle m}

.
 Remove every 



m


{\displaystyle m}

th.
 Increment 



n


{\displaystyle n}

.
Task requirements
 Write one or two subroutines (functions) to generate lucky numbers and even lucky numbers 
 Write a command-line interface to allow selection of which kind of numbers and which number(s). Since input is from the command line, tests should be made for the common errors:
 missing arguments
 too many arguments
 number (or numbers) aren't legal
 misspelled argument (lucky or evenLucky)
 The command line handling should:
 support mixed case handling of the (non-numeric) arguments
 support printing a particular number
 support printing a range of numbers by their index
 support printing a range of numbers by their values
 The resulting list of numbers should be printed on a single line.

The program should support the arguments:

                             what is displayed  (on a single line)
       argument(s)              (optional verbiage is encouraged)
  ╔═══════════════════╦════════════════════════════════════════════════════╗
  ║  j                ║  Jth       lucky number                            ║
  ║  j  ,      lucky  ║  Jth       lucky number                            ║
  ║  j  ,  evenLucky  ║  Jth  even lucky number                            ║
  ║                   ║                                                    ║
  ║  j  k             ║  Jth  through  Kth (inclusive)       lucky numbers ║
  ║  j  k      lucky  ║  Jth  through  Kth (inclusive)       lucky numbers ║
  ║  j  k  evenLucky  ║  Jth  through  Kth (inclusive)  even lucky numbers ║
  ║                   ║                                                    ║
  ║  j -k             ║  all       lucky numbers in the range  j ──► |k|   ║
  ║  j -k      lucky  ║  all       lucky numbers in the range  j ──► |k|   ║
  ║  j -k  evenLucky  ║  all  even lucky numbers in the range  j ──► |k|   ║
  ╚═══════════════════╩════════════════════════════════════════════════════╝
                           where    |k|    is the absolute value of   k

Demonstrate the program by:

 showing the first twenty lucky numbers
 showing the first twenty even lucky numbers
 showing all lucky numbers between 6,000 and 6,100 (inclusive)
 showing all even lucky numbers in the same range as above
 showing the 10,000th lucky number (extra credit)
 showing the 10,000th even lucky number (extra credit)
See also

 This task is related to the Sieve of Eratosthenes task.
 OEIS Wiki Lucky numbers.
 Sequence A000959 lucky numbers on The On-Line Encyclopedia of Integer Sequences.
 Sequence A045954 even lucky numbers or ELN on The On-Line Encyclopedia of Integer Sequences.
 Entry lucky numbers on The Eric Weisstein's World of Mathematics.


using Base, StringDistances
 
struct Lucky
    start::Int
    nmax::Int
    Lucky(iseven, nmax) = new(iseven ? 2 : 1, nmax)
end
 
struct LuckyState
    nextindex::Int
    sequence::Vector{Int}
end
 
Base.eltype(iter::Lucky) = Int
 
function Base.iterate(iter::Lucky, state = LuckyState(1, collect(iter.start:2:iter.nmax)))
    if length(state.sequence) < state.nextindex
        return nothing
    elseif state.nextindex == 1
        return (iter.start, LuckyState(2, state.sequence))
    end
    result = state.sequence[state.nextindex]
    newsequence = Vector{Int}()
    for (i, el) in enumerate(state.sequence)
        if i % result != 0
            push!(newsequence, el)
        end
    end
    (result, LuckyState(state.nextindex + 1, newsequence))
end
 
function luckyindex(j, wanteven, k=0)
    topindex = max(j, k) + 4
    luck = Lucky(wanteven, topindex * 20)
    iter_result = iterate(luck)
    while iter_result != nothing
        (elem, state) = iter_result
        iter_result = iterate(luck, state)
        if iter_result != nothing && iter_result[2].nextindex > topindex
            return iter_result[2].sequence[k > j ? (j:k) : j]
        end
    end
    throw("Index $j out of range for nmax of $(luck.nmax).")
end
 
function luckyrange(j, k, wanteven)
    topvalue = max(j, k)
    luck = Lucky(wanteven, topvalue + 1)
    iter_result = iterate(luck)
    (elem, state) = iter_result # save next to last result
    while iter_result != nothing
        (elem, state) = iter_result
        iter_result = iterate(luck, state)
    end
    filter(x -> (j <= x <= k), state.sequence)
end
 
function helpdisplay(exitlevel=1)
    println("\n", PROGRAM_FILE, " j [-][k] [lucky|evenLucky]")
    println("\tj: index wanted or a starting point (index or value)", 
            "\n\tk: optional ending point (index), \n\t-k: optional ending point (value)\n")
    helpstring =
""" | Argument(s)        |    What is printed                                  |
 |--------------------------------------------------------------------------|
 | j                  |  jth lucky number (required argument)               |
 | j , lucky          |  jth lucky number                                   |
 | j , evenLucky      |  jth even lucky number                              |
 | j  k               |  jth through kth (inclusive) lucky numbers          |
 | j  k  lucky        |  jth through kth (inclusive) lucky numbers          |
 | j  k  evenLucky    |  jth through kth (inclusive) even lucky numbers     |
 | j  -k              |  all lucky numbers in the value range [m, |k|]      |
 | j  -k  lucky       |  all lucky numbers in the value range [m, |k|]      |
 | j  -k  evenLucky   |  all even lucky numbers in the value range [m, |k|] |
 |--------------------------------------------------------------------------|\n\n"""
 
    println(helpstring)
    exit(exitlevel)
end
 
function parsecommandline()
    comma = false
    evenLucky = false
    range = false
    j = k = 0
    if length(ARGS) < 1
        helpdisplay()
    end
    for (pos, arg) in enumerate(ARGS)
        if pos == 1
            j = tryparse(Int, arg)
            if j == nothing
                println("The first argument must be a positive integer.\n")
                helpdisplay()
            end
        elseif pos == 2 || (pos == 3 && comma)
            k = tryparse(Int, arg)
            if k == nothing
                k = 0
                if arg == ","
                    comma = true
                    continue
                elseif arg == "lucky"
                    continue
                elseif arg == "evenLucky"
                    evenLucky = true
                elseif compare(Hamming(), arg, "lucky") > 0.4 || compare(Hamming(), arg, "evenLucky") > 0.4
                    println("Did you misspell \"lucky\" or \"evenLucky\"? Check capitalization.\n")
                    helpdisplay()
                else
                    helpdisplay()
                end
            elseif k < 0
                k = -k
                range = true
            end
        elseif pos == 3 || pos == 4 && comma
            if arg == ","
                comma = true
                continue
            elseif arg == "lucky"
                continue
            elseif arg == "evenLucky"
                evenLucky = true
            elseif compare(Hamming(), arg, "lucky") > 0.1 || compare(Hamming(), arg, "evenLucky") > 0.1
                println("Did you misspell "\lucky\" or "\evenLucky\"?\n\n")
                helpdisplay()
            else
                helpdisplay()
            end
        elseif arg == "lucky"
                continue
        elseif arg == "evenLucky"
                evenLucky = true
        else
            println("Too many arguments.\n")
            helpdisplay()
        end
    end
    (j, k, evenLucky, range)
end
 
function runopts()
    (j, k, evenLucky, range) = parsecommandline()
    if j < 1 || (k != 0 && j >= k)
        throw("Lucky number integer parameters out of range: $(typeof(j)), $j, $(typeof(k)), $k")
    end
    if range
        println(luckyrange(j, k, evenLucky))
    else
        println(luckyindex(j, evenLucky, k))
    end
end
 
runopts()