How to resolve the algorithm Brazilian numbers step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Brazilian numbers step by step in the Haskell programming language

Table of Contents

Problem Statement

Brazilian numbers are so called as they were first formally presented at the 1994 math Olympiad Olimpiada Iberoamericana de Matematica in Fortaleza, Brazil. Brazilian numbers are defined as: The set of positive integer numbers where each number N has at least one natural number B where 1 < B < N-1 where the representation of N in base B has all equal digits.

All even integers 2P >= 8 are Brazilian because 2P = 2(P-1) + 2, which is 22 in base P-1 when P-1 > 2. That becomes true when P >= 4. More common: for all all integers R and S, where R > 1 and also S-1 > R, then RS is Brazilian because RS = R(S-1) + R, which is RR in base S-1 The only problematic numbers are squares of primes, where R = S. Only 11^2 is brazilian to base 3.
All prime integers, that are brazilian, can only have the digit 1. Otherwise one could factor out the digit, therefore it cannot be a prime number. Mostly in form of 111 to base Integer(sqrt(prime number)). Must be an odd count of 1 to stay odd like primes > 2 Write a routine (function, whatever) to determine if a number is Brazilian and use the routine to show here, on this page;

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Brazilian numbers step by step in the Haskell programming language

This Haskell code is designed to identify and display the first 20 "Brazilian numbers" in various categories. A Brazilian number is defined as an integer greater than or equal to 7 that either is even or has all of its digits equal to each other except for its last digit (which can be different).

Here's a breakdown of the code:

  1. Importing Necessary Libraries:

    import Data.Numbers.Primes (primes)

    This line imports the primes function from the Data.Numbers.Primes library, which is used to generate a list of prime numbers.

  2. Defining the isBrazil Function:

    isBrazil :: Int -> Bool
    isBrazil n = 7 <= n && (even n || any (monoDigit n) [2 .. n - 2])

    This function takes an integer n as input and returns True if n is a Brazilian number; otherwise, it returns False. A number is considered Brazilian if it meets two criteria:

    • It must be greater than or equal to 7 (7 <= n).
    • It must either be even (even n) or satisfy the monoDigit condition for digits between 2 and n - 2 (any (monoDigit n) [2 .. n - 2]).
  3. Defining the monoDigit Function:

    monoDigit :: Int -> Int -> Bool
    monoDigit n b =
     let (q, d) = quotRem n b
     in d ==
        snd
          (until
             (uncurry (flip ((||) . (d /=)) . (0 ==)))
             ((`quotRem` b) . fst)
             (q, d))

    This function takes two integers, n and b, as input and returns True if n has all its digits (except for the last digit) equal to b; otherwise, it returns False. It uses the quotRem function to iteratively divide n by b and check whether the remainder (d) is equal to b.

  4. Main Function:

    main :: IO ()

    This is the entry point of the program.

  5. Using mapM_ to Process Lists of Data:

    main =
     mapM_
       (\(s, xs) ->
           (putStrLn . concat)
             [ "First 20 "
             , s
             , " Brazilians:\n"
             , show . take 20 $ filter isBrazil xs
             , "\n"
             ])
       [([], [1 ..]), ("odd", [1,3 ..]), ("prime", primes)]
    • mapM_ applies a function to each element of a list, returning a list of the results.
    • In this case, it takes a tuple (s, xs) as input, where s is a string label and xs is a list of integers.
    • Inside the function, it prints the label, the first 20 Brazilian numbers from the given list, and a newline character.
    • The list of tuples represents three different categories of numbers: all integers, odd integers, and prime integers.

In summary, this program generates lists of integers for each of the three categories (all, odd, and prime) and then uses the isBrazil function to identify the first 20 Brazilian numbers in each list. It finally prints the results in a readable format.

Source code in the haskell programming language

import Data.Numbers.Primes (primes)

isBrazil :: Int -> Bool
isBrazil n = 7 <= n && (even n || any (monoDigit n) [2 .. n - 2])

monoDigit :: Int -> Int -> Bool
monoDigit n b =
  let (q, d) = quotRem n b
  in d ==
     snd
       (until
          (uncurry (flip ((||) . (d /=)) . (0 ==)))
          ((`quotRem` b) . fst)
          (q, d))

main :: IO ()
main =
  mapM_
    (\(s, xs) ->
        (putStrLn . concat)
          [ "First 20 "
          , s
          , " Brazilians:\n"
          , show . take 20 $ filter isBrazil xs
          , "\n"
          ])
    [([], [1 ..]), ("odd", [1,3 ..]), ("prime", primes)]


  

You may also check:How to resolve the algorithm Show ASCII table step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Permutations step by step in the Ruby programming language
You may also check:How to resolve the algorithm Playfair cipher step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the PHP programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Shiny programming language