diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-19 18:21:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-19 18:21:17 +0100 |
| commit | 70efddf4cea00f4ef6a8747cbd8a361f4ce23239 (patch) | |
| tree | 30a0cb7275caef38f866108c835b02c72c04545e | |
| parent | c2c685bba24efe61916cf11bebed48cdd16a524e (diff) | |
| parent | ff723a578fd036478961280814c7e427b8634394 (diff) | |
| download | perlweeklychallenge-club-70efddf4cea00f4ef6a8747cbd8a361f4ce23239.tar.gz perlweeklychallenge-club-70efddf4cea00f4ef6a8747cbd8a361f4ce23239.tar.bz2 perlweeklychallenge-club-70efddf4cea00f4ef6a8747cbd8a361f4ce23239.zip | |
Merge pull request #6287 from adamcrussell/challenge-169
initial commit
| -rw-r--r-- | challenge-169/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-169/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-169/adam-russell/java/ch-1.java | 44 | ||||
| -rw-r--r-- | challenge-169/adam-russell/java/ch-2.java | 54 | ||||
| -rw-r--r-- | challenge-169/adam-russell/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-169/adam-russell/perl/ch-2.pl | 48 | ||||
| -rw-r--r-- | challenge-169/adam-russell/prolog/ch-1.p | 40 | ||||
| -rw-r--r-- | challenge-169/adam-russell/prolog/ch-2.p | 51 |
8 files changed, 277 insertions, 0 deletions
diff --git a/challenge-169/adam-russell/blog.txt b/challenge-169/adam-russell/blog.txt new file mode 100644 index 0000000000..20b7acb63a --- /dev/null +++ b/challenge-169/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/06/19
\ No newline at end of file diff --git a/challenge-169/adam-russell/blog1.txt b/challenge-169/adam-russell/blog1.txt new file mode 100644 index 0000000000..5b466f5717 --- /dev/null +++ b/challenge-169/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/06/19
\ No newline at end of file diff --git a/challenge-169/adam-russell/java/ch-1.java b/challenge-169/adam-russell/java/ch-1.java new file mode 100644 index 0000000000..09cdd62612 --- /dev/null +++ b/challenge-169/adam-russell/java/ch-1.java @@ -0,0 +1,44 @@ +import java.util.ArrayList; + +class Brilliant{ + private static ArrayList primeFactors(long n){ + ArrayList factors = new ArrayList(); + while (n % 2 == 0){ + factors.add(new Long(2)); + n = n / 2; + } + for(long i = 3; i <= Math.sqrt(n); i = i + 2){ + while (n % i == 0){ + factors.add(new Long(i)); + n = n / i; + } + } + if(n > 2) + factors.add(new Long(n)); + return factors; + } + + private static boolean isBrilliant(int n){ + ArrayList factors = primeFactors(n); + return factors.size() == 2 && ((Long)factors.get(0)).toString().length() == ((Long)factors.get(1)).toString().length(); + } + + public static ArrayList nBrilliants(int n){ + ArrayList brilliants = new ArrayList(); + int i = 0; + do{ + i++; + if(isBrilliant(i)) + brilliants.add(new Integer(i)); + }while(brilliants.size() < n); + return brilliants; + } + + public static void main(String[] args){ + ArrayList brilliants = Brilliant.nBrilliants(20); + for(int i = 0; i < brilliants.size() - 1; i++){ + System.out.print(brilliants.get(i) + ", "); + } + System.out.println(brilliants.get(brilliants.size() - 1)); + } +}
\ No newline at end of file diff --git a/challenge-169/adam-russell/java/ch-2.java b/challenge-169/adam-russell/java/ch-2.java new file mode 100644 index 0000000000..47c8d7813e --- /dev/null +++ b/challenge-169/adam-russell/java/ch-2.java @@ -0,0 +1,54 @@ +import java.util.ArrayList; + +class Achilles{ + private static final double EPSILON = 1e-15; + private static ArrayList primeFactors(long n){ + ArrayList factors = new ArrayList(); + while (n % 2 == 0){ + factors.add(new Long(2)); + n = n / 2; + } + for(long i = 3; i <= Math.sqrt(n); i = i + 2){ + while (n % i == 0){ + factors.add(new Long(i)); + n = n / i; + } + } + if(n > 2) + factors.add(new Long(n)); + return factors; + } + + private static boolean isAchilles(int n){ + ArrayList factors = primeFactors(n); + for(int i = 0; i < factors.size(); i++){ + if(n % Math.pow(((Long)factors.get(i)).longValue() + 0.0, 2) != 0) + return false; + } + for(int i = 2; i <= Math.sqrt(n); i++) { + double d = Math.log(n) / Math.log(i); + if(Math.abs(d - Math.round(d)) < EPSILON) + return false; + } + return true; + } + + public static ArrayList nAchilles(int n){ + ArrayList achilles = new ArrayList(); + int i = 1; + do{ + i++; + if(isAchilles(i)) + achilles.add(new Integer(i)); + }while(achilles.size() < n); + return achilles; + } + + public static void main(String[] args){ + ArrayList achilles = Achilles.nAchilles(20); + for(int i = 0; i < achilles.size() - 1; i++){ + System.out.print(achilles.get(i) + ", "); + } + System.out.println(achilles.get(achilles.size() - 1)); + } +}
\ No newline at end of file diff --git a/challenge-169/adam-russell/perl/ch-1.pl b/challenge-169/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..4f7367e9d2 --- /dev/null +++ b/challenge-169/adam-russell/perl/ch-1.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; +## +# Write a script to generate the first 20 Brilliant Numbers. +## +sub prime_factor{ + my $x = shift(@_); + my @factors; + for (my $y = 2; $y <= $x; $y++){ + next if $x % $y; + $x /= $y; + push @factors, $y; + redo; + } + return @factors; +} + +sub is_brilliant{ + my($n) = @_; + my @factors = prime_factor($n); + return @factors == 2 && length($factors[0]) == length($factors[1]); +} + +sub n_brilliants{ + my($n) = @_; + my @brilliants; + my $i = 0; + { + push @brilliants, $i if is_brilliant($i); + $i++; + redo if @brilliants < $n; + } + return @brilliants; +} + +MAIN:{ + print join(", ", n_brilliants(20)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-169/adam-russell/perl/ch-2.pl b/challenge-169/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..d677bc6c27 --- /dev/null +++ b/challenge-169/adam-russell/perl/ch-2.pl @@ -0,0 +1,48 @@ +use strict; +use warnings; +## +# Write a script to generate the first 20 Achilles Numbers. +## +use POSIX; +use boolean; + +sub prime_factor{ + my $x = shift(@_); + my @factors; + for (my $y = 2; $y <= $x; $y++){ + next if $x % $y; + $x /= $y; + push @factors, $y; + redo; + } + return @factors; +} + +sub is_achilles{ + my($n) = @_; + my @factors = prime_factor($n); + for my $factor (@factors){ + return false if $n % ($factor * $factor) != 0; + } + for(my $i = 2; $i <= sqrt($n); $i++) { + my $d = log($n) / log($i) . ""; + return false if ceil($d) == floor($d); + } + return true; +} + +sub n_achilles{ + my($n) = @_; + my @achilles; + my $i = 1; + { + $i++; + push @achilles, $i if is_achilles($i); + redo if @achilles < $n; + } + return @achilles; +} + +MAIN:{ + print join(", ", n_achilles(20)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-169/adam-russell/prolog/ch-1.p b/challenge-169/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..e360089652 --- /dev/null +++ b/challenge-169/adam-russell/prolog/ch-1.p @@ -0,0 +1,40 @@ +prime_factors(N, L):- + N > 0, + prime_factors(N, L, 2). +prime_factors(1, [], _):- + !. +prime_factors(N, [F|L], F):- + R is N // F, + N =:= R * F, + !, + prime_factors(R, L, F). +prime_factors(N, L, F):- + next_factor(N, F, NF), + prime_factors(N, L, NF). +next_factor(_, 2, 3):- + !. +next_factor(N, F, NF):- + F * F < N, + !, + NF is F + 2. +next_factor(N, _, N). + +brilliants(_) --> []. +brilliants(Seen) --> [X], {brilliant(X), \+ member(X, Seen)}, brilliants([X|Seen]). + +brilliant(X):- + current_prolog_flag(max_integer, MAX_INTEGER), + between(1, MAX_INTEGER, X), + prime_factors(X, Factors), + length(Factors, 2), + nth(1, Factors, First), + nth(2, Factors, Second), + number_chars(First, FirstChars), + number_chars(Second, SecondChars), + length(FirstChars, FirstCharsLength), + length(SecondChars, SecondCharsLength), + FirstCharsLength == SecondCharsLength. + +n_brilliants(N, Brilliants):- + length(Brilliants, N), + phrase(brilliants([]), Brilliants).
\ No newline at end of file diff --git a/challenge-169/adam-russell/prolog/ch-2.p b/challenge-169/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..7d7a8585b1 --- /dev/null +++ b/challenge-169/adam-russell/prolog/ch-2.p @@ -0,0 +1,51 @@ +prime_factors(N, L):- + N > 0, + prime_factors(N, L, 2). +prime_factors(1, [], _):- + !. +prime_factors(N, [F|L], F):- + R is N // F, + N =:= R * F, + !, + prime_factors(R, L, F). +prime_factors(N, L, F):- + next_factor(N, F, NF), + prime_factors(N, L, NF). +next_factor(_, 2, 3):- + !. +next_factor(N, F, NF):- + F * F < N, + !, + NF is F + 2. +next_factor(N, _, N). + +powerful(N, X):- + M is mod(N, X * X), + M == 0. + +imperfect(N):- + Sqrt is round(sqrt(N)), + S is Sqrt - 1, + length(I, S), + fd_domain(I, 2, Sqrt), + fd_all_different(I), + fd_labeling(I),!, + maplist(imperfect(N), I). +imperfect(N, X):- + D is log(N) / log(X), + Check is abs(D - round(D)), + \+ Check < 0.000001. + +achilles(_) --> []. +achilles(Seen) --> [X], {current_prolog_flag(max_integer, MAX_INTEGER), + between(2, MAX_INTEGER, X), \+ member(X, Seen), achilles(X)}, + achilles([X|Seen]). + +achilles(X):- + prime_factors(X, Factors), + maplist(powerful(X), Factors), + imperfect(X). + +n_achilles(N, Achilles):- + length(Achilles, N), + phrase(achilles([]), Achilles).
\ No newline at end of file |
