How to resolve the algorithm Lucky and even lucky numbers step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Lucky and even lucky numbers step by step in the 11l programming language

Table of Contents

Problem Statement

Note that in the following explanation list indices are assumed to start at one. Lucky numbers are positive integers that are formed by: This follows the same rules as the definition of lucky numbers above except for the very first step: The program should support the arguments: Demonstrate the program by:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Lucky and even lucky numbers step by step in the 11l programming language

Source code in the 11l programming language

-V NoValue = 0

F initLuckyNumbers(nelems, evenlucky, limit = -1)
   [Int] result
   L(i) 0 .< nelems
      V k = i
      L(j) (result.len - 1 .< 0).step(-1)
         k = k * result[j] I/ (result[j] - 1)
      V n = 2 * k + 1 + evenlucky
      I limit != -1 & n > limit
         L.break
      result.append(n)
   R result

F name(evenlucky)
   R [‘Lucky’, ‘Even lucky’][evenlucky]

F printSingle(j, evenlucky)
   V luckySeq = initLuckyNumbers(j, evenlucky)
   print(name(evenlucky)‘ number at index ’j‘ is ’luckySeq[j - 1])

F printRange(j, k, evenlucky)
   V luckySeq = initLuckyNumbers(k, evenlucky)
   print(name(evenlucky)‘ numbers at indexes ’j‘ to ’k‘ are: ’, end' ‘’)
   L(idx) j - 1 .< k
      I idx != j - 1
         print(‘, ’, end' ‘’)
      print(luckySeq[idx], end' ‘’)
   print()

F printInRange(j, k, evenlucky)
   V luckySeq = initLuckyNumbers(k, evenlucky, k)
   print(name(evenlucky)‘ numbers between ’j‘ to ’k‘ are: ’, end' ‘’)
   V first = 1B
   L(val) luckySeq
      I val > j
         I first
            first = 0B
         E
            print(‘, ’, end' ‘’)
         print(val, end' ‘’)
   print()

F process_args(args)
   assert(args.len C 1..3, ‘Wrong number of arguments’)

   // First argument: "j" value.
   V j = Int(args[0])

   V k = NoValue
   // Second argument: "k" value or a comma.
   I args.len > 1
      I args[1] == ‘,’
         // Must be followed by the kind of lucky number.
         assert(args.len == 3, ‘Missing kind argument’)
      E
         k = Int(args[1])
         assert(k != 0, ‘Expected a non null number’)

   V evenlucky = 0
   // Third argument: number kind.
   I args.len == 3
      V kind = args[2].lowercase()
      assert(kind C (‘lucky’, ‘evenlucky’), ‘Wrong kind’)
      I kind == ‘evenlucky’
         evenlucky = 1

   I k == NoValue
      // Print jth value.
      printSingle(j, evenlucky)
   E I k > 0
      // Print jth to kth values.
      printRange(j, k, evenlucky)
   E
      // Print values in range j..(-k).
      printInRange(j, -k, evenlucky)

:start:
I 1B
   L(args) [‘1 20’, ‘1 20 evenlucky’, ‘6000 -6100’, ‘6000 -6100 evenlucky’, ‘10000’, ‘10000 , lucky’, ‘10000 , evenlucky’]
      print(‘Command line arguments: ’args)
      process_args(args.split(‘ ’))
      print()
E
   process_args(:argv[1..])

  

You may also check:How to resolve the algorithm Round-robin tournament schedule step by step in the AWK programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the RPL programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Clojure programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the C# programming language