diff options
| author | Adam Russell <ac.russell@live.com> | 2022-02-06 17:37:35 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2022-02-06 17:37:35 -0500 |
| commit | 4e01c908099d4395d818e45cd32aa3e8085a772f (patch) | |
| tree | 0bfc27f2eaa7fe3bbaa66e1743d7a44ac5bb5541 | |
| parent | ebcd59a50056e1da3a3f28c98e867c2344548ac6 (diff) | |
| download | perlweeklychallenge-club-4e01c908099d4395d818e45cd32aa3e8085a772f.tar.gz perlweeklychallenge-club-4e01c908099d4395d818e45cd32aa3e8085a772f.tar.bz2 perlweeklychallenge-club-4e01c908099d4395d818e45cd32aa3e8085a772f.zip | |
initial commit
| -rw-r--r-- | challenge-150/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-150/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-150/adam-russell/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-150/adam-russell/perl/ch-2.pl | 34 | ||||
| -rw-r--r-- | challenge-150/adam-russell/prolog/ch-1.p | 8 | ||||
| -rw-r--r-- | challenge-150/adam-russell/prolog/ch-2.p | 31 |
6 files changed, 101 insertions, 0 deletions
diff --git a/challenge-150/adam-russell/blog.txt b/challenge-150/adam-russell/blog.txt new file mode 100644 index 0000000000..3e995cac42 --- /dev/null +++ b/challenge-150/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/02/06 diff --git a/challenge-150/adam-russell/blog1.txt b/challenge-150/adam-russell/blog1.txt new file mode 100644 index 0000000000..e33f0d21ff --- /dev/null +++ b/challenge-150/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/02/06 diff --git a/challenge-150/adam-russell/perl/ch-1.pl b/challenge-150/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..affb0bb1de --- /dev/null +++ b/challenge-150/adam-russell/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +## +# You are given two strings having same number of digits, $a and $b. +# Write a script to generate Fibonacci Words by concatenation of +# the previous two strings. Print the 51st digit of the first term +# having at least 51 digits. +## + +sub _fibonacci_words_51{ + my($accumulated) = @_; + my $i = @{$accumulated} - 1; + my $next = $accumulated->[$i - 1] . $accumulated->[$i]; + return substr($next, 51 - 1, 1) if length($next) >= 51; + push @{$accumulated}, $next; + _fibonacci_words_51($accumulated); +} + +sub fibonacci_words{ + my($u, $v) = @_; + return _fibonacci_words_51([$u, $v]); +} + +MAIN:{ + print fibonacci_words(q[1234], q[5678]) . "\n"; +}
\ No newline at end of file diff --git a/challenge-150/adam-russell/perl/ch-2.pl b/challenge-150/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..0d8892be10 --- /dev/null +++ b/challenge-150/adam-russell/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +## +# Write a script to generate all square-free integers <= 500. +## +use constant LIMIT => 500; + +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 square_free{ + my @square_free; + for my $x (1 .. LIMIT){ + my @factors = prime_factor($x); + my @a; + map {$a[$_]++} @factors; + @a = grep {$_ && $_ > 1} @a; + push @square_free, $x if !@a; + } + return @square_free; +} + +main:{ + print join(", ", square_free()) . "\n"; +} diff --git a/challenge-150/adam-russell/prolog/ch-1.p b/challenge-150/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..1be9ebb36b --- /dev/null +++ b/challenge-150/adam-russell/prolog/ch-1.p @@ -0,0 +1,8 @@ +fibonacci_words(Size, A, B) --> {atom_concat(A, B, C), atom_chars(C, Chars), length(Chars, N), N < Size}, [A], fibonacci_words(Size, B, C). +fibonacci_words(Size, A, B) --> {atom_concat(A, B, C), atom_chars(C, Chars), length(Chars, N), N >= Size}, [C]. + +fibonacci_words_nth_character(A, B, N, NthChar) :- + phrase(fibonacci_words(N, A, B), FibonacciWords), + last(FibonacciWords, LongestTerm), + atom_chars(LongestTerm, LongestTermChars), + nth(N, LongestTermChars, NthChar).
\ No newline at end of file diff --git a/challenge-150/adam-russell/prolog/ch-2.p b/challenge-150/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..4a7f900bf2 --- /dev/null +++ b/challenge-150/adam-russell/prolog/ch-2.p @@ -0,0 +1,31 @@ +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). + +square_free(N, SquareFree):- + findall(X, + (between(1, N, X), + prime_factors(X, PrimeFactors), + sort(PrimeFactors, PrimeFactorsSorted), + msort(PrimeFactors, PrimeFactorsMSorted), + length(PrimeFactorsSorted, SortedLength), + length(PrimeFactorsMSorted, MSortedLength), + SortedLength == MSortedLength), SquareFree). +
\ No newline at end of file |
