Skip to content

Instantly share code, notes, and snippets.

@JackZielke
Last active February 26, 2022 16:57
Show Gist options
  • Select an option

  • Save JackZielke/eebcf533fd7d421d563e6174c98e17f2 to your computer and use it in GitHub Desktop.

Select an option

Save JackZielke/eebcf533fd7d421d563e6174c98e17f2 to your computer and use it in GitHub Desktop.
Help find prime numbers for Primel https://converged.yt/primel/
#!/bin/bash
if test ! -e 5_digit_primes.txt.xz; then
echo 5_digit_primes.txt.xz does not exist
echo $0 requires this file
echo Please generate it with the following command
echo "seq 10007 99991|factor|grep '^\([^:]*\): \1$'|cut -d: -f1|xz -9e >5_digit_primes.txt.xz"
exit 2
fi
if test -z "$REG" -a -z "$XREG" -a -z "$SKIP"; then
echo Start with 25693
echo
echo Examples:
echo REG=..491 XREG=3.... SKIP=256870 $0
echo
echo REG=9.... XREG=.[35]5.3 SKIP=2681 $0
echo
echo 'REG = regex search - correct numbers'
echo XREG = regex remove - misplaced numbers
echo SKIP = numbers to remove - not in answer
exit 1
fi
COMMAND='xzcat 5_digit_primes.txt.xz'
if test -n "$REG"; then
COMMAND+="|grep ^$REG"
fi
if test -n "$XREG"; then
KEEP="$(echo $XREG|tr -d '.[]^'|fold -w1|sort -u |tr -d '\n')"
SEARCH=
for ((i=1;i<=${#XREG};i++)); do
if test "${XREG:$i-1:1}" = '['; then
SEARCH+='[^'
((i++))
while test "${XREG:$i-1:1}" != ']'; do
SEARCH+="${XREG:$i-1:1}"
((i++))
done
SEARCH+=']'
elif test "${XREG:$i-1:1}" != '.'; then
SEARCH+='[^'${XREG:$i-1:1}']'
else
SEARCH+=.
fi
done
if test -n "$SEARCH"; then
COMMAND+="|grep -E '$SEARCH'"
fi
fi
if test -n "$KEEP"; then
COMMAND+="|awk '"
for ((i=1;i<=${#KEEP};i++)); do
COMMAND+="/${KEEP:$i-1:1}/"
if test $i -ne ${#KEEP}; then
COMMAND+=' && '
fi
done
COMMAND+="'"
fi
if test -n "$SKIP"; then
PSKIP=$(echo $SKIP|sed 's/\B/|/g')
COMMAND+="|grep -Ev '$PSKIP'"
fi
eval $COMMAND
@JackZielke

JackZielke commented Feb 19, 2022

Copy link
Copy Markdown
Author

Builtin help

$ ./primel.sh

Start with 25693

Example:
REG=.7649 XREG=...9. SKIP=123580 ./primel.sh

REG = regex search - correct numbers
XREG = regex remove - misplaced numbers
SKIP = numbers to remove - not in answer

Example game

Guess: 25693
2 was green - correct and in the correct place.
5 and 9 were yellow - correct but in the incorrect place.
6 and 3 were gray - not in the answer.
$ REG=2.... XREG=.5.9. SKIP=63 ./primel.sh
From the resulting list, I picked a new prime. I picked one that had 2 numbers that I had not already tried (1 and 0).

Guess: 21059
2, 1, 5, and 9 were green.
0 was gray.
$ REG=21.59 SKIP=630 ./primel.sh
There were only 2 matching primes. I tried one, and then the other.

Guess: 21559
5 was gray.

Guess: 21859
Correct answer.

Prime numbers

You will need a list of all 5 digit primes. The script will tell you how to generate this list if it does not exist. The list of primes will use 7936 bytes of space. Generating the file takes less than 1 second on my Raspberry Pi so I thought directions on creating it would be easier than including it.

$ seq 10007 99991|factor|grep '^\([^:]*\): \1$'|cut -d: -f1|xz -9e >5_digit_primes.txt.xz

References

Script to generate primes came from nicodemus26 on Hacker News.
Starting prime came from Kiudee on Reddit.

@JackZielke

Copy link
Copy Markdown
Author

Updated to allow multiple misplaced numbers in the same position in XREG via [].
XREG=.[35]5.3

The second position can not be a 3 or a 5, the third position can not be a 5, the last position can not be a 3. 3 and 5 must be in the answer.

@JackZielke

JackZielke commented Feb 26, 2022

Copy link
Copy Markdown
Author

I don't think I would do it, but since the prime numbers are generated so quickly and everything is done in 1 pass, you could generate them on the fly each time and save 8k of disk space. To do this, delete (or comment out) lines 3 through 9. Replace the old line 25

COMMAND='xzcat 5_digit_primes.txt.xz'

with

COMMAND="seq 10007 99991|factor|grep '^\([^:]*\): \1$'|cut -d: -f1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment