aboutsummaryrefslogtreecommitdiff
path: root/challenge-012
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-16 19:22:04 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-16 19:22:04 +0100
commit4e2e50d29d3dfdcc15613ab3259f206681304ed1 (patch)
treecff26fa889d24938258ec7342809819b8c229564 /challenge-012
parentf8fccce87c49ef9d9e5da19d23e4c8549ff024c9 (diff)
downloadperlweeklychallenge-club-4e2e50d29d3dfdcc15613ab3259f206681304ed1.tar.gz
perlweeklychallenge-club-4e2e50d29d3dfdcc15613ab3259f206681304ed1.tar.bz2
perlweeklychallenge-club-4e2e50d29d3dfdcc15613ab3259f206681304ed1.zip
Challenge 010 011 LK Perl Python
Diffstat (limited to 'challenge-012')
-rw-r--r--challenge-012/lubos-kolouch/perl/ch-1.pl25
-rw-r--r--challenge-012/lubos-kolouch/perl/ch-2.pl23
-rw-r--r--challenge-012/lubos-kolouch/python/ch-1.py25
-rw-r--r--challenge-012/lubos-kolouch/python/ch-2.py18
4 files changed, 91 insertions, 0 deletions
diff --git a/challenge-012/lubos-kolouch/perl/ch-1.pl b/challenge-012/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..5fd044a666
--- /dev/null
+++ b/challenge-012/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub is_prime {
+ my $n = shift;
+ return 0 if $n < 2;
+ for ( my $i = 2 ; $i <= sqrt($n) ; $i++ ) {
+ return 0 if ( $n % $i == 0 );
+ }
+ return 1;
+}
+
+my $p = 2;
+my $euclid_number = 3;
+
+while ( is_prime($euclid_number) ) {
+ $p++;
+ while ( !is_prime($p) ) {
+ $p++;
+ }
+ $euclid_number = ( 2**( $p - 1 ) ) * ( ( 2**$p ) - 1 );
+}
+
+print "The smallest Euclid number that is not prime is: ", $euclid_number, "\n";
diff --git a/challenge-012/lubos-kolouch/perl/ch-2.pl b/challenge-012/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..af21886dc0
--- /dev/null
+++ b/challenge-012/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+my @paths = ( "/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e" );
+my $separator = "/";
+
+my @common_path;
+for my $path (@paths) {
+ my @parts = split( $separator, $path );
+ if ( !@common_path ) {
+ @common_path = @parts;
+ }
+ else {
+ for ( my $i = 0 ; $i < @common_path ; $i++ ) {
+ if ( $common_path[$i] ne $parts[$i] ) {
+ splice( @common_path, $i );
+ last;
+ }
+ }
+ }
+}
+
+print "The common directory path is: ", join( $separator, @common_path ), "\n";
diff --git a/challenge-012/lubos-kolouch/python/ch-1.py b/challenge-012/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d7f2eb96f4
--- /dev/null
+++ b/challenge-012/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from math import sqrt
+
+
+def is_prime(n):
+ if n < 2:
+ return False
+ for i in range(2, int(sqrt(n)) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+
+p = 2
+euclid_number = 3
+
+while is_prime(euclid_number):
+ p += 1
+ while not is_prime(p):
+ p += 1
+ euclid_number = (2 ** (p - 1)) * ((2**p) - 1)
+
+print(f"The smallest Euclid number that is not prime is: {euclid_number}")
diff --git a/challenge-012/lubos-kolouch/python/ch-2.py b/challenge-012/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..43788cde70
--- /dev/null
+++ b/challenge-012/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+paths = ["/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e"]
+separator = "/"
+
+common_path = []
+for path in paths:
+ parts = path.split(separator)
+ if not common_path:
+ common_path = parts
+ else:
+ for i in range(len(common_path)):
+ if common_path[i] != parts[i]:
+ common_path = common_path[:i]
+ break
+
+print(f"The common directory path is: {separator.join(common_path)}")