diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-04 22:46:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-04 22:46:06 +0100 |
| commit | 128fbc79fed882f4a6705110fb44c52c19f9891c (patch) | |
| tree | 27afbe956329380fef5246c713c7a72a5be86e55 | |
| parent | c0067ebe19afac01b42ccada07972d3833c4d762 (diff) | |
| parent | f9289c717b82cfc4b717f289374bfebeeefc21d9 (diff) | |
| download | perlweeklychallenge-club-128fbc79fed882f4a6705110fb44c52c19f9891c.tar.gz perlweeklychallenge-club-128fbc79fed882f4a6705110fb44c52c19f9891c.tar.bz2 perlweeklychallenge-club-128fbc79fed882f4a6705110fb44c52c19f9891c.zip | |
Merge pull request #4966 from andinus/master
Add solutions for challenge 133
| -rw-r--r-- | challenge-133/andinus/README | 76 | ||||
| -rw-r--r-- | challenge-133/andinus/README.org | 67 | ||||
| -rw-r--r-- | challenge-133/andinus/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-133/andinus/c/ch-1.c | 26 | ||||
| -rw-r--r-- | challenge-133/andinus/raku/ch-1.raku | 11 |
5 files changed, 158 insertions, 23 deletions
diff --git a/challenge-133/andinus/README b/challenge-133/andinus/README index 7e67d6381d..93034423ed 100644 --- a/challenge-133/andinus/README +++ b/challenge-133/andinus/README @@ -1,51 +1,81 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 130 + CHALLENGE 133 Andinus ━━━━━━━━━━━━━━━ - 2021-09-14 + 2021-10-04 -Table of Contents -───────────────── -Task 1 - Odd Number +Task 1 - Integer Square Root +════════════════════════════ + You are given a positive integer `$N'. + Write a script to calculate the integer square root of the given + number. -Task 1 - Odd Number -═══════════════════ + Please avoid using built-in function. Find out more about it [here]. - You are given an array of positive integers, such that all the numbers - appear even number of times except one number. - - Write a script to find that integer. - - Examples: ┌──── - │ Input: @N = (2, 5, 4, 4, 5, 5, 2) - │ Output: 5 as it appears 3 times in the array where as all other numbers 2 and 4 appears exactly twice. + │ Input: $N = 10 + │ Output: 3 + │ + │ Input: $N = 27 + │ Output: 5 + │ + │ Input: $N = 85 + │ Output: 9 │ - │ Input: @N = (1, 2, 3, 4, 3, 2, 1, 4, 4) - │ Output: 4 + │ Input: $N = 101 + │ Output: 10 └──── +[here] <https://en.wikipedia.org/wiki/Integer_square_root> + Raku ──── • Program: <file:raku/ch-1.raku> - `@nums' holds the input and `%seen' is True if it's seen odd number of - times. We loop over `@nums' and invert the value of that number in - `%seen'. + Initial estimate is set to `$n +> 1', then we loop infinitely until + previous 2 estimates are equal. + + ┌──── + │ my $x = $n +> 1; + │ loop { + │ given ($x + ($n / $x)) / 2 { + │ last if $x == $_; + │ $x = $_; + │ } + │ } + │ put $x; + └──── + + +C +─ + + • Program: <file:c/ch-1.c> + + `argv' holds the input & `argc' holds the number of inputs. The input + should be a single integer so `argc' should be equal to 2. After + checking for that, we check if valid value was passed. + + Loop until previous 2 estimates are equal. ┌──── - │ my Bool %seen; - │ %seen{$_} = !%seen{$_} for @nums; - │ put %seen.grep(*.value).map(*.key); + │ double x = num >> 1; + │ for (;;) { + │ double x_next = (x + (num / x)) / 2; + │ if (x == x_next) + │ break; + │ x = x_next; + │ } + │ printf("%f\n", x); └──── diff --git a/challenge-133/andinus/README.org b/challenge-133/andinus/README.org new file mode 100644 index 0000000000..a8c25aedc3 --- /dev/null +++ b/challenge-133/andinus/README.org @@ -0,0 +1,67 @@ +#+title: Challenge 133 +#+date: 2021-10-04 +#+html_link_up: ../ +#+export_file_name: index +#+options: toc:nil +#+setupfile: ~/.emacs.d/org-templates/level-2.org + +* Task 1 - Integer Square Root + +You are given a positive integer ~$N~. + +Write a script to calculate the integer square root of the given number. + +Please avoid using built-in function. Find out more about it [[https://en.wikipedia.org/wiki/Integer_square_root][here]]. + +#+begin_src +Input: $N = 10 +Output: 3 + +Input: $N = 27 +Output: 5 + +Input: $N = 85 +Output: 9 + +Input: $N = 101 +Output: 10 +#+end_src + +** Raku + +- Program: [[file:raku/ch-1.raku]] + +Initial estimate is set to ~$n +> 1~, then we loop infinitely until +previous 2 estimates are equal. + +#+begin_src raku +my $x = $n +> 1; +loop { + given ($x + ($n / $x)) / 2 { + last if $x == $_; + $x = $_; + } +} +put $x; +#+end_src + +** C + +- Program: [[file:c/ch-1.c]] + +~argv~ holds the input & ~argc~ holds the number of inputs. The input should +be a single integer so ~argc~ should be equal to 2. After checking for +that, we check if valid value was passed. + +Loop until previous 2 estimates are equal. + +#+begin_src c +double x = num >> 1; +for (;;) { + double x_next = (x + (num / x)) / 2; + if (x == x_next) + break; + x = x_next; + } +printf("%f\n", x); +#+end_src diff --git a/challenge-133/andinus/blog-1.txt b/challenge-133/andinus/blog-1.txt new file mode 100644 index 0000000000..cd040d689d --- /dev/null +++ b/challenge-133/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.unfla.me/pwc/challenge-133/ diff --git a/challenge-133/andinus/c/ch-1.c b/challenge-133/andinus/c/ch-1.c new file mode 100644 index 0000000000..f28d6d9355 --- /dev/null +++ b/challenge-133/andinus/c/ch-1.c @@ -0,0 +1,26 @@ +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <limits.h> + +int main(int argc, char *argv[]) { + if (argc != 2) { + puts("Usage: ./ch-1 <positive number>"); + exit(0); + } + + const char *errstr; + long num = strtonum(argv[1], 1, LONG_MAX, &errstr); + if (errstr != NULL) + errx(1, "number is %s: %s", errstr, argv[1]); + + double x = num >> 1; + for (;;) { + double x_next = (x + (num / x)) / 2; + if (x == x_next) + break; + x = x_next; + } + printf("%f\n", x); +} diff --git a/challenge-133/andinus/raku/ch-1.raku b/challenge-133/andinus/raku/ch-1.raku new file mode 100644 index 0000000000..cde47fcabe --- /dev/null +++ b/challenge-133/andinus/raku/ch-1.raku @@ -0,0 +1,11 @@ +#| Integer Square Root +sub MAIN(UInt $n) { + my $x = $n +> 1; + loop { + given ($x + ($n / $x)) / 2 { + last if $x == $_; + $x = $_; + } + } + put $x; +} |
