How to resolve the algorithm Brazilian numbers step by step in the Haskell programming language
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:
-
Importing Necessary Libraries:
import Data.Numbers.Primes (primes)
This line imports the
primes
function from theData.Numbers.Primes
library, which is used to generate a list of prime numbers. -
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 returnsTrue
ifn
is a Brazilian number; otherwise, it returnsFalse
. 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 themonoDigit
condition for digits between 2 andn - 2
(any (monoDigit n) [2 .. n - 2]
).
- It must be greater than or equal to 7 (
-
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
andb
, as input and returnsTrue
ifn
has all its digits (except for the last digit) equal tob
; otherwise, it returnsFalse
. It uses thequotRem
function to iteratively dividen
byb
and check whether the remainder (d
) is equal tob
. -
Main Function:
main :: IO ()
This is the entry point of the program.
-
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, wheres
is a string label andxs
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