From 136fb62d1de876d28a11733c8a8bd2d74f6e535b Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 9 Oct 2021 12:31:16 +0200 Subject: Challenge 133 Task 1 Perl Python --- challenge-133/lubos-kolouch/perl/ch-1.pl | 36 ++++++++++++++++++++++++++++++ challenge-133/lubos-kolouch/python/ch-1.py | 28 +++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-133/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-133/lubos-kolouch/python/ch-1.py 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 -- cgit From 87703e7d24524efcd104b1b3f70c97b1719ecd03 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 9 Oct 2021 13:45:13 +0200 Subject: Challenge 133 Task 2 Python LK --- challenge-133/lubos-kolouch/python/ch-2.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 challenge-133/lubos-kolouch/python/ch-2.py 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)) -- cgit