Gapful numbers | Rosetta Code | #6
URL to the problem page: http://rosettacode.org/wiki/Gapful_numbers
Numbers (positive integers expressed in base ten) 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
187 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 187.
About 7.46% of positive integers are gapful.
Show the first 30 gapful numbers.
Numbers (positive integers expressed in base ten) 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
187 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 187.
About 7.46% of positive integers are gapful.
Show the first 30 gapful numbers.
#include <iostream>
using namespace std;
int power(int a, int b) {
int result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
}
int main()
{
int number, counter = 0, tmp, digits;
cout << "First 30 gapful numbers are:" << endl;
for (int i = 100; counter < 30; i++) {
tmp = i;
digits = 1;
while (tmp >= 10) {
digits++;
tmp /= 10;
}
number = (i % 10) + ((i / power(10, digits - 1)) * 10);
if (i % number == 0) {
counter++;
cout << i << endl;
}
}
return 0;
}
Comments
Post a Comment