Awk Comb

Combinations of r elements chosen from a pool of n elements

BEGIN {
        for (i=1; i <= r; i++) {
                A[i] = i
                if (i < r ) printf i OFS
                else print i}
        while (A[1] < n - r + 1) {
                for (i = r; i >= 1; i--) {
                        if (A[i] < n - r + i) {
                                A[i]++
                                p = i
                                break}}
                for (i = p + 1; i <= r; i++) A[i] = A[i - 1] + 1
                for (i=1; i <= r; i++) {
                        if (i < r) printf A[i] OFS
                        else print A[i]}}
        exit}

Posted in Uncategorized | Leave a comment

Hex

Hex

#!/usr/bin/python3
import sys
with open(sys.argv[1], "rb") as f:
    data = f.read()
for offset in range(0, len(data), 16):
    b = data[offset:offset + 16]
    left = " ".join(f"{x:02x}" for x in b[:8])
    right = " ".join(f"{x:02x}" for x in b[8:])
    hexpart = f"{left:<23}  {right:<23}"
    text = "".join(chr(x) if 32 <= x <= 126 else "." for x in b)
    print(f"{offset:08x}  {hexpart}  |{text}|")

Posted in Uncategorized | Leave a comment

Euler

scale=90
for (e = n = f = 1; fย != 0; f /= ++n) e += f
e

Posted in Uncategorized | Leave a comment

Palprimes

Palindromic primes

20 FOR N=2 TO 100000
30 I=N
35 J=0
40 J=J*10+I-INT(I/10)*10
43 I=INT(I/10)
47 IF I>0 THEN 40
50 IF J<>N THEN 80
52 IF N=2 THEN 75
60 FOR I=2 TO SQR(N)
65 IF N-INT(N/I)*I=0 THEN 80
70 NEXT I
75 PRINT N;" ";
80 NEXT N
85 PRINT

Posted in Uncategorized | Leave a comment

Palindate

Palindate

#! /bin/bash
is_palyndrom_date() { date -d "$1" 1>/dev/null 2>&1 && echo "$1"ย ; }
for _H in {2..9}; do
 for _I in {0..9}; do
  for _J in {0..99}; do
   is_palyndrom_date ${_H}${_I}`printf "%02d%s" ${_J}`-`printf "%02d%s" ${_J} |
rev`-`printf "%02d%s" ${_H}${_I} | rev`
  done
 done
done

Posted in Uncategorized | Leave a comment

Deal

Shuffle and deal four hands of five cards each with #Python

#!/usr/bin/python3
import random
deck = [
'๐Ÿ‚ก','๐Ÿ‚ข','๐Ÿ‚ฃ','๐Ÿ‚ค','๐Ÿ‚ฅ','๐Ÿ‚ฆ','๐Ÿ‚ง','๐Ÿ‚จ','๐Ÿ‚ฉ','๐Ÿ‚ช','๐Ÿ‚ซ','๐Ÿ‚ญ','๐Ÿ‚ฎ',
'๐Ÿ‚ฑ','๐Ÿ‚ฒ','๐Ÿ‚ณ','๐Ÿ‚ด','๐Ÿ‚ต','๐Ÿ‚ถ','๐Ÿ‚ท','๐Ÿ‚ธ','๐Ÿ‚น','๐Ÿ‚บ','๐Ÿ‚ป','๐Ÿ‚ฝ','๐Ÿ‚พ',
'๐Ÿƒ','๐Ÿƒ‚','๐Ÿƒƒ','๐Ÿƒ„','๐Ÿƒ…','๐Ÿƒ†','๐Ÿƒ‡','๐Ÿƒˆ','๐Ÿƒ‰','๐ŸƒŠ','๐Ÿƒ‹','๐Ÿƒ','๐ŸƒŽ',
'๐Ÿƒ‘','๐Ÿƒ’','๐Ÿƒ“','๐Ÿƒ”','๐Ÿƒ•','๐Ÿƒ–','๐Ÿƒ—','๐Ÿƒ˜','๐Ÿƒ™','๐Ÿƒš','๐Ÿƒ›','๐Ÿƒ','๐Ÿƒž'
]
random.shuffle(deck)
for i in range(0, 20, 5):
    print(" ".join(deck[i:i+5]))

Posted in Uncategorized | Leave a comment

Perl dupes

Highlight consecutive duplicated words with Perl

perl -pe 's/\b(\w+)\s+\1\b/\e[31m$&\e[0m/gi' gettysburg.txt

Posted in Uncategorized | Leave a comment

Factorial Primes

Factorial primes with gawk

#! /usr/bin/gawk -f
function isprime(n, i) {
  if(n==2)return 1
  if(n<2||n%2==0)return 0
  for(i=3;i*i<=n;i+=2)
    if(n%i==0)return 0
  return 1
}
BEGIN {
  for(x=i=1;z<10;i++){
    x*=i
    if(isprime(x-1)){z++;print i"! - 1 = "x-1}
    if(isprime(x+1)){z++;print i"! + 1 = "x+1}
  }
}

The next output would be (if you had the patience to do it):

27! + 1 = 10,888,869,450,418,352,160,768,000,001

Posted in Uncategorized | Leave a comment

Trig

Trig functions

#! /usr/bin/bc -l
define t(x) {
        return s(x) / c(x)
}
define y(y) {
        if (y == -1) return -2 * a(1)  /* -pi/2 */
        if (y == 1) return 2 * a(1)    /* pi/2 */
        return a(y / sqrt(1 - y * y))
}
define x(x) {
        auto a
         if (x == 0) return 2 * a(1)  /* pi/2 */
        a = a(sqrt(1 - x * x) / x)
        if (a < 0) {
                return a + 4 * a(1)  /* add pi */
        } else {
                return a
        }
}
p = 4 * a(1)  /* pi */
d = p / 180   /* one degree in radians */
"Using radians:
"
"sin(-pi / 6) = "; s(-p / 6)
"cos(3 * pi / 4) = "; c(3 * p / 4)
"tan(pi / 3) = "; t(p / 3)
"asin(-1 / 2) = "; y(-1 / 2)
"acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2)
"atan(sqrt(3)) = "; a(sqrt(3))

"Using degrees:
"
"sin(-30) = "; s(-30 * d)
"cos(135) = "; c(135 * d)
"tan(60) = "; t(60 * d)
"asin(-1 / 2) = "; y(-1 / 2) / d
"acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2) / d
"atan(sqrt(3)) = "; a(sqrt(3)) / d
quit

Posted in Uncategorized | Leave a comment

All the vowels

With Python find words longer than 10 characters that use all the vowels:

#! /usr/bin/env python3
with open('unixdict.txt') as f:
    while (lineย := f.readline().strip()):
        if (len(line) > 10 and all(
                line.count(c) == 1 for c in 'aeiou')):
            print(line)

Posted in Uncategorized | Leave a comment