Magnanimous numbers | Rosetta Code | #21

URL to the problem page: http://rosettacode.org/wiki/Magnanimous_numbers


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.

E.G.


  • 6425 is a magnanimous number. 6 + 425 == 431 which is prime; 64 + 25 == 89 which is prime; 642 + 5 == 647 which is prime.
  • 3538 is not a magnanimous number. 3 + 538 == 541 which is prime; 35 + 38 == 73 which is prime; but 353 + 8 == 361 which is not prime.


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.


Find and display, here on this page the first 45 magnanimous numbers.
Find and display, here on this page the 241st through 250th magnanimous numbers.
Find and display, here on this page the 391st through 400th magnanimous numbers.



#include <iostream>
using namespace std;

int finddigits(int a) {
    int result = 1;
    while (a >= 10) {
        result++;
        a /= 10;
    }
    return result;
}

int power(int aint b) {
    int result = 1;
    for (int i = 0; i < b; i++) {
        result *= a;
    }
    return result;
}

bool primecheck(int a) {
    int cnt = 0;
    for (int i = 2; i <= sqrt(a); i++) {
        if (a % i == 0) {
            cnt++;
            break;
        }
    }
    if (cnt == 0) {
        return 1;
    }
    else {
        return 0;
    }
}

int main() {
    int cnt = 0, cnt2, digitnumber, number1, number2, result[65], n = 0;
    for (int i = 0; cnt < 400; i++) {
        cnt2 = 0;
        digitnumber = finddigits(i);
        int* digits = new int[digitnumber];
        for (int j = 0; j < digitnumber; j++) {
            digits[j] = (i % power(10, j + 1)) / power(10, j);
        }
        for (int j = digitnumber - 1; j > 0; j--) {
            number1 = 0;
            number2 = 0;
            for (int k = j, a = 0; k < digitnumber; k++, a++) {
                number1 += digits[k] * power(10, a);
            }
            for (int k = 0; k < j; k++) {
                number2 += digits[k] * power(10, k);
            }
            if (primecheck(number1 + number2) == 0) {
                cnt2++;
                break;
            }
        }
        if (cnt2 == 0 && i != 10) {
            if (cnt < 45 || cnt > 239 && cnt < 250 || cnt > 389 && cnt < 400) {
                result[n] = i;
                n++;
            }
            cnt++;
        }
    }
    cout << "First 45 magnanimous numbers are:" << endl;
    for (int i = 0; i < 65; i++) {
        if (i == 45) {
            cout << endl << endl << "241st through 250th magnanimous numbers are:" << endl;
        }
        else if (i == 55) {
            cout << endl << endl << "391st through 400th magnanimous numbers are:" << endl;
        }
        cout << result[i] << ", ";
    }
    cout << endl;
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac