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

Published on 12 May 2024 09:40 PM

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

Source code in the groovy programming language

import org.codehaus.groovy.GroovyBugError

class Brazilian {
    private static final List<Integer> primeList = new ArrayList<>(Arrays.asList(
            2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
            97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 169, 173, 179, 181,
            191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 247, 251, 257, 263, 269, 271, 277, 281,
            283, 293, 299, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 377, 379, 383, 389,
            397, 401, 403, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 481, 487, 491,
            499, 503, 509, 521, 523, 533, 541, 547, 557, 559, 563, 569, 571, 577, 587, 593, 599, 601, 607,
            611, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 689, 691, 701, 709, 719,
            727, 733, 739, 743, 751, 757, 761, 767, 769, 773, 787, 793, 797, 809, 811, 821, 823, 827, 829,
            839, 853, 857, 859, 863, 871, 877, 881, 883, 887, 907, 911, 919, 923, 929, 937, 941, 947, 949,
            953, 967, 971, 977, 983, 991, 997
    ))

    static boolean isPrime(int n) {
        if (n < 2) {
            return false
        }

        for (Integer prime : primeList) {
            if (n == prime) {
                return true
            }
            if (n % prime == 0) {
                return false
            }
            if (prime * prime > n) {
                return true
            }
        }

        BigInteger bi = BigInteger.valueOf(n)
        return bi.isProbablePrime(10)
    }

    private static boolean sameDigits(int n, int b) {
        int f = n % b
        n = n.intdiv(b)
        while (n > 0) {
            if (n % b != f) {
                return false
            }
            n = n.intdiv(b)
        }
        return true
    }

    private static boolean isBrazilian(int n) {
        if (n < 7) return false
        if (n % 2 == 0) return true
        for (int b = 2; b < n - 1; ++b) {
            if (sameDigits(n, b)) {
                return true
            }
        }
        return false
    }

    static void main(String[] args) {
        for (String kind : Arrays.asList("", "odd ", "prime ")) {
            boolean quiet = false
            int bigLim = 99_999
            int limit = 20
            System.out.printf("First %d %sBrazilian numbers:\n", limit, kind)
            int c = 0
            int n = 7
            while (c < bigLim) {
                if (isBrazilian(n)) {
                    if (!quiet) System.out.printf("%d ", n)
                    if (++c == limit) {
                        System.out.println("\n")
                        quiet = true
                    }
                }
                if (quiet && "" != kind) continue
                switch (kind) {
                    case "":
                        n++
                        break
                    case "odd ":
                        n += 2
                        break
                    case "prime ":
                        while (true) {
                            n += 2
                            if (isPrime(n)) break
                        }
                        break
                    default:
                        throw new GroovyBugError("Oops")
                }
            }
            if ("" == kind) {
                System.out.printf("The %dth Brazilian number is: %d\n\n", bigLim + 1, n)
            }
        }
    }
}


  

You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Haskell programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the Oz programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the APL programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Priority queue step by step in the ARM Assembly programming language