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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Brazilian numbers step by step in the F# 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 F# programming language

Source code in the fsharp programming language

// Generate Brazilian sequence. Nigel Galloway: August 13th., 2019
let isBraz α=let mutable n,i,g=α,α+1,1 in (fun β->(while (i*g)<β do if g<α-1 then g<-g+1 else (n<-n*α; i<-n+i; g<-1)); β=i*g)

let Brazilian()=let rec fN n g=seq{if List.exists(fun α->α n) g then yield n
                                   yield! fN (n+1) ((isBraz (n-1))::g)}
                fN 4 [isBraz 2]


Brazilian() |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""


Brazilian() |> Seq.filter(fun n->n%2=1) |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""


Brazilian() |> Seq.filter isPrime |> Seq.take 20 |> Seq.iter(printf "%d "); printfn ""


printfn "%d" (Seq.item 99999 Brazilian)


  

You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm 24 game step by step in the Wren programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Racket programming language