Skip to content

Instantly share code, notes, and snippets.

@kevincolyer
Last active July 4, 2019 07:42
Show Gist options
  • Save kevincolyer/487b83bebc2fa941fc16bbb31c756efa to your computer and use it in GitHub Desktop.
Save kevincolyer/487b83bebc2fa941fc16bbb31c756efa to your computer and use it in GitHub Desktop.
#!/usr/bin/perl6
use v6;
use Test;
# 15.1 Write a script to generate first 10 strong and weak prime numbers.
#
# For example, the nth prime number is represented by p(n).
#
# p(1) = 1
# p(2) = 3
# p(3) = 5
#
# Strong Prime number p(n) when p(n) > [ p(n-1) + p(n+1) ] / 2
# Weak Prime number p(n) when p(n) < [ p(n-1) + p(n+1) ] / 2
my @p = (2,3,*+2 ... ∞).grep: *.is-prime;
my @weak = (1 .. ∞).map: { @p[$_] if @p[$_] < ( @p[$_-1] + @p[$_+1] ) /2 };
my @strong = (1 .. ∞).map: { @p[$_] if @p[$_] > ( @p[$_-1] + @p[$_+1] ) /2 };
sub MAIN($num where *>1)
{
say "$num weak primes";
say @weak[^$num];
say "$num strong primes";
say @strong[^$num];
}
#!/usr/bin/perl6
use v6;
use Test;
# 15.2
# Write a script to implement Vigenère cipher. The script should be able encode and decode. https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
#| Encodes or Decodes a text using Vigenère cipher.
multi sub MAIN(Str $text , Str $key, Bool :$encode=True, Bool :$decode=False) {
die "key needs to be smaller or equal to text" unless $text.chars > 0 and $key.chars > 0 and $key.chars <= $text.chars;
die "key [$key] must be ASCII" unless $key ~~ m:i /^ <[A..Z]>+ $/;
die "text [$text] must be ASCII" unless $text ~~ m:i /^ <[A..Z]>+ $/;
my $which=$encode && ! $decode;
say VigenereCipher($text,$key,$which);
};
multi sub MAIN(Bool :$test=False) {
say $*USAGE and exit() unless $test==True;
say "Testing";
my $plaintext = "ATTACKATDAWN";
my $key = "LEMONLEMONLE";
my $ciphertext = "LXFOPVEFRNHR";
is VigenereCipher($plaintext,$key,True), $ciphertext,"VigenereCipher encrypts ok";
is VigenereCipher($ciphertext,$key,False),$plaintext ,"VigenereCipher decrypts ok";
done-testing;
}
sub VigenereCipher($text,$key,$encode) {
my $offset="A".ord;
my @t = $text.uc.comb.map(*.ord-$offset);
my @k = $key .uc.comb.map(*.ord-$offset);
my @result;
my $EorD = $encode==True ?? 1 !! -1;
my $i=0;
for ^@t -> $j {
@result.push: chr($offset + ( (@t[$j]+ $EorD*@k[$i]) mod 26) );
$i=($i+1) mod @k.elems;
}
return @result.join ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment