Brazilian numbers

From Rosetta Code
Revision as of 14:03, 11 August 2019 by Thundergnat (talk | contribs) (New draft task and Perl 6 entry)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Brazilian numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 and 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.


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


Perl 6

Works with: Rakudo version 2019.07.1

<lang perl6>sub is-Brazilian (Int $n) {

   for 2 ..^ $n - 1 { return True if +(my @b = $n.polymod( $_ xx * )) > 1 and [==] @b }
   False

}

my @Brazilians = lazy (^Inf).hyper.grep: { .&is-Brazilian }

put "First 20 Brazilian numbers:\n", @Brazilians[^20];

put "\nFirst 20 odd Brazilian numbers:\n", @Brazilians.grep( * % 2 )[^20];

put "\nFirst 20 prime Brazilian numbers:\n", @Brazilians.grep( *.is-prime )[^20];</lang>

Output:
First 20 Brazilian numbers:
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33

First 20 odd Brazilian numbers:
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77

First 20 prime Brazilian numbers:
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801