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

Published on 7 June 2024 03:52 AM
#C

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

Table of Contents

Problem Statement

A magnanimous number is an integer where there is no place in the number where a + (plus sign) could be added between any two digits to give a non-prime sum.

Traditionally the single digit numbers 0 through 9 are included as magnanimous numbers as there is no place in the number where you can add a plus between two digits at all. (Kind of weaselly but there you are...) Except for the actual value 0, leading zeros are not permitted. Internal zeros are fine though, 1001 -> 1 + 001 (prime), 10 + 01 (prime) 100 + 1 (prime). There are only 571 known magnanimous numbers. It is strongly suspected, though not rigorously proved, that there are no magnanimous numbers above 97393713331910, the largest one known.

Let's start with the solution:

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

The provided C code finds and lists magnanimous numbers within a specified range. A magnanimous number is one where the sum of the digits of its numerator and denominator (when expressed as a fraction) are both prime numbers. For example, 132 is a magnanimous number because 1 + 3 + 2 = 6 and 6 is a prime number, and 13/2 = 6.5 and 6 + 5 = 11, which is also a prime number.

Here's a breakdown of the code:

  • Header Files: The code includes the <stdio.h> and <string.h> header files for input/output and string manipulation, respectively.

  • Type Definitions:

    • The code defines bool as a synonym for int, representing Boolean values (TRUE or FALSE).
    • It also defines ull as a synonym for unsigned long long, representing unsigned 64-bit integers.
  • Constants:

    • TRUE and FALSE are defined as 1 and 0, respectively, representing Boolean values.
  • is_prime Function:

    • This function checks if a given number n is a prime number. It uses trial division to check for divisibility by small primes (2 and 3) and then checks for divisibility by odd numbers up to the square root of n. If n is divisible by any of these numbers, it returns FALSE; otherwise, it returns TRUE.
  • ord Function:

    • This function converts an integer n to its ordinal representation (e.g., "1st", "2nd", "3rd", etc.). It checks the last two digits of n and appends the appropriate suffix.
  • is_magnanimous Function:

    • This function checks if a given number n is a magnanimous number. It starts by checking if n is less than 10, in which case it returns TRUE. Otherwise, it iterates through the digits of n from left to right, calculating q as the quotient of n divided by the current power of 10, and r as the remainder. It checks if the sum of q and r is a prime number. If it is not, it returns FALSE. It repeats this process until the quotient becomes less than 10. If all the checks pass, it returns TRUE, indicating that n is a magnanimous number.
  • list_mags Function:

    • This function lists magnanimous numbers within a specified range. It takes three arguments: from (the starting number), thru (the ending number), digs (the number of digits to print per number), and per_line (the number of numbers to print per line).
    • It iterates through a range of numbers starting from i = 0. For each number i, it checks if it is a magnanimous number using the is_magnanimous function. If it is, and its value is greater than or equal to from, it prints the number in the specified format and increments a counter c.
    • When the counter c reaches thru, the function stops iterating and printing.
  • main Function:

    • The main function calls the list_mags function three times with different ranges and formatting options to demonstrate the listing of magnanimous numbers within specified ranges and with different formatting.

Source code in the c programming language

#include <stdio.h> 
#include <string.h>

typedef int bool;
typedef unsigned long long ull;

#define TRUE 1
#define FALSE 0

/* OK for 'small' numbers. */
bool is_prime(ull n) {
    ull d;
    if (n < 2) return FALSE;
    if (!(n % 2)) return n == 2;
    if (!(n % 3)) return n == 3;
    d = 5;
    while (d * d <= n) {
        if (!(n % d)) return FALSE;
        d += 2;
        if (!(n % d)) return FALSE;
        d += 4;
    }
    return TRUE;
}

void ord(char *res, int n) {
    char suffix[3];
    int m = n % 100;
    if (m >= 4 && m <= 20) {
        sprintf(res,"%dth", n);
        return;
    }
    switch(m % 10) {
        case 1:
            strcpy(suffix, "st");
            break;
        case 2:
            strcpy(suffix, "nd");
            break;
        case 3:
            strcpy(suffix, "rd");
            break;
        default:
            strcpy(suffix, "th");
            break;
    }
    sprintf(res, "%d%s", n, suffix);
}

bool is_magnanimous(ull n) {
    ull p, q, r;
    if (n < 10) return TRUE;
    for (p = 10; ; p *= 10) {
        q = n / p;
        r = n % p;
        if (!is_prime(q + r)) return FALSE;
        if (q < 10) break;
    }
    return TRUE;
}

void list_mags(int from, int thru, int digs, int per_line) {
    ull i = 0;
    int c = 0;
    char res1[13], res2[13];
    if (from < 2) {
        printf("\nFirst %d magnanimous numbers:\n", thru);
    } else {
        ord(res1, from);
        ord(res2, thru);
        printf("\n%s through %s magnanimous numbers:\n", res1, res2);
    }
    for ( ; c < thru; ++i) {
        if (is_magnanimous(i)) {
            if (++c >= from) {
                printf("%*llu ", digs, i);
                if (!(c % per_line)) printf("\n");
            }
        }
    }
}
 
int main() {
    list_mags(1, 45, 3, 15);
    list_mags(241, 250, 1, 10);
    list_mags(391, 400, 1, 10);
    return 0;
}


  

You may also check:How to resolve the algorithm N-queens problem step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Comments step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Special variables step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sleep step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Distributed programming step by step in the Oz programming language