user
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:

1. 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 {\displaystyle 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39...}}
2. Return the first number from the list (which is **1**).
3. (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 {\displaystyle 1,3,7,9,13,15,19,21,25,27,31,33,37,39,43,45,49,51,55,57...}}
4. (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 {\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 {\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.
5. (Rule for the loop)
   * Note the 

     n
     {\displaystyle {\displaystyle n}}
     th, which is 

     m
     {\displaystyle {\displaystyle m}}
     .
   * Remove every 

     m
     {\displaystyle {\displaystyle m}}
     th.
   * Increment 

     n
     {\displaystyle {\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*:

1. 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 {\displaystyle 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40...}}
2. Return the first number from the list (which is **2**).
3. (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 {\displaystyle 2,4,6,10,12,14,18,20,22,26,28,30,34,36,38,42,44,46,50,52...}}
4. (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 {\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.
5. (Rule for the loop)
   * Note the 

     n
     {\displaystyle {\displaystyle n}}
     th, which is 

     m
     {\displaystyle {\displaystyle m}}
     .
   * Remove every 

     m
     {\displaystyle {\displaystyle m}}
     th.
   * Increment 

     n
     {\displaystyle {\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.
assistant
(formerly Perl 6)

```
sub luck(\a,\b) {
    gather {
	my @taken = take a;
	my @rotor;
	my $i = b;

	loop {
	    loop (my $j = 0; $j < @rotor; $j++) {
		--@rotor[$j] or last;
	    }
	    if $j < @rotor {
		@rotor[$j] = @taken[$j+1];
	    }
	    else {
		push @taken, take $i;
		push @rotor, $i - @taken;
	    }
	    $i += 2;
	}
    }
}

constant @lucky = luck(1,3);
constant @evenlucky = luck(2,4);

subset Luck where m:i/^ 'even'? 'lucky' $/;

multi MAIN (Int $num where * > 0) {
    say @lucky[$num-1];
}

multi MAIN (Int $num where * > 0, ',', Luck $howlucky = 'lucky') {
    say @::(lc $howlucky)[$num-1];
}

multi MAIN (Int $first where * > 0, Int $last where * > 0, Luck $howlucky = 'lucky') {
    say @::(lc $howlucky)[$first-1 .. $last - 1];
}

multi MAIN (Int $min where * > 0, Int $neg-max where * < 0, Luck $howlucky = 'lucky') {
    say grep * >= $min, (@::(lc $howlucky) ...^ * > abs $neg-max);
}

```

Output:

```
$ ./lucky
Usage:
  ./lucky <num>
  ./lucky <num> , [<howlucky>] 
  ./lucky <first> <last> [<howlucky>] 
  ./lucky <min> <neg-max> [<howlucky>]
$ ./lucky 20 , lucky
79
$ ./lucky 20 , evenlucky
76
$ ./lucky 1 20
1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
$ ./lucky 1 20 evenlucky
2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
$ ./lucky 6000 -6100
6009 6019 6031 6049 6055 6061 6079 6093
$ ./lucky 6000 -6100 evenLucky
6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
$ ./lucky 10000
115591
$ ./lucky 10000 , EVENLUCKY
111842
```