Play a fun math game with Linux commands

Play the numbers game from the popular British game show "Countdown" at home.
95 readers like this.
Math formulas in green writing

João Trindade. Modified by Jason Baker. CC BY-SA 2.0.

Like many people, I've been exploring lots of new TV shows during the pandemic. I recently discovered a British game show called Countdown, where contestants play two types of games: a words game, where they try to make the longest word out of a jumble of letters, and a numbers game, where they calculate a target number from a random selection of numbers. Because I enjoy mathematics, I've found myself drawn to the numbers game.

The numbers game can be a fun addition to your next family game night, so I wanted to share my own variation of it. You start with a collection of random numbers, divided into "small" numbers from 1 to 10 and "large" numbers that are 15, 20, 25, and so on until 100. You pick any combination of six numbers from both large and small numbers.

Next, you generate a random "target" number between 200 and 999. Then use simple arithmetic operations with your six numbers to try to calculate the target number using each "small" and "large" number no more than once. You get the highest number of points if you calculate the target number exactly and fewer points if you can get within 10 of the target number.

For example, if your random numbers were 75, 100, 2, 3, 4, and 1, and your target number was 505, you might say 2+3=5, 5×100=500, 4+1=5, and 5+500=505. Or more directly: (2+3100 + 4 + 1 = 505.

Randomize lists on the command line

I've found the best way to play this game at home is to pull four "small" numbers from a pool of 1 to 10 and two "large" numbers from multiples of five from 15 to 100. You can use the Linux command line to create these random numbers for you.

Let's start with the "small" numbers. I want these to be in the range of 1 to 10. You can generate a sequence of numbers using the Linux seq command. You can run seq a few different ways, but the simplest form is to provide the starting and ending numbers for the sequence. To generate a list from 1 to 10, you might run this command:

$ seq 1 10
1
2
3
4
5
6
7
8
9
10

To randomize this list, you can use the Linux shuf ("shuffle") command. shuf will randomize the order of whatever you give it, usually a file. For example, if you send the output of the seq command to the shuf command, you will receive a randomized list of numbers between 1 and 10:

$ seq 1 10 | shuf
3
6
8
10
7
4
5
2
1
9

To select just four random numbers from a list of 1 to 10, you can send the output to the head command, which prints out the first few lines of its input. Use the -4 option to specify that head should print only the first four lines:

$ seq 1 10 | shuf | head -4
6
1
8
4

Note that this list is different from the earlier example because shuf will generate a random order every time.

Now you can take the next step to generate the random list of "large" numbers. The first step is to generate a list of possible numbers starting at 15, incrementing by five, until you reach 100. You can generate this list with the Linux seq command. To increment each number by five, insert another option for the seq command to indicate the step:

$ seq 15 5 100
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100

And just as before, you can randomize this list and select two of the "large" numbers:

$ seq 15 5 100 | shuf | head -2
75
40

Generate a random number with Bash

I suppose you could use a similar method to select the game's target number from the range 200 to 999. But the simplest solution to generate a single random value is to use the RANDOM variable directly in Bash. When you reference this built-in variable, Bash generates a large random number. To put this in the range of 200 to 999, you need to put the random number into the range 0 to 799 first, then add 200.

To put a random number into a specific range starting at 0, you can use the modulo arithmetic operation. Modulo calculates the remainder after dividing two numbers. If I started with 801 and divided by 800, the result is 1 with a remainder of 1 (the modulo is 1). Dividing 800 by 800 gives 1 with a remainder of 0 (the modulo is 0). And dividing 799 by 800 results in 0 with a remainder of 799 (the modulo is 799).

Bash supports arithmetic expansion with the $(( )) construct. Between the double parentheses, Bash will perform arithmetic operations on the values you provide. To calculate the modulo of 801 divided by 800, then add 200, you would type:

$ echo $(( 801 % 800 + 200 ))
201

With that operation, you can calculate a random target number between 200 and 999:

$ echo $(( RANDOM % 800 + 200 ))
673

You might wonder why I used RANDOM instead of $RANDOM in my Bash statement. In arithmetic expansion, Bash will automatically expand any variables within the double parentheses. You don't need the $ on the $RANDOM variable to reference the value of the variable because Bash will do it for you.

Playing the numbers game

Let's put all that together to play the numbers game. Generate two random "large" numbers, four random "small" values, and the target value:

$ seq 15 5 100 | shuf | head -2
75
100
$ seq 1 10 | shuf | head -4
4
3
10
2
$ echo $(( RANDOM % 800 + 200 ))
868

My numbers are 75, 100, 4, 3, 10, and 2, and my target number is 868.

I can get close to the target number if I do these arithmetic operations using each of the "small" and "large" numbers no more than once:

10×75 = 750
750+100 = 850

and:

4×3 = 12
850+12 = 862
862+2 = 864

That's only four away—not bad! But I found this way to calculate the exact number using each random number no more than once:

4×2 = 8
8×100 = 800

and:

75-10+3 = 68
800+68 = 868

Or I could perform these calculations to get the target number exactly. This uses only five of the six random numbers:

4×3 = 12
75+12 = 87

and:

87×10 = 870
870-2 = 868

Give the Countdown numbers game a try, and let us know how well you did in the comments.

What to read next
photo of Jim Hall
Jim Hall is an open source software advocate and developer, best known for usability testing in GNOME and as the founder + project coordinator of FreeDOS.

13 Comments

I'll add that I adapted the Numbers game for my article. The show does the Numbers game with two sets of small numbers 1-10, and one set of large numbers: 25, 50, 75, and 100. But 'seq 25 25 100' doesn't show off the 'seq' command very well. :-) To do the Numbers game more like the show, do this:

seq 25 25 100 | shuf | head -2
(seq 1 10 ; seq 1 10) | shuf | head -4
echo 'and the target is:'
echo $(( RANDOM % 800 + 200 ))

Hi Jim,

Thanks very much for this interesting article. Talking of games I've had in mind a tenuous subject on games and my idea on using the "grep" or "egrep" command to detect a win in that old game "4 in a row". I would like to share it on here at some point. Just thinking that I'm sure someone may have covered this already, but I'd like to share the idea I have in mind, as your post has kind of inspired me towards this idea, in using Linux commands.

In reply to by Jim Hall

That sounds like a great article idea - you should write this! I encourage you to contact the editors at https://opensource.com/contact to propose your article.

In reply to by Benny-B0Y

75 x 10 + 100 = 850
(4 + 2) x 3 = 18
850 + 18 = 868

small:7,10,3,2
large:40,90
target:353

90/10x40-7=353
(90-40)X7+3=353

Excellent - well done! I really enjoy the Numbers game. I have the Linux commands in a script so I can play Numbers whenever I have some down time. Here are a few plays:

Numbers: 25 100 10 2 9 8
Target: 795

8 x 100 = 800
10 / 2 = 5
800 - 5 = 795

Numbers: 25 75 7 6 3 10
Target: 225

25 x 10 = 250
75 / 3 = 25
250 - 25 = 225

Numbers: 100 75 3 4 5 2
Target: 707

3 + 4 = 7
7 x 100 = 700
5 + 2 = 7
7 + 700 = 707

In reply to by vdpolg

I use android : Termux APP
but bash scritpt can't support `echo $RANDOM`
===
#!/bin/sh
echo -n 'small 4 : '
echo `seq 1 10 | shuf | head -4 `
echo -n 'large 2 : '
echo `seq 15 5 100 | shuf | head -2`
echo -n 'target : '
echo `shuf -i 200-999 -n 1`
echo ===============================
echo -n 'Use RANDOM : '
echo $RANDOM
===
output
```
$ uname -a
Linux localhost 4.14.117-perf+ #1 SMP PREEMPT Thu Aug 20 00:23:47 2020 aarch64 Android
$ ./math.sh
small 4 : 6 8 3 4
large 2 : 30 60
target : 822
===============================
Use RANDOM :
$
```

That's because you're not actually running Bash. /bin/sh points to the Toybox shell by default in Termux, despite Bash is the default user shell there. The similar applies, for example, to Debian, where /bin/sh points to Dash by default, to Mac OS X 10.0–10.2, where it pointed to tcsh, or to OpenWRT and bare Android, where it points to Busybox.

Generally, it's a bad practice to use the `#!/bin/sh` shebang line when you explicitly use Bash-isms (which $RANDOM belongs to) rather than targeting any POSIX-compliant shell.

In reply to by vdpolg

4*(75*3-10+2) :P

Holy cow. That's fun. The first time I had [4,2,7,8,50,75,[327]] where 7*50 - 8*4 + 7 = 325!

The second time, and I think I'll quit while I'm ahead, was: [4,9,2,7,20,80,[650]] where 7*8+9*20/2 = 650!

I think I'll go buy a lottery ticket.

shuf -e {15..100..5} -n2
shuf -i 1-10 -n4
shuf -i 200-999 -n1

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.