How to resolve the algorithm Brazilian numbers step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

INCLUDE "H6:SIEVE.ACT"

BYTE FUNC SameDigits(INT x,b)
  INT d

  d=x MOD b
  x==/b
  WHILE x>0
  DO
    IF x MOD b#d THEN
      RETURN (0)
    FI
    x==/b
  OD
RETURN (1)

BYTE FUNC IsBrazilian(INT x)
  INT b

  IF x<7 THEN RETURN (0) FI
  IF x MOD 2=0 THEN RETURN (1) FI
  FOR b=2 TO x-2
  DO
    IF SameDigits(x,b) THEN
      RETURN (1)
    FI
  OD
RETURN (0)

PROC Main()
  DEFINE COUNT="20"
  DEFINE MAXNUM="3000"
  BYTE ARRAY primes(MAXNUM+1)
  INT i,x,c
  CHAR ARRAY s

  Put(125) PutE() ;clear the screen
  Sieve(primes,MAXNUM+1)

  FOR i=0 TO 2
  DO
    IF i=0 THEN
      s=" "
    ELSEIF i=1 THEN
      s=" odd "
    ELSE
      s=" prime "
    FI
    PrintF("First %I%SBrazilian numbers:%E",COUNT,s)
    c=0 x=7
    DO
      IF IsBrazilian(x) THEN
        PrintI(x) Put(32)
        c==+1
        IF c=COUNT THEN EXIT FI
      FI
      IF i=0 THEN
        x==+1
      ELSEIF i=1 THEN
        x==+2
      ELSE
        DO
          x==+2
        UNTIL primes(x)
        OD
      FI
    OD
    PutE() PutE()
  OD
RETURN

  

You may also check:How to resolve the algorithm FASTA format step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Character codes step by step in the PureBasic programming language
You may also check:How to resolve the algorithm String length step by step in the FreeBASIC programming language