diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-26 14:29:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-26 14:29:22 +0100 |
| commit | 863628b11635cda8d3e5cafc22c5ba9b7ca3c98d (patch) | |
| tree | 8b5fdf74c66bbe718f1fc6f1e73987be7eff6cc0 | |
| parent | 26044569d4cf46f54f8967c403ee832c4e5b645e (diff) | |
| parent | 0561f06314e878c5d8c02ffbba8876183d43ad76 (diff) | |
| download | perlweeklychallenge-club-863628b11635cda8d3e5cafc22c5ba9b7ca3c98d.tar.gz perlweeklychallenge-club-863628b11635cda8d3e5cafc22c5ba9b7ca3c98d.tar.bz2 perlweeklychallenge-club-863628b11635cda8d3e5cafc22c5ba9b7ca3c98d.zip | |
Merge pull request #6342 from adamcrussell/challenge-170
initial commit
| -rw-r--r-- | challenge-170/adam-russell/java/ch-1.java | 54 | ||||
| -rw-r--r-- | challenge-170/adam-russell/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-170/adam-russell/prolog/ch-1.p | 24 |
3 files changed, 121 insertions, 0 deletions
diff --git a/challenge-170/adam-russell/java/ch-1.java b/challenge-170/adam-russell/java/ch-1.java new file mode 100644 index 0000000000..1797fec5b2 --- /dev/null +++ b/challenge-170/adam-russell/java/ch-1.java @@ -0,0 +1,54 @@ +import java.util.List; +import java.util.ArrayList; + +class Primorial{ + 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 ArrayList nPrimes(int n){ + ArrayList primes = new ArrayList(); + primes.add(new Integer(1)); + if(n == 0) + return primes; + int i = 1; + do{ + i++; + if(primeFactors(i).size() == 1) + primes.add(new Integer(i)); + }while(primes.size() < n); + return primes; + } + + public static ArrayList nPrimorials(int n){ + ArrayList primes = nPrimes(n); + ArrayList primorials = new ArrayList(); + for(int i = 0; i < n; i++){ + long primorial = 1; + List l = primes.subList(0, i + 1); + for(int j = 0; j < l.size(); j++) + primorial *= ((Integer)l.get(j)).intValue(); + primorials.add(new Long(primorial)); + } + return primorials; + } + + public static void main(String[] args){ + ArrayList primorials = Primorial.nPrimorials(10); + for(int i = 0; i < primorials.size(); i++) + System.out.println("P(" + i + ") = " + primorials.get(i)); + } +}
\ No newline at end of file diff --git a/challenge-170/adam-russell/perl/ch-1.pl b/challenge-170/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..803e99e372 --- /dev/null +++ b/challenge-170/adam-russell/perl/ch-1.pl @@ -0,0 +1,43 @@ +use strict; +use warnings; +## +# Write a script to generate first 10 Primorial Numbers. +## +use boolean; +use constant N => 10; +use Math::Primality qw/is_prime/; + +sub n_primes{ + my($n) = @_; + my @primes = (1); + return @primes if $n == 0; + my $x = 2; + { + push @primes, $x if is_prime($x); + $x++; + redo if @primes < $n; + } + return @primes; +} + +sub n_primorials{ + my($n) = @_; + my $x = 0; + my @primes = n_primes($n); + my @primorials; + { + my $primorial = 1; + map {$primorial *= $_} @primes[0 .. $x]; + push @primorials, $primorial; + $x++; + redo if $x < $n; + } + return @primorials; +} + +MAIN:{ + my @primorials = n_primorials(N); + for(my $i = 0; $i < @primorials; $i++){ + print "P($i) = $primorials[$i]\n"; + } +}
\ No newline at end of file diff --git a/challenge-170/adam-russell/prolog/ch-1.p b/challenge-170/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..1e267da859 --- /dev/null +++ b/challenge-170/adam-russell/prolog/ch-1.p @@ -0,0 +1,24 @@ +primes(_) --> []. +primes(Seen) --> [X], {current_prolog_flag(max_integer, MAX_INTEGER), + between(2, MAX_INTEGER, X), + fd_prime(X), + \+ member(X, Seen)}, + primes([X|Seen]). + +product_list([], 1). +product_list([H|T], Product):- + product_list(T, P), + Product is H * P. + +primorial(N, Primorial):- + length(Primes, N), + phrase(primes([]), Primes), + product_list(Primes, Primorial), !. + +primorials(_) --> []. +primorials(Seen) --> [Primorial], + {current_prolog_flag(max_integer, MAX_INTEGER), + between(0, MAX_INTEGER, X), + primorial(X, Primorial), + \+ member(X, Seen)}, + primorials([X|Seen]).
\ No newline at end of file |
