aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-144/lubos-kolouch/perl/ch-1.pl27
-rw-r--r--challenge-144/lubos-kolouch/perl/ch-2.pl24
-rw-r--r--challenge-144/lubos-kolouch/python/ch-1.py27
-rw-r--r--challenge-144/lubos-kolouch/python/ch-2.py19
-rw-r--r--challenge-145/lubos-kolouch/perl/ch-1.pl15
-rw-r--r--challenge-145/lubos-kolouch/perl/ch-2.pl23
-rw-r--r--challenge-145/lubos-kolouch/python/ch-1.py11
-rw-r--r--challenge-145/lubos-kolouch/python/ch-2.py21
-rw-r--r--challenge-146/lubos-kolouch/perl/ch-1.pl17
-rw-r--r--challenge-146/lubos-kolouch/perl/ch-2.pl22
-rw-r--r--challenge-146/lubos-kolouch/python/ch-1.py15
-rw-r--r--challenge-146/lubos-kolouch/python/ch-2.py19
12 files changed, 240 insertions, 0 deletions
diff --git a/challenge-144/lubos-kolouch/perl/ch-1.pl b/challenge-144/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..9198eecce5
--- /dev/null
+++ b/challenge-144/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+sub is_prime {
+ my ($n) = @_;
+ return 0 if $n < 2;
+ for my $i ( 2 .. sqrt $n ) {
+ return 0 if $n % $i == 0;
+ }
+ return 1;
+}
+
+sub generate_semiprimes {
+ my ($n) = @_;
+ my @primes = grep { is_prime($_) } ( 2 .. $n );
+ my %semiprimes;
+
+ for my $i ( 0 .. $#primes ) {
+ for my $j ( $i .. $#primes ) {
+ my $semiprime = $primes[$i] * $primes[$j];
+ $semiprimes{$semiprime} = 1 if $semiprime <= $n;
+ }
+ }
+ return sort { $a <=> $b } keys %semiprimes;
+}
+
+print join ", ", generate_semiprimes(100);
diff --git a/challenge-144/lubos-kolouch/perl/ch-2.pl b/challenge-144/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..bbb813a1bf
--- /dev/null
+++ b/challenge-144/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+
+sub ulam_sequence {
+ my ( $u, $v, $n ) = @_;
+ $n //= 10;
+ my @sequence = ( $u, $v );
+ while ( @sequence < $n ) {
+ my %sums;
+ for my $i ( 0 .. $#sequence ) {
+ for my $j ( $i + 1 .. $#sequence ) {
+ $sums{ $sequence[$i] + $sequence[$j] }++;
+ }
+ }
+ my $next_val = ( sort { $a <=> $b }
+ grep { $sums{$_} == 1 and $_ > $sequence[-1] } keys %sums )[0];
+ push @sequence, $next_val;
+ }
+ return @sequence;
+}
+
+print join( ", ", ulam_sequence( 1, 2 ) ), "\n";
+print join( ", ", ulam_sequence( 2, 3 ) ), "\n";
+print join( ", ", ulam_sequence( 2, 5 ) ), "\n";
diff --git a/challenge-144/lubos-kolouch/python/ch-1.py b/challenge-144/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..96562b361f
--- /dev/null
+++ b/challenge-144/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def is_prime(n):
+ if n < 2:
+ return False
+ for i in range(2, int(n**0.5) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+
+def generate_semiprimes(n):
+ primes = [i for i in range(2, n + 1) if is_prime(i)]
+ semiprimes = set()
+
+ for i in range(len(primes)):
+ for j in range(i, len(primes)):
+ semiprime = primes[i] * primes[j]
+ if semiprime <= n:
+ semiprimes.add(semiprime)
+
+ return sorted(list(semiprimes))
+
+
+print(generate_semiprimes(100))
diff --git a/challenge-144/lubos-kolouch/python/ch-2.py b/challenge-144/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..a27d2b676e
--- /dev/null
+++ b/challenge-144/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,19 @@
+def ulam_sequence(u, v, n=10):
+ sequence = [u, v]
+ while len(sequence) < n:
+ sums = {
+ sequence[i] + sequence[j]: 0
+ for i in range(len(sequence))
+ for j in range(i + 1, len(sequence))
+ }
+ for i in range(len(sequence)):
+ for j in range(i + 1, len(sequence)):
+ sums[sequence[i] + sequence[j]] += 1
+ next_val = min(x for x in sums if sums[x] == 1 and x > sequence[-1])
+ sequence.append(next_val)
+ return sequence
+
+
+print(ulam_sequence(1, 2))
+print(ulam_sequence(2, 3))
+print(ulam_sequence(2, 5))
diff --git a/challenge-145/lubos-kolouch/perl/ch-1.pl b/challenge-145/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..927bca97b6
--- /dev/null
+++ b/challenge-145/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+
+sub dot_product {
+ my ( $a, $b ) = @_;
+ my $dot_product = 0;
+ for my $i ( 0 .. $#$a ) {
+ $dot_product += $a->[$i] * $b->[$i];
+ }
+ return $dot_product;
+}
+
+my @a = ( 1, 2, 3 );
+my @b = ( 4, 5, 6 );
+print dot_product( \@a, \@b ); # Output: 32
diff --git a/challenge-145/lubos-kolouch/perl/ch-2.pl b/challenge-145/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..a148ba5490
--- /dev/null
+++ b/challenge-145/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+sub palindromic_tree {
+ my ($s) = @_;
+ my @result;
+ for my $i ( 0 .. length($s) - 1 ) {
+ for my $j ( $i + 1 .. length($s) ) {
+ my $substring = substr $s, $i, $j - $i;
+ if ( $substring eq reverse $substring ) {
+ push @result, $substring;
+ }
+ }
+ }
+ return \@result;
+}
+
+my $s = 'redivider';
+print join( " ", @{ palindromic_tree($s) } ),
+ "\n"; # Output: r redivider e edivide d divid i ivi v
+
+$s = 'deific';
+print join( " ", @{ palindromic_tree($s) } ), "\n"; # Output: d e i ifi f c
diff --git a/challenge-145/lubos-kolouch/python/ch-1.py b/challenge-145/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..bcd2e57de6
--- /dev/null
+++ b/challenge-145/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def dot_product(a, b):
+ return sum(x * y for x, y in zip(a, b))
+
+
+a = [1, 2, 3]
+b = [4, 5, 6]
+print(dot_product(a, b)) # Output: 32
diff --git a/challenge-145/lubos-kolouch/python/ch-2.py b/challenge-145/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7a9584b223
--- /dev/null
+++ b/challenge-145/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def palindromic_tree(s):
+ result = []
+ for i in range(len(s)):
+ for j in range(i + 1, len(s) + 1):
+ substring = s[i:j]
+ if substring == substring[::-1]:
+ result.append(substring)
+ return result
+
+
+s = "redivider"
+print(
+ palindromic_tree(s)
+) # Output: ['r', 'redivider', 'e', 'edivide', 'd', 'divid', 'i', 'ivi', 'v']
+
+s = "deific"
+print(palindromic_tree(s)) # Output: ['d', 'e', 'i', 'ifi', 'f', 'c']
diff --git a/challenge-146/lubos-kolouch/perl/ch-1.pl b/challenge-146/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..c14852dffa
--- /dev/null
+++ b/challenge-146/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+sub nth_prime {
+ my ($n) = @_;
+ my @primes = (2);
+ my $i = 3;
+ while ( @primes < $n ) {
+ if ( !grep { $i % $_ == 0 } @primes ) {
+ push @primes, $i;
+ }
+ $i += 2;
+ }
+ return $primes[-1];
+}
+
+print nth_prime(10001), "\n"; # Output: 104743
diff --git a/challenge-146/lubos-kolouch/perl/ch-2.pl b/challenge-146/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..3537ebcfc9
--- /dev/null
+++ b/challenge-146/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+use Math::Fraction;
+
+sub find_ancestors {
+ my ($fraction) = @_;
+ my ( $num, $denom ) = split( '/', $fraction );
+ my $parent =
+ $denom > $num
+ ? Math::Fraction->new( $num, $denom - $num )
+ : Math::Fraction->new( $num - $denom, $denom );
+ my $grandparent =
+ $parent->denominator > $parent->numerator
+ ? Math::Fraction->new( $parent->numerator,
+ $parent->denominator - $parent->numerator )
+ : Math::Fraction->new( $parent->numerator - $parent->denominator,
+ $parent->denominator );
+ return $parent->to_string, $grandparent->to_string;
+}
+
+print join( ' and ', find_ancestors('3/5') ), "\n"; # Output: 3/2 and 1/2
+print join( ' and ', find_ancestors('4/3') ), "\n"; # Output: 1/3 and 1/2
diff --git a/challenge-146/lubos-kolouch/python/ch-1.py b/challenge-146/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..3191b3e2e4
--- /dev/null
+++ b/challenge-146/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def nth_prime(n):
+ primes = [2]
+ i = 3
+ while len(primes) < n:
+ if all(i % p > 0 for p in primes):
+ primes.append(i)
+ i += 2
+ return primes[-1]
+
+
+print(nth_prime(10001)) # Output: 104743
diff --git a/challenge-146/lubos-kolouch/python/ch-2.py b/challenge-146/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..0cfdb31445
--- /dev/null
+++ b/challenge-146/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from fractions import Fraction
+
+
+def find_ancestors(fraction):
+ num, denom = map(int, fraction.split("/"))
+ parent = Fraction(num, denom - num) if denom > num else Fraction(num - denom, denom)
+ grandparent = (
+ Fraction(parent.numerator, parent.denominator - parent.numerator)
+ if parent.denominator > parent.numerator
+ else Fraction(parent.numerator - parent.denominator, parent.denominator)
+ )
+ return str(parent), str(grandparent)
+
+
+print(find_ancestors("3/5")) # Output: ('3/2', '1/2')
+print(find_ancestors("4/3")) # Output: ('1/3', '1/2')