Commit 8254c6c9 by Andrew Dahl

Solved Problem 145

parent 257a2a8e
Showing with 50 additions and 0 deletions
Question:
Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).
There are 120 reversible numbers below one-thousand.
How many reversible numbers are there below one-billion (10^9)?
Answer: 608720
File added
#include <iostream>
using namespace std;
int reverse(int num) {
int rev = 0;
while(num != 0) {
rev = (rev * 10) + (num % 10);
num = num / 10;
}
return rev;
}
bool is_odds(int num) {
while(num != 0) {
if(num % 2 == 0)
return false;
num = num / 10;
}
return true;
}
int main() {
int end = 1000000000;
int num = 0;
int temp = 0;
for(int start = 1; start < end; start++) {
if(start % 10 != 0) {
temp = start + reverse(start);
if(is_odds(temp))
num++;
}
}
cout << num << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment