diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-05 20:20:08 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-05 20:20:08 +0000 |
| commit | ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194 (patch) | |
| tree | 6bd752aaaf3f2e6c35d81156583c4ddc4cb2ae67 /challenge-102 | |
| parent | 0821b332ee9c31f57ca94a56c491288f7b46c3e1 (diff) | |
| download | perlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.tar.gz perlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.tar.bz2 perlweeklychallenge-club-ca934ad2fd8208c9ce9d8a6761c09d7f7ce54194.zip | |
Add Basic, C and C++ solutions to challenge 102
Diffstat (limited to 'challenge-102')
| -rw-r--r-- | challenge-102/paulo-custodio/basic/ch-1.bas | 53 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/basic/ch-2.bas | 44 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/c/ch-1.c | 70 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/c/ch-2.c | 66 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/cpp/ch-1.cpp | 68 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/cpp/ch-2.cpp | 54 |
6 files changed, 355 insertions, 0 deletions
diff --git a/challenge-102/paulo-custodio/basic/ch-1.bas b/challenge-102/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..b33e778d9a --- /dev/null +++ b/challenge-102/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,53 @@ +' Challenge 102 +' +' TASK #1 > Rare Numbers +' Submitted by: Mohammad S Anwar +' +' You are given a positive integer $N. +' +' Write a script to generate all Rare numbers of size $N if exists. Please +' checkout the page for more information about it. +' Examples +' +' (a) 2 digits: 65 +' (b) 6 digits: 621770 +' (c) 9 digits: 281089082 + +' convert 1234 to 4321 +function invert_number(a as Integer) as Integer + dim a1 as Integer + do while a<>0 + a1 = a1*10 + (a mod 10) + a = int(a / 10) + loop + invert_number = a1 +end function + +' check if number is a perfect square +function is_perfect_square(n as Integer) as Boolean + dim sq as Double + sq = sqr(CDbl(n)) + if int(sq)^2=n then + is_perfect_square = True + else + is_perfect_square = False + end if +end function + +' print all rare numbers with n digits +sub print_rare_numbers(n as Integer) + dim r as Integer, r1 as Integer + for r=10^(n-1) to 10^(n)-1 + r1 = invert_number(r) + if is_perfect_square(r+r1) then + if r>=r1 then + if is_perfect_square(r-r1) then + print trim(str(r)) + end if + end if + end if + next +end sub + +' main +print_rare_numbers val(Command(1)) diff --git a/challenge-102/paulo-custodio/basic/ch-2.bas b/challenge-102/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..9987f13b4e --- /dev/null +++ b/challenge-102/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,44 @@ +' Challenge 102 +' +' TASK #2 › Hash-counting String +' Submitted by: Stuart Little +' +' You are given a positive integer $N. +' +' Write a script to produce Hash-counting string of that length. +' +' The definition of a hash-counting string is as follows: +' - the string consists only of digits 0-9 and hashes, ‘#’ +' - there are no two consecutive hashes: ‘##’ does not appear in your string +' - the last character is a hash +' - the number immediately preceding each hash (if it exists) is the position +' of that hash in the string, with the position being counted up from 1 +' +' It can be shown that for every positive integer N there is exactly one such +' length-N string. +' Examples: +' +' (a) "#" is the counting string of length 1 +' (b) "2#" is the counting string of length 2 +' (c) "#3#" is the string of length 3 +' (d) "#3#5#7#10#" is the string of length 10 +' (e) "2#4#6#8#11#14#" is the string of length 14 + +function hash_count(n as Integer) as String + Dim res as String, i as Integer, p as Integer + i = n + do while i>0 + p = i + res = "#" & res + i = i-1 + do while i>0 and p<>0 + res = chr(asc("0") + (p mod 10)) & res + p = int(p / 10) + i = i-1 + loop + loop + hash_count = res +end function + +'main +print hash_count(val(Command(1))) diff --git a/challenge-102/paulo-custodio/c/ch-1.c b/challenge-102/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..49dfb4c3d5 --- /dev/null +++ b/challenge-102/paulo-custodio/c/ch-1.c @@ -0,0 +1,70 @@ +/* +Challenge 102 + +TASK #1 › Rare Numbers +Submitted by: Mohammad S Anwar + +You are given a positive integer $N. + +Write a script to generate all Rare numbers of size $N if exists. Please +checkout the page for more information about it. +Examples + +(a) 2 digits: 65 +(b) 6 digits: 621770 +(c) 9 digits: 281089082 +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <math.h> + +int ipow(int base, int exp) { + int result = 1; + for (;;) { + if (exp & 1) + result *= base; + exp >>= 1; + if (!exp) + break; + base *= base; + } + return result; +} + +int invert_number(int r) { + int r1 = 0; + while (r != 0) { + r1 = r1 * 10 + (r % 10); + r = r / 10; + } + return r1; +} + +bool is_perfect_square(int n) { + double s = sqrt((double)n); + if (floor(s) == s) + return true; + else + return false; +} + +void print_rare(int n) { + int start = ipow(10, n - 1); + int end = ipow(10, n ); + for (int r = start; r < end; r++) { + int r1 = invert_number(r); + if (is_perfect_square(r + r1) && r >= r1 && is_perfect_square(r - r1)) + printf("%d\n", r); + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("Usage: ch-1 N\n", stderr); + return EXIT_FAILURE; + } + else + print_rare(atoi(argv[1])); +} diff --git a/challenge-102/paulo-custodio/c/ch-2.c b/challenge-102/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..c0e1b06f06 --- /dev/null +++ b/challenge-102/paulo-custodio/c/ch-2.c @@ -0,0 +1,66 @@ +/* +Challenge 102 + +TASK #2 › Hash-counting String +Submitted by: Stuart Little + +You are given a positive integer $N. + +Write a script to produce Hash-counting string of that length. + +The definition of a hash-counting string is as follows: +- the string consists only of digits 0-9 and hashes, ‘#’ +- there are no two consecutive hashes: ‘##’ does not appear in your string +- the last character is a hash +- the number immediately preceding each hash (if it exists) is the position +of that hash in the string, with the position being counted up from 1 + +It can be shown that for every positive integer N there is exactly one such +length-N string. +Examples: + +(a) "#" is the counting string of length 1 +(b) "2#" is the counting string of length 2 +(c) "#3#" is the string of length 3 +(d) "#3#5#7#10#" is the string of length 10 +(e) "2#4#6#8#11#14#" is the string of length 14 +*/ + +#include <stdio.h> +#include <stdlib.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +char* hash_count(int n) { + char* str = check_mem(malloc(n + 1)); + str[n] = '\0'; + int i = n - 1; + while (i >= 0) { + int pos = i + 1; + str[i--] = '#'; + while (i >= 0 && pos != 0) { + str[i--] = '0' + (pos % 10); + pos /= 10; + } + } + return str; +} + + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("Usage: ch-1 N\n", stderr); + return EXIT_FAILURE; + } + else { + char* str = hash_count(atoi(argv[1])); + printf("%s\n", str); + free(str); + } +} diff --git a/challenge-102/paulo-custodio/cpp/ch-1.cpp b/challenge-102/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..01d88ccd5a --- /dev/null +++ b/challenge-102/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,68 @@ +/* +Challenge 102 + +TASK #1 › Rare Numbers +Submitted by: Mohammad S Anwar + +You are given a positive integer $N. + +Write a script to generate all Rare numbers of size $N if exists. Please +checkout the page for more information about it. +Examples + +(a) 2 digits: 65 +(b) 6 digits: 621770 +(c) 9 digits: 281089082 +*/ + +#include <iostream> +#include <cmath> + +int ipow(int base, int exp) { + int result = 1; + for (;;) { + if (exp & 1) + result *= base; + exp >>= 1; + if (!exp) + break; + base *= base; + } + return result; +} + +int invert_number(int r) { + int r1 = 0; + while (r != 0) { + r1 = r1 * 10 + (r % 10); + r = r / 10; + } + return r1; +} + +bool is_perfect_square(int n) { + double s = sqrt((double)n); + if (floor(s) == s) + return true; + else + return false; +} + +void print_rare(int n) { + int start = ipow(10, n - 1); + int end = ipow(10, n ); + for (int r = start; r < end; r++) { + int r1 = invert_number(r); + if (is_perfect_square(r + r1) && r >= r1 && is_perfect_square(r - r1)) + std::cout << r << std::endl; + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "Usage: ch-1 N" << std::endl; + return EXIT_FAILURE; + } + else + print_rare(atoi(argv[1])); +} diff --git a/challenge-102/paulo-custodio/cpp/ch-2.cpp b/challenge-102/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..24fbf2fb06 --- /dev/null +++ b/challenge-102/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,54 @@ +/* +Challenge 102 + +TASK #2 › Hash-counting String +Submitted by: Stuart Little + +You are given a positive integer $N. + +Write a script to produce Hash-counting string of that length. + +The definition of a hash-counting string is as follows: +- the string consists only of digits 0-9 and hashes, ‘#’ +- there are no two consecutive hashes: ‘##’ does not appear in your string +- the last character is a hash +- the number immediately preceding each hash (if it exists) is the position +of that hash in the string, with the position being counted up from 1 + +It can be shown that for every positive integer N there is exactly one such +length-N string. +Examples: + +(a) "#" is the counting string of length 1 +(b) "2#" is the counting string of length 2 +(c) "#3#" is the string of length 3 +(d) "#3#5#7#10#" is the string of length 10 +(e) "2#4#6#8#11#14#" is the string of length 14 +*/ + +#include <iostream> +#include <string> + +std::string hash_count(int n) { + std::string str(n, ' '); + int i = n - 1; + while (i >= 0) { + int pos = i + 1; + str[i--] = '#'; + while (i >= 0 && pos != 0) { + str[i--] = '0' + (pos % 10); + pos /= 10; + } + } + return str; +} + + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "Usage: ch-1 N" << std::endl; + return EXIT_FAILURE; + } + else + std::cout << hash_count(atoi(argv[1])) << std::endl; +} |
