Palindromic gapful numbers | Rosetta Code | #19

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


Numbers that are (evenly) divisible by the number formed by the first and last digit are known as gapful numbers.

Evenly divisible means divisible with no remainder.

All one─ and two─digit numbers have this property and are trivially excluded. Only numbers ≥ 100 will be considered for this Rosetta Code task.


      Example

1037 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 1037.

A palindromic number, when the number is reversed, is the same as the original number.


Show (nine sets) the first 20 palindromic gapful numbers that end with:

  • the digit 1
  • the digit 2
  • the digit 3
  • ··· ···
  • the digit 9



#include <iostream>
using namespace std;

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

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

int main() {
    int cnt = 1, cnt2, digitnumber, divisor, numbers[9][20], a[9] = { 0 };
    for (int i = 100; cnt <= 180; 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));
        }
        divisor = (digits[digitnumber - 1] * 10) + digits[0];
        if (i % divisor == 0) {
            for (int j = 0, k = digitnumber - 1; j < (digitnumber / 2), k >= (digitnumber + 1) / 2; j++, k--) {
                if (digits[j] != digits[k]) {
                    cnt2++;
                    break;
                }
            }
            if (cnt2 == 0) {
                if (a[digits[0] - 1] < 20) {
                    numbers[digits[0] - 1][a[digits[0] - 1]] = i;
                    a[digits[0] - 1]++;
                    cnt++;
                }
            }
        }
    }
    for (int i = 0; i < 9; i++) {
        cout << "First 20 palindromic gapful numbers that end with " << i + 1 << " are:" << endl << endl;
        for (int j = 0; j < 20; j++) {
            cout << numbers[i][j] << ", ";
        }
        cout << endl << endl << endl;
    }
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac