diff options
| -rw-r--r-- | challenge-171/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-171/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-171/adam-russell/java/ch-1.java | 37 | ||||
| -rw-r--r-- | challenge-171/adam-russell/java/ch-2.java | 82 | ||||
| -rw-r--r-- | challenge-171/adam-russell/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-171/adam-russell/perl/ch-2.pl | 28 | ||||
| -rw-r--r-- | challenge-171/adam-russell/prolog/ch-1.p | 24 | ||||
| -rw-r--r-- | challenge-171/adam-russell/prolog/ch-2.p | 9 |
8 files changed, 211 insertions, 0 deletions
diff --git a/challenge-171/adam-russell/blog.txt b/challenge-171/adam-russell/blog.txt new file mode 100644 index 0000000000..4d16c3f2c1 --- /dev/null +++ b/challenge-171/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/07/03
\ No newline at end of file diff --git a/challenge-171/adam-russell/blog1.txt b/challenge-171/adam-russell/blog1.txt new file mode 100644 index 0000000000..17e83c0678 --- /dev/null +++ b/challenge-171/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/07/03
\ No newline at end of file diff --git a/challenge-171/adam-russell/java/ch-1.java b/challenge-171/adam-russell/java/ch-1.java new file mode 100644 index 0000000000..fa9739d927 --- /dev/null +++ b/challenge-171/adam-russell/java/ch-1.java @@ -0,0 +1,37 @@ +import java.util.ArrayList; + +class Abundant{ + private static ArrayList properDivisors(int n){ + ArrayList divisors = new ArrayList(); + for(int i = 1; i < n / 2; i++){ + if(n % i == 0){ + divisors.add(new Integer(i)); + } + } + return divisors; + } + + public static ArrayList nAbundantsOdd(int n){ + ArrayList abundants = new ArrayList(); + int x = 1; + do{ + x++; + if(x % 2 == 1){ + ArrayList divisors = properDivisors(x); + int sum = 0; + for(int i = 0; i < divisors.size(); i++) + sum += ((Integer)divisors.get(i)).intValue(); + if(sum > x) + abundants.add(new Integer(x)); + } + }while(abundants.size() < n); + return abundants; + } + + public static void main(String[] args){ + ArrayList abundants = Abundant.nAbundantsOdd(20); + for(int i = 0; i < abundants.size() - 1; i++) + System.out.print((Integer)abundants.get(i) + ", "); + System.out.println((Integer)abundants.get(abundants.size() - 1)); + } +}
\ No newline at end of file diff --git a/challenge-171/adam-russell/java/ch-2.java b/challenge-171/adam-russell/java/ch-2.java new file mode 100644 index 0000000000..5fb543747e --- /dev/null +++ b/challenge-171/adam-russell/java/ch-2.java @@ -0,0 +1,82 @@ +import java.lang.reflect.Method; + +class Compose{ + + private Method f, g; + + public void setF(Method _f){ + this.f = _f; + } + + public void setG(Method _g){ + this.g = _g; + } + + public int f(int x){ + return x + x; + } + + public int g(int x){ + return x * x; + } + + public int h(int x){ + try{ + return ((Integer) this.f.invoke(this, new Object[]{this.g.invoke(this, new Object[]{new Integer(x)})})).intValue(); + } + catch(java.lang.reflect.InvocationTargetException ite){ + exceptionMessageInvoke(ite); + } + catch(IllegalArgumentException iae){ + exceptionMessageInvoke(iae); + } + catch(IllegalAccessException iae){ + exceptionMessageInvoke(iae); + } + return -1; + } + + public Method compose(Method f, Method g){ + setF(f); + setG(g); + Method h = null; + try{ + h = Compose.class.getMethod("h", new Class[]{int.class}); + } + catch(NoSuchMethodException e){ + e.printStackTrace(); + } + return h; + } + + public static void exceptionMessageInvoke(Exception e){ + System.out.println("Error executing h"); + e.printStackTrace(); + } + + public static void main(String[] args){ + Compose composer = new Compose(); + try{ + Method f = Compose.class.getMethod("f", new Class[]{int.class}); + Method g = Compose.class.getMethod("g", new Class[]{int.class}); + Method h = composer.compose(f, g); + try{ + int x = ((Integer)h.invoke(composer, new Object[]{new Integer(7)})).intValue(); + System.out.println(x); + } + catch(java.lang.reflect.InvocationTargetException ite){ + exceptionMessageInvoke(ite); + } + catch(IllegalArgumentException iae){ + exceptionMessageInvoke(iae); + } + catch(IllegalAccessException iae){ + exceptionMessageInvoke(iae); + } + } + catch(NoSuchMethodException nsme){ + System.out.println("Method not found."); + System.exit(1); + } + } +}
\ No newline at end of file diff --git a/challenge-171/adam-russell/perl/ch-1.pl b/challenge-171/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..43ecb13b1c --- /dev/null +++ b/challenge-171/adam-russell/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +## +# Write a script to generate the first twenty Abundant Odd Numbers. +## +sub proper_divisors{ + my($n) = @_; + my @divisors; + for my $x (1 .. $n / 2){ + push @divisors, $x if $n % $x == 0; + } + return @divisors; +} + +sub n_abundant_odd{ + my($n) = @_; + my $x = 0; + my @odd_abundants; + { + push @odd_abundants, $x if $x % 2 == 1 && unpack("%32I*", pack("I*", proper_divisors($x))) > $x; + $x++; + redo if @odd_abundants < $n; + } + return @odd_abundants; +} + +MAIN:{ + print join(", ", n_abundant_odd(20)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-171/adam-russell/perl/ch-2.pl b/challenge-171/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..addda3a281 --- /dev/null +++ b/challenge-171/adam-russell/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +## +# Create sub compose($f, $g) which takes in two parameters $f and $g as subroutine refs +# and returns subroutine ref i.e. compose($f, $g)->($x) = $f->($g->($x)). +## +sub f{ + my($x) = @_; + return $x + $x; +} + +sub g{ + my($x) = @_; + return $x * $x; +} + +sub compose{ + my($f, $g) = @_; + return sub{ + my($x) = @_; + return $f->($g->($x)); + }; +} + +MAIN:{ + my $h = compose(\&f, \&g); + print $h->(7) . "\n"; +}
\ No newline at end of file diff --git a/challenge-171/adam-russell/prolog/ch-1.p b/challenge-171/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..f2946617a1 --- /dev/null +++ b/challenge-171/adam-russell/prolog/ch-1.p @@ -0,0 +1,24 @@ +proper_divisors(X, Divisors):- + Half is X // 2, + findall(Divisor,( + between(1, Half, Divisor), + M is mod(X, Divisor), + M == 0 + ), Divisors). + +abundants_odd(_) --> []. +abundants_odd(Previous) --> [X], {abundant_odd(Previous, X)}, abundants_odd(X). + +abundant_odd(Previous, X):- + current_prolog_flag(max_integer, MAX_INTEGER), + between(Previous, MAX_INTEGER, X), + X > Previous, + M is mod(X, 2), + M == 1, + proper_divisors(X, Divisors), + sum_list(Divisors, DivisorsSum), + DivisorsSum > X. + +n_abundants(N, Abundants):- + length(Abundants, N), + phrase(abundants_odd(-1), Abundants).
\ No newline at end of file diff --git a/challenge-171/adam-russell/prolog/ch-2.p b/challenge-171/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..0332606c51 --- /dev/null +++ b/challenge-171/adam-russell/prolog/ch-2.p @@ -0,0 +1,9 @@ +f(S, T):- + T is S + S. + +g(S, T):- + T is S * S. + +compose(F, G, H):- + asserta((h(X, Y) :- call(G, X, X0), call(F, X0, Y))), + H = h.
\ No newline at end of file |
