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

Published on 12 May 2024 09:40 PM

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

The provided code defines an array of Interval structs and iterates over it to print the so-called "eban" numbers within specified intervals. "Eban" numbers are numbers in which the individual digits (when grouped into blocks of three) are all even or all odd.

  • Struct Interval:

    • Defines a range of numbers (start and end) and a flag (print) indicating whether to print the numbers.
  • Main Program:

    • Initializes an array of Interval objects with different intervals and print flags.
    • Iterates through each interval:
      • Prints a message indicating the interval range.
      • Counts the "eban" numbers within the interval using a loop.
      • Prints the count of "eban" numbers.
  • Loop for Finding "Eban" Numbers:

    • Iterates through numbers within the interval in increments of 2.
    • Divides the number into billions, millions, thousands, and ones (b, m, t, r).
    • Checks if m, t, and r are between 30 and 66 and replaces them with the last digit if so.
    • Checks if b, m, t, and r are even or odd within certain conditions.
    • If the conditions are met, it increments the count and prints the number if the print flag is set.

Source code in the csharp programming language

using System;

namespace EbanNumbers {
    struct Interval {
        public int start, end;
        public bool print;

        public Interval(int start, int end, bool print) {
            this.start = start;
            this.end = end;
            this.print = print;
        }
    }

    class Program {
        static void Main() {
            Interval[] intervals = {
                new Interval(2, 1_000, true),
                new Interval(1_000, 4_000, true),
                new Interval(2, 10_000, false),
                new Interval(2, 100_000, false),
                new Interval(2, 1_000_000, false),
                new Interval(2, 10_000_000, false),
                new Interval(2, 100_000_000, false),
                new Interval(2, 1_000_000_000, false),
            };
            foreach (var intv in intervals) {
                if (intv.start == 2) {
                    Console.WriteLine("eban numbers up to and including {0}:", intv.end);
                } else {
                    Console.WriteLine("eban numbers between {0} and {1} (inclusive):", intv.start, intv.end);
                }

                int count = 0;
                for (int i = intv.start; i <= intv.end; i += 2) {
                    int b = i / 1_000_000_000;
                    int r = i % 1_000_000_000;
                    int m = r / 1_000_000;
                    r = i % 1_000_000;
                    int t = r / 1_000;
                    r %= 1_000;
                    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) Console.Write("{0} ", i);
                                    count++;
                                }
                            }
                        }
                    }
                }
                if (intv.print) {
                    Console.WriteLine();
                }
                Console.WriteLine("count = {0}\n", count);
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Delegates step by step in the Phix programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Delphi programming language
You may also check:How to resolve the algorithm Euler method step by step in the C programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the D programming language