diff options
| author | Adam Russell <ac.russell@live.com> | 2021-05-30 10:44:47 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2021-05-30 10:44:47 -0400 |
| commit | 0c8a40ef2ed36d863c9e0002d0d061b1254d1995 (patch) | |
| tree | e90039c47668ab4884dc22bb778ce8709e5ec31f /challenge-114 | |
| parent | b4f2c135093c3d380c25c426b66b54e1ec908f32 (diff) | |
| download | perlweeklychallenge-club-0c8a40ef2ed36d863c9e0002d0d061b1254d1995.tar.gz perlweeklychallenge-club-0c8a40ef2ed36d863c9e0002d0d061b1254d1995.tar.bz2 perlweeklychallenge-club-0c8a40ef2ed36d863c9e0002d0d061b1254d1995.zip | |
initial commit
Diffstat (limited to 'challenge-114')
| -rw-r--r-- | challenge-114/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-114/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-114/adam-russell/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-114/adam-russell/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-114/adam-russell/prolog/ch-1.p | 18 | ||||
| -rw-r--r-- | challenge-114/adam-russell/prolog/ch-2.p | 26 |
6 files changed, 105 insertions, 0 deletions
diff --git a/challenge-114/adam-russell/blog.txt b/challenge-114/adam-russell/blog.txt new file mode 100644 index 0000000000..7f0aa0ec07 --- /dev/null +++ b/challenge-114/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/05/29 diff --git a/challenge-114/adam-russell/blog1.txt b/challenge-114/adam-russell/blog1.txt new file mode 100644 index 0000000000..b58751fb9b --- /dev/null +++ b/challenge-114/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/05/29 diff --git a/challenge-114/adam-russell/perl/ch-1.pl b/challenge-114/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..b087b883f5 --- /dev/null +++ b/challenge-114/adam-russell/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +## +# You are given a positive integer $N. +# Write a script to find out the next Palindrome Number +# higher than the given integer $N. +## +sub next_palindrome{ + my($n) = @_; + { + $n++; + return $n if $n eq join("", reverse(split(//, $n))); + redo; + } +} + +MAIN:{ + my($N); + $N = 1234; + print next_palindrome($N) . "\n"; + $N = 999; + print next_palindrome($N) . "\n"; +} diff --git a/challenge-114/adam-russell/perl/ch-2.pl b/challenge-114/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..0104194db1 --- /dev/null +++ b/challenge-114/adam-russell/perl/ch-2.pl @@ -0,0 +1,36 @@ +use strict; +use warnings; +## +# You are given a positive integer $N. +# Write a script to find the next higher integer +# having the same number of 1 bits in binary representation as $N. +## +sub count_bits{ + my($n) = @_; + my $total_count_set_bit = 0; + while($n){ + my $b = $n & 1; + $total_count_set_bit++ if $b; + $n = $n >> 1; + } + return $total_count_set_bit; +} + +sub next_same_bits{ + my($n) = @_; + my $number_bits = count_bits($n); + { + my $next = $n + 1; + return $next if count_bits($next) == $number_bits; + $n = $next; + redo; + } +} + +MAIN:{ + my($N); + $N = 3; + print next_same_bits($N) . "\n"; + $N = 12; + print next_same_bits($N) . "\n"; +} diff --git a/challenge-114/adam-russell/prolog/ch-1.p b/challenge-114/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..7c17436039 --- /dev/null +++ b/challenge-114/adam-russell/prolog/ch-1.p @@ -0,0 +1,18 @@ +:-initialization(main). + +next_palindrome(N, NextPalindrome):- + current_prolog_flag(max_integer, MAX_INTEGER), + N0 is N + 1, + between(N0, MAX_INTEGER, X), + number_chars(X, C), + reverse(C, R), + number_chars(NR, R), + NR == X, + NextPalindrome = NR. + +main:- + next_palindrome(1234, NextPalindrome0), + write(NextPalindrome0), nl, + next_palindrome(999, NextPalindrome1), + write(NextPalindrome1), nl, + halt. diff --git a/challenge-114/adam-russell/prolog/ch-2.p b/challenge-114/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..dd8a3700e2 --- /dev/null +++ b/challenge-114/adam-russell/prolog/ch-2.p @@ -0,0 +1,26 @@ +:-initialization(main). + +set_bits(N, X):- + set_bits(N, 0, X). +set_bits(0, X, X). +set_bits(N, X_Acc, X):- + B is N /\ 1, + X0 is X_Acc + B, + N0 is N >> 1, + set_bits(N0, X0, X), !. + +next_same_bits(N, NextSameBits):- + current_prolog_flag(max_integer, MAX_INTEGER), + set_bits(N, NumberBits), + N0 is N + 1, + between(N0, MAX_INTEGER, X), + set_bits(X, B), + B == NumberBits, + NextSameBits = X. + +main:- + next_same_bits(3, NextSameBits0), + write(NextSameBits0), nl, + next_same_bits(12, NextSameBits1), + write(NextSameBits1), nl, + halt. |
