├── README └── one-liners.sh /README: -------------------------------------------------------------------------------- 1 | A repo with assorted one-liners I come up with to do simple math stuff 2 | using regexps. 3 | 4 | These are not golf exercises (they all can be shortened indeed), the 5 | motivation is to explore the technique Abigail uses in his famous 6 | primality test. 7 | -------------------------------------------------------------------------------- /one-liners.sh: -------------------------------------------------------------------------------- 1 | # Prints "coprime" if the arguments are relatively prime. 2 | perl -le 'print "coprime" if "@{[1 x pop]} @{[1 x pop]}" !~ /^(11+)\1* \1+$/' 3 4 3 | coprime 4 | 5 | # Number of divisors. This solution shows off how to embed a loop in the 6 | # regexp by forcing backtracking. In this case using the Perl (??{ ... }) 7 | # construct, but the techinique is generic. 8 | perl -le '(1 x pop) =~ /^(1+)\1*$(??{++$t})/; print $t' 8 9 | 4 10 | 11 | # Prime factorization. 12 | perl -le '$_ = 1 x pop; print $+[1] and s/$1/1/g while /^(11+?)\1*$/' 60 13 | 2 14 | 2 15 | 3 16 | 5 17 | 18 | # n mod m. 19 | perl -le "(1 x shift) =~ /(1{@{[shift]}})*/; print length $'" 17 8 20 | 1 21 | 22 | # Fraction reduction to lowest terms. 23 | perl -le '$_ = "@{[1 x shift]} @{[1 x shift]}"; s/$1/1/g while /^(11+?)\1* \1+$/; print length $& while /1+/g' 60 24 24 | 5 25 | 2 26 | 27 | # Euler's phi function: number of positive integers less than or equal to n relatively prime to n. 28 | perl -le '$n = pop; print~~grep {"@{[1 x $_]} @{[1 x $n]}" !~ /^(11+)\1* \1+$/} 1..$n' 60 29 | 16 30 | 31 | # Greatest common divisor (gcd). 32 | perl -le '"@{[1 x pop]} @{[1 x pop]}" =~ /(1+)\1* \1+$/ && print $+[1]' 27 36 33 | 9 34 | 35 | # Square-free test: an integer is square-free if it is divisible by no perfect square (except 1). 36 | perl -le '$_ = 1 x pop; s/$1/1/g && /^($&)+$/ && exit while /(11+?)\1*$/; print "square-free"' 15 37 | square-free 38 | 39 | # Perfect square test: an integer is a perfect square if it is equal to n^2 for some n. 40 | perl -le '$_ = 1 x (pop||1); 1 while /^(11+?)\1*$/ && /^(($1){$+[1]})+$/ && s/$1/1/g; print "perfect square" if /^1$/' 64 41 | perfect square 42 | 43 | # Alternative perfect square test. 44 | perl -le '$_ = 1 x pop; $n = 1; $n += 2 while s/1{$n}//; $_ || print "perfect square"' 64 45 | perfect square 46 | 47 | # Integer square root. 48 | perl -le '$_ = 1 x pop; $n = 1; $n += 2 while s/1{$n}/0/; print tr/0//d' 2012 49 | 44 50 | 51 | # Triangular number test. 52 | perl -le '$_ = 1 x pop; 1 while s/($1 1)//x; $_ || print "triangular"' 21 53 | triangular 54 | 55 | # Continued fraction. 56 | perl -le '($_, $q) = map {1 x $_} @ARGV; $q = $& while print s/$q//g||0 and s/1+/$q/' 93 415 57 | 0 58 | 4 59 | 2 60 | 6 61 | 7 62 | 63 | # Sieve of Eratosthenes. 64 | perl -le '$_ = join " ", map {1 x $_} 2..pop; print $+[1] and s/\b($1)+\b *//g while s/(1+) ?//' 10 65 | 2 66 | 3 67 | 5 68 | 7 69 | 70 | # Perfect number test: Perfect numbers are positive integers that are equal to 71 | # the sum of their proper positive divisors. 72 | perl -le '$n = 1 x pop; $n =~ /^(1+)\1+$(??{$s.=$^N})/; print "perfect number" if $s eq $n' 496 73 | perfect number 74 | 75 | # Fibonacci numbers. 76 | perl -le '$f = "1 1"; $f =~ s/(1+) (1+)/$1$2 $1/ for 3..pop; $f =~ /1+/; print $+[0]' 16 77 | 987 78 | --------------------------------------------------------------------------------