aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-10-04 18:46:26 +0530
committerAndinus <andinus@nand.sh>2021-10-04 18:46:26 +0530
commitac9c54c2a8b6e43ad154c8b67ea5c2810d0791cd (patch)
treea63bdefa19caa4f0f5c1a7528b8f8f5dfc6cdc98
parent20ffd7b7c80e195548325ace22fe92f2a499984c (diff)
downloadperlweeklychallenge-club-ac9c54c2a8b6e43ad154c8b67ea5c2810d0791cd.tar.gz
perlweeklychallenge-club-ac9c54c2a8b6e43ad154c8b67ea5c2810d0791cd.tar.bz2
perlweeklychallenge-club-ac9c54c2a8b6e43ad154c8b67ea5c2810d0791cd.zip
Add solutions for challenge 133
-rw-r--r--challenge-133/andinus/README76
-rw-r--r--challenge-133/andinus/README.org67
-rw-r--r--challenge-133/andinus/c/ch-1.c26
-rw-r--r--challenge-133/andinus/raku/ch-1.raku11
4 files changed, 157 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/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;
+}