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

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Lucky and even lucky numbers step by step in the Haskell 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 Haskell programming language

The given Haskell program is a command-line utility for generating and displaying lucky numbers based on user-provided input. It offers various options to specify the type of lucky numbers and ranges of numbers to generate.

Here's a detailed explanation of the code:

  1. Data Type Definition:

    • In Haskell, the program defines a data type Lucky with two constructors: Lucky and EvenLucky. This data type represents the two types of lucky numbers the program can generate: regular lucky numbers and even lucky numbers.
  2. Help Message Function:

    • helpMessage is a function that prints a usage message with information on how to use the program. It displays a table showing the available command-line arguments and their corresponding actions.
  3. Number Lists:

    • oddNumbers and evenNumbers are lists of odd and even numbers, respectively. These lists are used to generate lucky numbers.
  4. Lucky Numbers Function:

    • luckyNumbers is a function that generates lucky numbers based on the provided input list. It filters a list of numbers using a sieve algorithm, creating a list of either odd or even prime numbers, depending on the input.
  5. Nth Lucky Number:

    • nth is a function that returns the n-th lucky number of the specified type (regular or even lucky).
  6. Range Function:

    • range is a function that generates a list of lucky numbers within a specified range. It takes the starting and ending indices and the type of lucky number to generate.
  7. Interval Function:

    • interval is a function that generates a list of lucky numbers within a specified interval. It takes the lower and upper bounds and the type of lucky number to generate.
  8. Lucky Type:

    • lucky is a function that determines the type of lucky number to generate based on the command-line arguments. It returns either Lucky or EvenLucky depending on whether the "evenLucky" argument is present.
  9. Argument Handling:

    • readn is a function that converts a string to an integer.
    • isInt is a function that checks if a string represents an integer.
    • main is the main function that parses the command-line arguments and determines the appropriate action to take. It uses the getArgs function to retrieve the arguments from the command line.
  10. Command-Line Usage:

    • If the first argument is "--help" or no arguments are provided, the helpMessage function is called to display the usage information.
    • Otherwise, the program processes the arguments and performs one of the following actions:
      • If a single integer argument is provided, it prints the n-th lucky number of the specified type.
      • If two integer arguments are provided, it generates and prints a list of lucky numbers within the specified range.
      • If two integer arguments with a '-' sign in between are provided, it generates and prints a list of lucky numbers within the specified interval.
      • If the arguments are invalid or the number of arguments is incorrect, the program prints an error message and usage information.

Source code in the haskell programming language

import System.Environment
import Text.Regex.Posix

data Lucky = Lucky | EvenLucky 

helpMessage :: IO ()
helpMessage = do
  putStrLn "                           what is displayed  (on a single line)"
  putStrLn "     argument(s)              (optional verbiage is encouraged)"
  putStrLn "======================|==================================================="
  putStrLn " j                    | Jth       lucky number                            "
  putStrLn " j  ,          lucky  | Jth       lucky number                            "
  putStrLn " j  ,      evenLucky  | Jth  even lucky number                            "
  putStrLn "                                                                          "
  putStrLn " j  k                 | Jth  through  Kth (inclusive)       lucky numbers "
  putStrLn " j  k          lucky  | Jth  through  Kth (inclusive)       lucky numbers "
  putStrLn " j  k      evenlucky  | Jth  through  Kth (inclusive)  even lucky numbers "
  putStrLn "                                                                          "
  putStrLn " j -k                 | all       lucky numbers in the range  j -> |k|    "
  putStrLn " j -k          lucky  | all       lucky numbers in the range  j -> |k|    "
  putStrLn " j -k      evenlucky  | all  even lucky numbers in the range  j -> |k|    "
  putStrLn "======================|==================================================="

oddNumbers :: [Int]
oddNumbers = filter odd [1..]

evenNumbers :: [Int]
evenNumbers = filter even [1..]

luckyNumbers :: [Int] -> [Int]
luckyNumbers xs = 
  let i = 3 in
  sieve i xs
    where
      sieve i (ln:s:xs) =
        ln : sieve (i + 1) (s : [x | (n, x) <- zip [i..] xs, rem n s /= 0])

nth :: Int -> Lucky -> Int
nth j Lucky     = luckyNumbers oddNumbers !! (j-1)
nth j EvenLucky = luckyNumbers evenNumbers !! (j-1)

range :: Int -> Int -> Lucky -> [Int]
range x x2 Lucky     = drop (x-1) (take x2 (luckyNumbers oddNumbers))
range x x2 EvenLucky = drop (x-1) (take x2 (luckyNumbers evenNumbers))

interval :: Int -> Int -> Lucky -> [Int]
interval x x2 Lucky     = dropWhile (<x) (takeWhile (<=x2) (luckyNumbers oddNumbers))
interval x x2 EvenLucky = dropWhile (<x) (takeWhile (<=x2) (luckyNumbers evenNumbers))

lucky :: [String] -> Lucky
lucky xs = 
  if "evenLucky" `elem` xs
   then EvenLucky
   else Lucky

readn :: String -> Int
readn s = read s :: Int

isInt :: String -> Bool
isInt s = not (null (s =~ "-?[0-9]{0,10}" :: String))

main :: IO ()
main = do
  args <- getArgs
  if head args == "--help" || null args
    then
      helpMessage
    else
      let l = lucky args in
      case map readn (filter isInt args) of
        [] -> do
          putStrLn "Invalid input, missing arguments"
          putStrLn "Type --help"
        [x] -> print (nth x l)
        [x, x2] -> if x2 > 0
          then print (range x x2 l)
          else print (interval x (-x2) l)
        _ -> do 
          putStrLn "Invalid input, wrong number of arguments"
          putStrLn "Type --help"


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Clay programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the SequenceL programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Mathematica/Wolfram Language programming language