From c31796a55c790185a9d0097c0fccd0f06144b774 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:09:46 -0700 Subject: [PATCH] Add script primes.sh to 1985/august for fun The inspiration of the script is based on a 'try' command I added to the README.md a while back to have more fun with the entry. The difference is that it allows you to specify the number of lines to print (or up to the number of lines). There might be some misleading output in a way but this is a mystery for users to solve. --- 1985/august/README.md | 12 +++++++++--- 1985/august/primes.sh | 25 +++++++++++++++++++++++++ thanks-for-fixes.md | 11 +++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100755 1985/august/primes.sh diff --git a/1985/august/README.md b/1985/august/README.md index 1e5f544fb..27ab703a9 100644 --- a/1985/august/README.md +++ b/1985/august/README.md @@ -21,13 +21,16 @@ make all ./august | head -n 10 ``` -If you have the `primes(6)` tool (sometimes part of BSD Games) you can see -what of the output in the first `N` (say `15`) lines are primes: +If you have the `primes(6)` tool (sometimes part of [BSD +Games](https://github.com/vattam/BSDGames)) you can see +what of the output in the first `N` (say `15` or `25`) lines are primes: ```sh -while read -r n ; do primes "$n" $((n + 1)) ; done < <((./august | head -n 15 )) +./primes.sh # 15 +./primes.sh 25 ``` + ## Judges' remarks: An interesting use of a recursive call to `main()`. Compile and execute @@ -38,6 +41,9 @@ If you let it, the program will continue to print a numerical sequence (can you guess in what base it is printed by looking at the code?) until you run out of memory or until they sell your computer, whichever comes first. +If you use the [primes.sh](primes.sh) script can you figure out if there's +anything funny going on with the output? + ## Author's remarks: No remarks were provided by the author. diff --git a/1985/august/primes.sh b/1985/august/primes.sh new file mode 100755 index 000000000..9cd9bdc8b --- /dev/null +++ b/1985/august/primes.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + + +make everything || exit 1 + +PRIMES=$(type -P primes) + +COUNT=15 +if [[ "$#" -ge 1 ]]; then + COUNT="$1" + if ! [[ $COUNT =~ ^[0-9]+$ ]] ; then + echo "not a number: $COUNT" 1>&2 + exit 1 + fi +fi + +if [[ -n "$PRIMES" ]]; then + while read -r n ; do primes "$n" $((n + 1)) ; done < <((./august | head -n "$COUNT" )) +else + echo "primes not installed" 1>&2 + echo "See the following GitHub repo:" 1>&2 + echo "" 1>&2 + echo " https://github.com/vattam/BSDGames" + exit 1 +fi diff --git a/thanks-for-fixes.md b/thanks-for-fixes.md index fafd7f2b4..6d55b4d2a 100644 --- a/thanks-for-fixes.md +++ b/thanks-for-fixes.md @@ -144,12 +144,13 @@ The crash was because it destructively rewrites string literals. However with `strdup()` it's safe. The problem with macOS is that although it didn't crash, it printed `H????` in a -seemingly infinite loop, each time printing another `?`, probably until it ran +seemingly infinite loop, each time printing another `?`, probably until it runs out of memory. The fix for macOS is that there was no prototype for `execlp()` and macOS has problems with missing prototypes for some functions (this was also seen when -Cody fixed [1984/anonymous](/1984/anonymous/anonymous.c) for macOS as well). +Cody fixed [1984/anonymous](/1984/anonymous/anonymous.c) for macOS as well). As +this is a one-liner the include of `unistd.h` was done in the Makefile. Ironically this fix was discovered through linux! NOTE: originally this entry did not print a newline prior to returning to the @@ -157,6 +158,12 @@ shell, after the output (despite having `\n` in the string - can you figure out why?) but to make it more friendly to users Cody made it print a `\n` prior to returning to the shell. The original version does not have this change. +## [1985/august](1985/august/august.c) ([README.md](1985/august/README.md)) + +Cody added the script [primes.sh](1985/august/primes.sh) which allows one to +check the output for the first N prime numbers of the output, where N is either +the default or user specified. The inspiration was the previous 'try' command he +gave to have fun with finding primes that might seem unusual in a way. ## [1985/lycklama](1985/lycklama/lycklama.c) ([README.md](1985/lycklama/README.md]))