Lucky and even lucky numbers

From Rosetta Code
Revision as of 09:58, 5 March 2014 by rosettacode>Gerard Schildberger (added a draft task: lucky and even lucky numbers.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Lucky and even lucky numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Lucky numbers are positive integers that are formed by:

  • take a list of all the positive integers
  • remove every 2nd number
  • take the 2nd number   (which is   3 )
  • remove every 3rd number
  • take the 3rd number   (which is   7 )
  • remove every 7th number
  • take the 4th number   (which is   9 )
  • remove every 9th number
  • take the 5th number   (which is   13 )
  • remove every 13th number
  • take the 6th number ···


Even lucky numbers are positive even integers that are formed by:

  • take a list of all the positive even integers
  • remove every 2nd number
  • take the 2nd number   (which is   4 )
  • remove every 4th number
  • take the 3rd number   (which is   6 )
  • remove every 6th number
  • take the 4th number   (which is   10 )
  • remove every 10th number
  • take the 5th number   (which is   12 )
  • remove every 12th number
  • take the 6th number ···


The task's requirements are:

  • write one or two subroutines (functions) to generate lucky numbers and even lucky numbers
  • which kind of numbers and which number(s) will be specified via the command line or a similar interface.
  • 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
  • mixed case should be supported
  • support for a particular number
  • support for a range of particular numbers
  • support for a range of values


The program should support the arguments:

                           what is displayed  (on a single line)
     argument(s)              (optional verbage is encouraged)
╔═══════════════════╦═══════════════════════════════════════════════════╗
║  j                ║  Jth       lucky number                           ║
║  j  ,      lucky  ║  Jth       lucky number                           ║
║  j  ,  evenLucky  ║  Jth  even lucky number                           ║
║                   ║                                                   ║
║  j  k             ║  Jth  through  Kth (inclusive)       lucky number ║
║  j  k      lucky  ║  Jth  through  Kth (inclusive)       lucky number ║
║  j  k  evenLucky  ║  Jth  through  Kth (inclusive)  even lucky number ║
║                   ║                                                   ║
║  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

Specifically:

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


REXX

<lang REXX>/*REXX program displays lucky or evenLucky integers (#'s or a # range).*/ parse arg bot top func _ . /*get what we need from the C.L. */ if func== then func='lucky' /*Not defined? Use the default. */ s=left('s',bot\==top & top\==",") /*plural results (or not plural).*/ say func 'number's":" bot top '───►' $lucky(bot, top, func, _) exit /*stick a fork in it, we're done.*/ /*────────────────────────────────$LUCKY subroutine─────────────────────*/ $lucky: arg x,y,f,?; if y== | y==',' then y=x /*get some vars.*/

  1. =0; $=; ny=y<0 /*set variable: NOY: value range*/

if f== then f='LUCKY'; lucky=f=='LUCKY' /*assume LUCKY if omitted*/ if f\=='LUCKY' & f\=='EVENLUCKY' then return 'function not valid: ' f if arg()>3 &  ?\= then return "too many arguments entered: "  ? if x= then return "1st argument is missing." if x<1 then return "1st argument isn't a positive integer: " x if \datatype(x,'W') then return "1st argument isn't an integer: " x if \datatype(y,'W') then return "2nd argument isn't an integer: " y if x>ay then return "2nd arg is less than 1st arg." ay=abs(y); yL=ay; if y>0 then yL=y*10+y+y /*adjust the upper Y limit. */ if f== then f='LUCKY'; lucky=f=='LUCKY'

                                      /* [↓]  build for LUCKY|EVENLUCKY*/
   do j=1  until j>=yL                /*construct list of pos integers.*/
   if j//2==(\lucky)  then iterate    /*EVENLUCKY?  Use only even ints.*/
   if lucky  then if (j+1)//6==0  then iterate /*prune  if  mod 6≡zero.*/
                                  else nop     /*balance the IF logic. */
             else if  j   //8==0  then iterate /*prune next if mod 8≡0.*/
   #=#+1                              /*bump the counter of #'s found. */
   $=$ j                              /*append integer to the  $  list.*/
   end   /*j*/

z=0

   do p=3  until  z==;  z=word($,p) /*start to prune the integer list*/
   if z>#  then leave                 /*if integer is too large, stop. */
                      do j=#%z*z  by -z  to z  /*elide every  Zth  int.*/
                      $=delword($,j,1)         /*delete a particular #.*/
                      #=#-1                    /*decrease integer count*/
                      end   /*j*/              /*delete from right end.*/
   end   /*p*/

@.=

          do k=1; parse var $ q $; if q== then leave; @.k=q; end  /*k*/

@.0=k-1

          do j=1  for #               /*restrict the found integers.   */
          if (\ny & (j<x | j>ay))  |  (ny & (@.j<x | @.j>ay))  then  @.j=
          end   /*j*/                 /* [↑]  a list of #s or a range. */

_=

                      do b=1  for @.0; _=_ @.b; end    /*build a list. */

return space(_) /*remove superfluous list blanks.*/</lang> output when the input is:   1 20 lucky

lucky numbers: 1 20 ───► 1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79

output when the input is:   1 20 evenLucky

evenLucky numbers: 1 20 ───► 2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76

output when the input is:   6000 -6100 lucky

lucky numbers: 6000 -6100 ───► 6009 6019 6031 6049 6055 6061 6079 6093

output when the input is:   6000 -6100 evenLucky

evenLucky numbers: 6000 -6100 ───► 6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092

output when the input is:   10000

lucky number: 10000 ───► 115591

output when the input is:   10000 , evenLucky

evenLucky number: 10000 ───► 111842