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

Published on 12 May 2024 09:40 PM
#D

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

Source code in the d programming language

import std.stdio;

bool sameDigits(int n, int b) {
    int f = n % b;
    while ((n /= b) > 0) {
        if (n % b != f) {
            return false;
        }
    }
    return true;
}

bool 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;
}

bool isPrime(int n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    if (n % 3 == 0) return n == 3;
    int d = 5;
    while (d * d <= n) {
        if (n % d == 0) return false;
        d += 2;
        if (n % d == 0) return false;
        d += 4;
    }
    return true;
}

void main() {
    foreach (kind; ["", "odd ", "prime "]) {
        bool quiet = false;
        int BigLim = 99999;
        int limit = 20;
        writefln("First %s %sBrazillion numbers:", limit, kind);
        int c = 0;
        int n = 7;
        while (c < BigLim) {
            if (isBrazilian(n)) {
                if (!quiet) write(n, ' ');
                if (++c == limit) {
                    writeln("\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: assert(false);
            }
        }
        if (kind == "") writefln("The %sth Brazillian number is: %s\n", BigLim + 1, n);
    }
}


  

You may also check:How to resolve the algorithm Topic variable step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Kabap programming language
You may also check:How to resolve the algorithm Bifid cipher step by step in the Julia programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the newLISP programming language