diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-07-04 22:18:36 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-07-04 22:18:36 +0800 |
| commit | 28eabc69c70046cc047e98e2b2987d71b4aae9fa (patch) | |
| tree | f2fe2351445f68451ec14f21072719bc9db488b0 /challenge-116/paulo-custodio/cpp/ch-2.cpp | |
| parent | cdfef3f9404041b4c7936a26532757e63fd4fcef (diff) | |
| parent | 5f7b76d5b841606c17f177a90397196ebf434c05 (diff) | |
| download | perlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.tar.gz perlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.tar.bz2 perlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.zip | |
week 119
Diffstat (limited to 'challenge-116/paulo-custodio/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-116/paulo-custodio/cpp/ch-2.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-116/paulo-custodio/cpp/ch-2.cpp b/challenge-116/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..0215e90188 --- /dev/null +++ b/challenge-116/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,47 @@ +/* +Challenge 116 + +TASK #2 - Sum of Squares +Submitted by: Mohammad Meraj Zia +You are given a number $N >= 10. + +Write a script to find out if the given number $N is such that sum of squares +of all digits is a perfect square. Print 1 if it is otherwise 0. + +Example +Input: $N = 34 +Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 + +Input: $N = 50 +Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 + +Input: $N = 52 +Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +*/ + +#include <iostream> +#include <cmath> +using namespace std; + +bool sum_of_squares_is_perfect_square(int num) { + if (num < 10) return false; + + double sum = 0; + while (num > 0) { + int digit = num % 10; + num /= 10; + sum += digit*digit; + } + + double sqrt_int = floor(sqrt(sum)); + if (sqrt_int*sqrt_int == sum) + return true; + else + return false; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + cout << (sum_of_squares_is_perfect_square(n) ? 1 : 0) << endl; +} |
