aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-09 16:07:58 +0100
committerGitHub <noreply@github.com>2021-10-09 16:07:58 +0100
commit230eefba416d22cdf3baa58766840e96f78f1261 (patch)
tree8677db82bcda3296fd28f67a73888dd299b28834
parent8c655222ad4da687d7e6ad5ce653ca54d36dd952 (diff)
parent87703e7d24524efcd104b1b3f70c97b1719ecd03 (diff)
downloadperlweeklychallenge-club-230eefba416d22cdf3baa58766840e96f78f1261.tar.gz
perlweeklychallenge-club-230eefba416d22cdf3baa58766840e96f78f1261.tar.bz2
perlweeklychallenge-club-230eefba416d22cdf3baa58766840e96f78f1261.zip
Merge pull request #4990 from LubosKolouch/master
Challenge 133 Task 1 Perl Python
-rw-r--r--challenge-133/lubos-kolouch/perl/ch-1.pl36
-rw-r--r--challenge-133/lubos-kolouch/python/ch-1.py28
-rw-r--r--challenge-133/lubos-kolouch/python/ch-2.py9
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-133/lubos-kolouch/perl/ch-1.pl b/challenge-133/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..4381d74d4e
--- /dev/null
+++ b/challenge-133/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+
+sub get_square_root {
+ my ( $what, $low, $high ) = @_;
+
+ $high //= $what;
+ $low //= 0;
+
+ my $middle = int( ( $high + $low ) / 2 );
+ my $incr_middle = $middle + 1;
+
+ # halve the interval
+
+ if ( ( $middle * $middle <= $what ) and ( $incr_middle * $incr_middle >= $what ) ) {
+ return $middle;
+ }
+
+ if ( $middle * $middle > $what ) {
+ $middle = get_square_root( $what, $low, $middle );
+ }
+ else {
+ $middle = get_square_root( $what, $middle, $high );
+ }
+
+ return $middle;
+}
+
+use Test::More;
+
+is( get_square_root(10), 3 );
+is( get_square_root(27), 5 );
+is( get_square_root(85), 9 );
+is( get_square_root(101), 10 );
+
+done_testing;
diff --git a/challenge-133/lubos-kolouch/python/ch-1.py b/challenge-133/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..41c973def1
--- /dev/null
+++ b/challenge-133/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,28 @@
+""" The Weekly Challenge 133 Task 1 """
+
+
+def get_square_root(what: int, low: int = 0, high: int = 0):
+ """ Calculate the square root using interval halving"""
+
+ if not high:
+ high = what
+
+ middle = int((high + low) / 2)
+ incr_middle = middle + 1
+
+ if middle * middle <= what <= incr_middle * incr_middle:
+ return middle
+
+ if middle * middle > what:
+ middle = get_square_root(what, low, middle)
+
+ else:
+ middle = get_square_root(what, middle, high)
+
+ return middle
+
+
+assert get_square_root(10) == 3
+assert get_square_root(27) == 5
+assert get_square_root(85) == 9
+assert get_square_root(101) == 10
diff --git a/challenge-133/lubos-kolouch/python/ch-2.py b/challenge-133/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..5762c277d2
--- /dev/null
+++ b/challenge-133/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from pyoeis import OEISClient
+
+c = OEISClient()
+seq = c.get_by_id('A104170')
+
+print(seq.unsigned(10))