How to resolve the algorithm Eban numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Eban numbers step by step in the C programming language

Table of Contents

Problem Statement

An   eban   number is a number that has no letter   e   in it when the number is spelled in English. Or more literally,   spelled numbers that contain the letter   e   are banned.

The American version of spelling numbers will be used here   (as opposed to the British). 2,000,000,000   is two billion,   not   two milliard.

Only numbers less than   one sextillion   (1021)   will be considered in/for this task. This will allow optimizations to be used.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Eban numbers step by step in the C programming language

This C code defines an array of Interval structs and iterates through them to print "eban" numbers within specified ranges. An "eban" number is one that only contains certain repeating digits (0, 2, 4, or 6).

  • Header Includes:

    • #include "stdio.h": For input and output functions.
    • #include "stdbool.h": For the bool data type.
  • Array Length Macro:

    • #define ARRAY_LEN(a,T) (sizeof(a) / sizeof(T)): A macro to calculate the length of an array by dividing its size by the size of its elements.
  • Interval Struct:

    • Defines a struct Interval with three members:
      • start: The starting value of the interval.
      • end: The ending value of the interval.
      • print: A boolean value indicating whether to print the eban numbers within the interval.
  • main Function:

    • Initializes an array intervals of Interval structs with predefined ranges and print settings.
    • Iterates through the intervals array:
      • For each interval, prints the range and calculates the count of eban numbers within it.
      • If intv.print is true, prints the eban numbers and their count.
  • Calculating Eban Numbers:

    • The code iterates through numbers in the specified range and checks if they satisfy the "eban" condition:
      • All digits except the first are either 0, 2, 4, or 6.
  • Printing Results:

    • For each interval, it prints the range of eban numbers and their count. If intv.print is true, it also prints each eban number within the interval.

Source code in the c programming language

#include "stdio.h"
#include "stdbool.h"

#define ARRAY_LEN(a,T) (sizeof(a) / sizeof(T))

struct Interval {
    int start, end;
    bool print;
};

int main() {
    struct Interval intervals[] = {
        {2, 1000, true},
        {1000, 4000, true},
        {2, 10000, false},
        {2, 100000, false},
        {2, 1000000, false},
        {2, 10000000, false},
        {2, 100000000, false},
        {2, 1000000000, false},
    };
    int idx;

    for (idx = 0; idx < ARRAY_LEN(intervals, struct Interval); ++idx) {
        struct Interval intv = intervals[idx];
        int count = 0, i;

        if (intv.start == 2) {
            printf("eban numbers up to and including %d:\n", intv.end);
        } else {
            printf("eban numbers between %d and %d (inclusive:)", intv.start, intv.end);
        }

        for (i = intv.start; i <= intv.end; i += 2) {
            int b = i / 1000000000;
            int r = i % 1000000000;
            int m = r / 1000000;
            int t;

            r = i % 1000000;
            t = r / 1000;
            r %= 1000;
            if (m >= 30 && m <= 66) m %= 10;
            if (t >= 30 && t <= 66) t %= 10;
            if (r >= 30 && r <= 66) r %= 10;
            if (b == 0 || b == 2 || b == 4 || b == 6) {
                if (m == 0 || m == 2 || m == 4 || m == 6) {
                    if (t == 0 || t == 2 || t == 4 || t == 6) {
                        if (r == 0 || r == 2 || r == 4 || r == 6) {
                            if (intv.print) printf("%d ", i);
                            count++;
                        }
                    }
                }
            }
        }
        if (intv.print) {
            printf("\n");
        }
        printf("count = %d\n\n", count);
    }

    return 0;
}


  

You may also check:How to resolve the algorithm Extend your language step by step in the Lua programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the C# programming language
You may also check:How to resolve the algorithm 21 game step by step in the Raku programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Elena programming language