aboutsummaryrefslogtreecommitdiff
path: root/challenge-133/arne-sommer/raku/integer-square-root-bitwise
blob: 3d7813b566ddcd534a92927ffd7255e884859f10 (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
#! /usr/bin/env raku

unit sub MAIN (Int $N where $N > 0);

say int-sqare-root($N);

sub int-sqare-root ($number)
{
  my $x0 = lrs($number);

  if $x0
  {
    my $x1 = lrs( $x0 + $number / $x0 );
		
    while $x1 < $x0			
    {
      $x0 = $x1;
      $x1 = lrs( $x0 + $number / $x0 );
    }
    return $x0;
  }
  else
  {
    return $number;
  }
}

sub lrs ($value)
{
  my $binary = $value.Int.base(2);
  my $new    = '0' ~ $binary.substr(0, $binary.chars -1);
  return $new.parse-base(2);
}