blob: 93034423eda55ad485dd13e6438264b3d2282c40 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
━━━━━━━━━━━━━━━
CHALLENGE 133
Andinus
━━━━━━━━━━━━━━━
2021-10-04
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 [here].
┌────
│ Input: $N = 10
│ Output: 3
│
│ Input: $N = 27
│ Output: 5
│
│ Input: $N = 85
│ Output: 9
│
│ Input: $N = 101
│ Output: 10
└────
[here] <https://en.wikipedia.org/wiki/Integer_square_root>
Raku
────
• Program: <file:raku/ch-1.raku>
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.
┌────
│ double x = num >> 1;
│ for (;;) {
│ double x_next = (x + (num / x)) / 2;
│ if (x == x_next)
│ break;
│ x = x_next;
│ }
│ printf("%f\n", x);
└────
|