diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-09-29 21:37:51 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-09-29 21:37:51 +0200 |
| commit | 4c69de67681a1bb6e78c9adf1072a5f3b459e4c9 (patch) | |
| tree | be70f6a410be995420cf8e732f407669f6df42be /c | |
| parent | f942937de1496e38f2d58d6383d03f5e254abfb6 (diff) | |
| download | project_euler-4c69de67681a1bb6e78c9adf1072a5f3b459e4c9.tar.gz project_euler-4c69de67681a1bb6e78c9adf1072a5f3b459e4c9.tar.bz2 project_euler-4c69de67681a1bb6e78c9adf1072a5f3b459e4c9.zip | |
problem 145 c
Diffstat (limited to 'c')
| -rw-r--r-- | c/145-how_many_reversible_numbers_are_there_below_onebillion.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/c/145-how_many_reversible_numbers_are_there_below_onebillion.c b/c/145-how_many_reversible_numbers_are_there_below_onebillion.c new file mode 100644 index 0000000..4cddb5e --- /dev/null +++ b/c/145-how_many_reversible_numbers_are_there_below_onebillion.c @@ -0,0 +1,52 @@ +/* +* How many reversible numbers are there below one-billion? +* Problem 145 +* +* 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 (109)? +*/ + +#include <stdio.h> +#include <stdbool.h> + +int reverse(int n); +bool is_reversible(int n); + +int main(void) +{ + int counter = 0; + for (int i = 0; i < 1000000000; i++) + { + if (i % 10 == 0) + continue; + if (is_reversible(i)) + counter++; + } + printf("%d\n", counter); + return 0; +} + +bool is_reversible(int n) +{ + int sum = n + reverse(n); + while (sum > 0) + { + if ((sum % 10) % 2 == 0) + return false; + sum /= 10; + } + return true; +} + +int reverse(int n) +{ + int rn = 0; + while (n > 0) + { + rn *= 10; + rn += n % 10; + n /= 10; + } + return rn; +} |
