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