From b860115509e49b10448c7dcce8dd6f7ecd3d9290 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Thu, 25 May 2023 18:53:22 +0200 Subject: feat(challenge-109/lubos-kolouch/perl,python): Challenge 109 LK Perl Python --- challenge-109/lubos-kolouch/perl/ch-1.pl | 16 ++++++++++++++++ challenge-109/lubos-kolouch/perl/ch-2.pl | 18 ++++++++++++++++++ challenge-109/lubos-kolouch/python/ch-1.py | 18 ++++++++++++++++++ challenge-109/lubos-kolouch/python/ch-2.py | 12 ++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 challenge-109/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-109/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-109/lubos-kolouch/python/ch-1.py create mode 100644 challenge-109/lubos-kolouch/python/ch-2.py diff --git a/challenge-109/lubos-kolouch/perl/ch-1.pl b/challenge-109/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..16d67eb7d2 --- /dev/null +++ b/challenge-109/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,16 @@ +sub chowla { + my ($n) = @_; + my $sum = 0; + for my $i (2 .. sqrt($n)) { + if ($n % $i == 0) { + $sum += $i; + $sum += $n/$i unless $i == $n/$i; + } + } + return $sum; +} + +for my $n (1 .. 20) { + print chowla($n), "\n"; +} + diff --git a/challenge-109/lubos-kolouch/perl/ch-2.pl b/challenge-109/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..f0ef66e780 --- /dev/null +++ b/challenge-109/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,18 @@ +use List::Util qw(sum); + +my @numbers = (1,2,3,4,5,6,7); + +for my $perm (permute(@numbers)) { + my ($a, $b, $c, $d, $e, $f, $g) = @$perm; + if (sum($a, $b) == sum($b, $c, $d) and sum($b, $c, $d) == sum($d, $e, $f) and sum($d, $e, $f) == sum($f, $g)) { + print "a = $a\nb = $b\nc = $c\nd = $d\ne = $e\nf = $f\ng = $g\n"; + last; + } +} + +sub permute { + my @items = @_; + return [] unless @items; + return map { [@items[$_], @$_] } 0..$#items for permute(@items[0..$_-1, $_+1..$#items]) for 0..$#items; +} + diff --git a/challenge-109/lubos-kolouch/python/ch-1.py b/challenge-109/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..0a72a5859e --- /dev/null +++ b/challenge-109/lubos-kolouch/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import math + + +def chowla(n): + sum = 0 + for i in range(2, math.isqrt(n) + 1): + if n % i == 0: + sum += i + if i != n//i: + sum += n//i + return sum + + +for n in range(1, 21): + print(chowla(n)) diff --git a/challenge-109/lubos-kolouch/python/ch-2.py b/challenge-109/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..7e545b4499 --- /dev/null +++ b/challenge-109/lubos-kolouch/python/ch-2.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from itertools import permutations + +numbers = [1, 2, 3, 4, 5, 6, 7] + +for perm in permutations(numbers): + a, b, c, d, e, f, g = perm + if sum([a, b]) == sum([b, c, d]) == sum([d, e, f]) == sum([f, g]): + print(f"a = {a}\nb = {b}\nc = {c}\nd = {d}\ne = {e}\nf = {f}\ng = {g}\n") + break -- cgit