diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-02 20:32:31 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-02 20:32:31 +0100 |
| commit | 513a02f52eaa1fa3aad6853daf10b3e6da3482ad (patch) | |
| tree | 93be5abf1eaf4a2434231566ea32348959358c1c /challenge-116 | |
| parent | 0ecbc6fb2a0ae9e9e5ccd6092d772579466d48a4 (diff) | |
| download | perlweeklychallenge-club-513a02f52eaa1fa3aad6853daf10b3e6da3482ad.tar.gz perlweeklychallenge-club-513a02f52eaa1fa3aad6853daf10b3e6da3482ad.tar.bz2 perlweeklychallenge-club-513a02f52eaa1fa3aad6853daf10b3e6da3482ad.zip | |
Add C, C++, D and BC solutions to challenge 116
Diffstat (limited to 'challenge-116')
| -rw-r--r-- | challenge-116/paulo-custodio/bc/ch-2.bc | 52 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/c/ch-1.c | 90 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/c/ch-2.c | 48 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/cpp/ch-1.cpp | 65 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/cpp/ch-2.cpp | 47 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/d/ch_1.d | 62 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/d/ch_2.d | 46 |
7 files changed, 410 insertions, 0 deletions
diff --git a/challenge-116/paulo-custodio/bc/ch-2.bc b/challenge-116/paulo-custodio/bc/ch-2.bc new file mode 100644 index 0000000000..a55630a75e --- /dev/null +++ b/challenge-116/paulo-custodio/bc/ch-2.bc @@ -0,0 +1,52 @@ +#!/usr/bin/bc -ql + +/* +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 +*/ + +scale = 10 +num = read() + +define sum_of_squares_is_perfect_square(num) { + if (num < 10) return 0; + + sum = 0; + while (num > 0) { + scale = 0 + digit = num % 10; + num = num / 10; + scale = 10 + sum += digit*digit; +/*print "num ",num," digit ",digit," sum ",sum,"\n"*/ + } + + sqrt_int = sqrt(sum); + scale = 0 + sqrt_int = sqrt_int / 1; + scale = 10 + if (sqrt_int*sqrt_int == sum) { + return 1; + } else { + return 0; + } +} + +sum_of_squares_is_perfect_square(num) +quit diff --git a/challenge-116/paulo-custodio/c/ch-1.c b/challenge-116/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..ff13fd1c11 --- /dev/null +++ b/challenge-116/paulo-custodio/c/ch-1.c @@ -0,0 +1,90 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +bool found_solution = false; + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +char* substr(const char* str, int start, int len) { + int sublen = strlen(str) - start; + assert(sublen >= 0); + + if (sublen > len) sublen = len; + + char* sub = check_mem(malloc(sublen + 1)); + strncpy(sub, str + start, sublen); + sub[sublen] = '\0'; + return sub; +} + +void print_sequences(int prev, const char* seq, const char* rest) { + if (*rest == '\0') { // consumed all rest + if (!found_solution) { // only first solution printed + printf("%s\n", seq); + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= (int)strlen(rest); i++) { + char* prefix = substr(rest, 0, i); + char* suffix = substr(rest, i, strlen(rest)); + char* new_seq = check_mem(malloc(strlen(seq) + 1 + strlen(prefix) + 1)); + int new_prev = atoi(prefix); + + if (suffix[0] != '0') { + if (prev < 0) { // first time + sprintf(new_seq, "%s", prefix); + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + sprintf(new_seq, "%s,%s", seq, prefix); + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + free(prefix); + free(suffix); + free(new_seq); + } + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + print_sequences(-1, "", argv[1]); +} diff --git a/challenge-116/paulo-custodio/c/ch-2.c b/challenge-116/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..976f1ffe1f --- /dev/null +++ b/challenge-116/paulo-custodio/c/ch-2.c @@ -0,0 +1,48 @@ +/* +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 <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <math.h> + +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]); + printf("%d\n", sum_of_squares_is_perfect_square(n) ? 1 : 0); +} diff --git a/challenge-116/paulo-custodio/cpp/ch-1.cpp b/challenge-116/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..34e8882ae9 --- /dev/null +++ b/challenge-116/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,65 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +#include <cassert> +#include <iostream> +#include <string> +using namespace std; + +bool found_solution = false; + +void print_sequences(int prev, const string& seq, const string& rest) { + if (rest.empty()) { // consumed all rest + if (!found_solution) { // only first solution printed + cout << seq << endl; + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= static_cast<int>(rest.size()); i++) { + string prefix = rest.substr(0, i); + string suffix = rest.substr(i); + int new_prev = atoi(prefix.c_str()); + + if (suffix[0] != '0') { + if (prev < 0) { // first time + string new_seq = prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + string new_seq = seq + "," + prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + } + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + print_sequences(-1, "", argv[1]); +} 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; +} diff --git a/challenge-116/paulo-custodio/d/ch_1.d b/challenge-116/paulo-custodio/d/ch_1.d new file mode 100644 index 0000000000..b041438c7e --- /dev/null +++ b/challenge-116/paulo-custodio/d/ch_1.d @@ -0,0 +1,62 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +import std.stdio; +import std.conv; + +bool found_solution = false; + +void print_sequences(int prev, string seq, string rest) { + if (rest == "") { // consumed all rest + if (!found_solution) { // only first solution printed + writeln(seq); + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= rest.length; i++) { + string prefix = rest[0 .. i]; + string suffix = rest[i .. $]; + int new_prev = to!int(prefix); + char suffix_begin = (suffix == "") ? 0 : suffix[0]; + if (suffix_begin != '0') { + if (prev < 0) { // first time + string new_seq = prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + string new_seq = seq ~ "," ~ prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + } + } +} + +void main(string[] args) { + print_sequences(-1, "", args[1]); +} diff --git a/challenge-116/paulo-custodio/d/ch_2.d b/challenge-116/paulo-custodio/d/ch_2.d new file mode 100644 index 0000000000..c8f17d5319 --- /dev/null +++ b/challenge-116/paulo-custodio/d/ch_2.d @@ -0,0 +1,46 @@ +/* +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 +*/ + +import std.stdio; +import std.conv; +import core.stdc.math; + +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; +} + +void main(string[] args) { + int n = to!int(args[1]); + writeln(sum_of_squares_is_perfect_square(n) ? 1 : 0); +} |
