aboutsummaryrefslogtreecommitdiff
path: root/challenge-085
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-08 21:30:21 +0000
committerGitHub <noreply@github.com>2020-11-08 21:30:21 +0000
commit3b241b5fc35a18f4fd957b41fe99e9082cd23491 (patch)
tree76f7589e4fa0dfdf87bc9c4058a14051b9865928 /challenge-085
parent22643524dce4958380bdec1bece1619845159149 (diff)
parent1e7c28885c7b3cac05d54a1b526f78894fa368f3 (diff)
downloadperlweeklychallenge-club-3b241b5fc35a18f4fd957b41fe99e9082cd23491.tar.gz
perlweeklychallenge-club-3b241b5fc35a18f4fd957b41fe99e9082cd23491.tar.bz2
perlweeklychallenge-club-3b241b5fc35a18f4fd957b41fe99e9082cd23491.zip
Merge pull request #2727 from adamcrussell/challenge-085
Challenge 085
Diffstat (limited to 'challenge-085')
-rw-r--r--challenge-085/adam-russell/blog.txt1
-rw-r--r--challenge-085/adam-russell/perl/ch-1.pl44
-rw-r--r--challenge-085/adam-russell/perl/ch-2.pl28
-rw-r--r--challenge-085/adam-russell/prolog/ch-1.p33
-rw-r--r--challenge-085/adam-russell/prolog/ch-2.p34
5 files changed, 140 insertions, 0 deletions
diff --git a/challenge-085/adam-russell/blog.txt b/challenge-085/adam-russell/blog.txt
new file mode 100644
index 0000000000..1744dc1029
--- /dev/null
+++ b/challenge-085/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/2020/11/08#pwc085
diff --git a/challenge-085/adam-russell/perl/ch-1.pl b/challenge-085/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..87d57d24a5
--- /dev/null
+++ b/challenge-085/adam-russell/perl/ch-1.pl
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+##
+# You are given an array of real numbers greater than zero.
+# Write a script to find if there exists a triplet (a,b,c)
+# such that 1 < a+b+c < 2. Print 1 if you succeed otherwise 0.
+##
+use boolean;
+use Math::Combinatorics;
+
+sub build_constraints{
+ my @constraints;
+ my $a_not_equal_b = sub { $_[0] != $_[1] };
+ my $a_not_equal_c = sub { $_[0] != $_[2] };
+ my $b_not_equal_c = sub { $_[1] != $_[2] };
+ my $sum_greater_than_1 = sub { 1 < ($_[0] + $_[1] + $_[2]) };
+ my $sum_less_than_2 = sub { 2 > ($_[0] + $_[1] + $_[2]) };
+ return (
+ $a_not_equal_b,
+ $a_not_equal_c,
+ $b_not_equal_c,
+ $sum_greater_than_1,
+ $sum_less_than_2
+ );
+}
+
+MAIN:{
+ my $combinations = Math::Combinatorics->new(
+ count => 3,
+ data => [@ARGV],
+ );
+ my $found;
+ while(my @combination = $combinations->next_combination()){
+ $found = true;
+ for my $constraint (build_constraints()){
+ if(!$constraint->(@combination)){
+ $found = false;
+ last;
+ }
+ }
+ do{ print "1\n"; last; } if($found);
+ }
+ print "0\n" if(!$found);
+} \ No newline at end of file
diff --git a/challenge-085/adam-russell/perl/ch-2.pl b/challenge-085/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..bebabe1ec2
--- /dev/null
+++ b/challenge-085/adam-russell/perl/ch-2.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+##
+# You are given a positive integer $N.
+# Write a script to find if it can be expressed
+# as a ^ b where a > 0 and b > 1.
+# Print 1 if you succeed otherwise 0.
+##
+use boolean;
+
+sub log_a{
+ my($a, $n) = @_;
+ return log($n)/log($a);
+}
+
+MAIN:{
+ my $N = $ARGV[0];
+ my $found = false;
+ for my $a (2 .. $N){
+ my $b = log_a($a, $N);
+ if($b =~ /^[-]?\d+$/ && $b > 1){
+ print "1\n";
+ $found = true;
+ last;
+ }
+ }
+ print "0\n" if(!$found);
+} \ No newline at end of file
diff --git a/challenge-085/adam-russell/prolog/ch-1.p b/challenge-085/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..c861b9056f
--- /dev/null
+++ b/challenge-085/adam-russell/prolog/ch-1.p
@@ -0,0 +1,33 @@
+:- use_module(library(optparse)).
+/*
+ You are given an array of real numbers greater than zero.
+ Write a script to find if there exists a triplet (a,b,c)
+ such that 1 < a+b+c < 2. Print 1 if you succeed otherwise 0.
+*/
+opts_spec(
+ [
+ [opt(numbers),
+ default([1.2, 0.4, 0.1, 2.5]),
+ longflags([numbers])]
+ ]).
+
+ch_1(L):-
+ member(A, L),
+ member(B, L),
+ member(C, L),
+ A =\= B,
+ B =\= C,
+ A =\= C,
+ D is A + B + C,
+ D > 1,
+ D < 2,
+ writeln(1).
+
+ch_1(_):-
+ writeln(0).
+
+main:-
+ opts_spec(OptsSpec),
+ opt_arguments(OptsSpec, [numbers(N)], _AdditionalArguments),
+ ch_1(N),
+ halt. \ No newline at end of file
diff --git a/challenge-085/adam-russell/prolog/ch-2.p b/challenge-085/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..4da87c5624
--- /dev/null
+++ b/challenge-085/adam-russell/prolog/ch-2.p
@@ -0,0 +1,34 @@
+:- use_module(library(clpfd)).
+:- use_module(library(optparse)).
+/*
+ You are given a positive integer $N.
+ Write a script to find if it can be expressed
+ as a ^ b where a > 0 and b > 1.
+ Print 1 if you succeed otherwise 0.
+*/
+opts_spec(
+ [
+ [opt(number),
+ default(0),
+ longflags([number])]
+ ]).
+
+/*
+ Ok, I'll admit, this is a pretty silly use of clpfd when
+ a simple logarithm calculation would do the job! Still clpfd
+ is more fun.
+*/
+ch_2(N) :-
+ A in 0 .. N,
+ B in 1 .. N,
+ N #= A ^ B,
+ label([A,B]),
+ writeln(1).
+ch_2(_) :-
+ writeln(0).
+
+main:-
+ opts_spec(OptsSpec),
+ opt_arguments(OptsSpec, [number(N)], _AdditionalArguments),
+ ch_2(N),
+ halt. \ No newline at end of file