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.



E.G.

 1, 2 & 3 can not be Brazilian; there is no base B that satisfies the condition 1 < B < N-1.
 4 is not Brazilian; 4 in base 2 is 100. The digits are not all the same.
 5 is not Brazilian; 5 in base 2 is 101, in base 3 is 12. There is no representation where the digits are the same.
 6 is not Brazilian; 6 in base 2 is 110, in base 3 is 20, in base 4 is 12. There is no representation where the digits are the same.
 7 is Brazilian; 7 in base 2 is 111. There is at least one representation where the digits are all the same.
 8 is Brazilian; 8 in base 3 is 22. There is at least one representation where the digits are all the same.
 and so on...


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 R*S is Brazilian because R*S = 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


Task

Write a routine (function, whatever) to determine if a number is Brazilian and use the routine to show here, on this page;

 the first 20 Brazilian numbers;
 the first 20 odd Brazilian numbers;
 the first 20 prime Brazilian numbers;


See also

 OEIS:A125134 - Brazilian numbers
 OEIS:A257521 - Odd Brazilian numbers
 OEIS:A085104 - Prime Brazilian numbers


/*REXX pgm finds:  1st N Brazilian #s;  odd Brazilian #s;  prime Brazilian #s;  ZZZth #.*/
parse arg  t.1  t.2  t.3  t.4  .                 /*obtain optional arguments from the CL*/
if t.4=='' | t.4==","  then t.4= 0               /*special test case of Nth Brazilian #.*/
hdr.1= 'first';   hdr.2= "first odd";   hdr.3= 'first prime';   hdr.4=   /*four headers.*/
                                        #p= 0    /*#P:   the number of primes  (so far).*/
    do c=1  for 4                                /*process each of the four cases.      */
    if t.c=='' | t.c==","  then t.c= 20          /*check if a target is null or a comma.*/
    step= 1 + (c==2)                             /*STEP is set to unity or two (for ODD)*/
    if t.c==0  then iterate                      /*check to see if this case target ≡ 0.*/
    $=;                       #= 0               /*initialize list to null; counter to 0*/
       do j=1  by step  until #>= t.c            /*search integers for Brazilian # type.*/
       prime= 0                                  /*signify if  J  may not be prime.     */
       if c==3  then do                          /*is this a  "case 3"  calculation?    */
                     if \isPrime(j) then iterate /*(case 3)  Not a prime?  Then skip it.*/
                     prime= 1                    /*signify if  J  is definately a prime.*/
                     end                         /* [↓] J≡prime will be used for speedup*/
       if \isBraz(j, prime)  then iterate        /*Not  Brazilian number?   "    "    " */
       #= # + 1                                  /*bump the counter of Brazilian numbers*/
       if c\==4  then $= $  j                    /*for most cases, append J to ($) list.*/
       end   /*j*/                               /* [↑] cases 1──►3, $ has leading blank*/
    say                                          /* [↓]  use a special header for cases.*/
    if c==4  then do;  $= j;  t.c= th(t.c);  end /*for Nth Brazilian number, just use J.*/
    say center(' 'hdr.c" "    t.c      " Brazilian number"left('s',  c\==4)" ",  79,  '═')
    say strip($)                                 /*display a case result to the terminal*/
    end      /*c*/                               /* [↑] cases 1──►3 have a leading blank*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isBraz:  procedure; parse arg x,p;  if x<7      then return 0  /*Is # < seven?  Nope.   */
                                    if x//2==0  then return 1  /*Is # even?     Yup.   _*/
         if p  then mx= iSqrt(x)                               /*X prime? Use integer √X*/
               else mx= x%3 -1                                 /*X  not known if prime. */
                              do b=2  for mx                   /*scan for base 2 ──► max*/
                              if sameDig(x, b)  then return 1  /*it's a Brazilian number*/
                              end   /*b*/;           return 0  /*not  "     "        "  */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isPrime: procedure expose @. !. #p; parse arg x '' -1 _ /*get 1st arg & last decimal dig*/
         if #p==0 then do;  !.=0;  y= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
                         do i=1  for words(y);  #p= #p+1; z=word(y,i); @.#p= z; !.z=1; end
                       end                              /*#P:  is the number of primes. */
         if !.x      then return 1;   if x<61  then return 0;  if x//2==0  then return 0
         if x//3==0  then return 0;   if _==5  then return 0;  if x//7==0  then return 0
            do j=5  until @.j**2>x;                 if x//@.j     ==0  then return 0
                                                    if x//(@.j+2) ==0  then return 0
            end   /*j*/;   #p= #p + 1;   @.#p= x;   !.x= 1;    return 1  /*it's a prime.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt:   procedure; parse arg x;     q= 1;     r= 0;        do while q<=x;   q= q*4;   end
          do while q>1; q=q%4; _=x-r-q; r=r%2; if _>=0 then do;x=_;r=r+q;end;end; return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
sameDig: procedure; parse arg x, b;           f= x // b        /* //  ◄── the remainder.*/
                                              x= x  % b        /*  %  ◄── is integer  ÷ */
                    do while x>0;  if x//b \==f  then return 0
                    x= x % b
                    end   /*while*/;      return 1             /*it has all the same dig*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: parse arg th; return th || word('th st nd rd', 1+(th//10)*(th//100%10\==1)*(th//10<4))