diff options
Diffstat (limited to 'challenge-133/andinus/README.org')
| -rw-r--r-- | challenge-133/andinus/README.org | 67 |
1 files changed, 67 insertions, 0 deletions
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 |
