aboutsummaryrefslogtreecommitdiff
path: root/challenge-133/jo-37/perl/ch-1.pl
blob: 263116b17b66b9a6a6ed4ea1fe37813ded3ca0f2 (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
#!/usr/bin/perl -s

use v5.16;
use integer;
use Test2::V0;
use experimental 'signatures';

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [N]

-examples
    run the examples from the challenge
 
-tests
    run some tests

N
    calculate integer square root of N

EOS


### Input and Output

say int_sqrt(shift);


### Implementation

# Following the formulae in
# https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division

sub int_sqrt ($n) {
    my $prev = $n;
    while () {
        my $next = ($prev + $n / $prev) / 2;
        return $prev if $next >= $prev;
        $prev = $next;
    }
}


### Examples and tests

use Math::Prime::Util ();

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is int_sqrt(10), 3, 'example 1';
        is int_sqrt(27), 5, 'example 2';
        is int_sqrt(85), 9, 'example 3';
        is int_sqrt(101), 10, 'example 4';
    }

    SKIP: {
        skip "tests" unless $tests;

        my $mpu_sqrtint = \&Math::Prime::Util::sqrtint;
        grep {
            int_sqrt($_) != $mpu_sqrtint->($_) and !fail "$_ failed";
        } 1 .. 1e5 or pass 'cross check';
	}

    done_testing;
    exit;
}