How to resolve the algorithm Brazilian numbers step by step in the Racket programming language
How to resolve the algorithm Brazilian numbers step by step in the Racket 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 Racket programming language
Source code in the racket programming language
#lang racket
(require math/number-theory)
(define (repeat-digit? n base d-must-be-1?)
(call-with-values
(λ () (quotient/remainder n base))
(λ (q d) (and (or (not d-must-be-1?) (= d 1))
(let loop ((n q))
(if (zero? n)
d
(call-with-values
(λ () (quotient/remainder n base))
(λ (q r) (and (= d r) (loop q))))))))))
(define (brazilian? n (for-prime? #f))
(for/first ((b (in-range 2 (sub1 n))) #:when (repeat-digit? n b for-prime?)) b))
(define (prime-brazilian? n)
(and (prime? n) (brazilian? n #t)))
(module+ main
(displayln "First 20 Brazilian numbers:")
(stream->list (stream-take (stream-filter brazilian? (in-naturals)) 20))
(displayln "First 20 odd Brazilian numbers:")
(stream->list (stream-take (stream-filter brazilian? (stream-filter odd? (in-naturals))) 20))
(displayln "First 20 prime Brazilian numbers:")
(stream->list (stream-take (stream-filter prime-brazilian? (stream-filter odd? (in-naturals))) 20)))
You may also check:How to resolve the algorithm Animation step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Combinations step by step in the M4 programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the Tcl programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Mathematica / Wolfram Language programming language