Ye Olde Curiosity Shoppe Of Software

Red rose of Lancashire

Some programs and scripts for amusement

Help yourself to the downloads from this site - all are GPL version 3 even though there's no comment to say so.

Fun with big numbers

There's a nice little program comes with most Linux distributions and other Unix-lookalikes. It's called bc. You can get a windows version here but I'm not sure how you'll go about running the scripts.

So what is bc?

bc is an arbitrary precision calculator, which is a fairly innocuous-sounding description util you realise what it can do.

Run the program and type some calculations into it. You don't get a prompt, so just type. Hit return (enter) to get the result of the calculation.

Pocket-calculator stuff. :-)

Try typing 2^32 (2 to the power of 31, or 2*2*2 ...*2 32 times). That might seem like a big number, but really it's pretty small, as numbers go. Your pocket calculator can manage it too. If your PC has 4 GB of memory that's how many bytes it has.

2^64 is bigger, but not much. Your pocket calculator might switch to E notation for that, which means it has probably lost some digits.

Now try 2^(2^32). It will probably fail with a runtime error because bc does have some built-in limits. So try 2^(2^31) instead. Your pocket calculator certainly won't manage that. If bc doesn't print an error message, it will be off thinking about it. It may take some time. And your PC might get a bit warm (the fan will speed up). But eventually it will print out a biggish number. 2^(2^31) has about 215,000,000 digits, if I've calculated correctly.

If you can't be bothered to wait for 2^(2^31), try 2^(2^20) instead. Even my weedy PC works that out in a few seconds. It has about 320,000 digits.

Prime numbers

prime-sheep.bc is a simple program written for bc that will help you go to sleep by counting sheep. Except in this case only prime sheep are counted.

Yes, it's a program the prints out all the prime numbers. Well, I say "all", but it will actually stop when it uses up all the memory in your PC. But you might get fed up before that and kill it or shut down your PC.

It works by looking at each number in turn; if the number doesn't divide by any of the prime numbers that it has already found, then it must be prime. :-). To start the ball rolling, 2 and 3 are prime. Note that, conventionally, 1 isn't prime. A whole load of mathematics would fall flat on its face if you made 1 a prime number.

The program needs to store all the prime numbers that it has already calculated, so the largest prime it can calculate is somewhat smaller than will fit in the memory.

You'll notice it gets slower and slower as the numbers get larger.

Fibonacci numbers using bc

Here's another little non-terminating program for bc. The Fibonacci sequence is all the numbers that are the sum of the previous two numbers in the sequence. Just to get the ball rolling, the first two numbers are 1,1. fib.bc prints out the Fibonacci sequence until it runs out of memory. Unlike prime-sheep, fib only needs to remember the two previous numbers. So theoretically it should be able to calculate larger numbers before it runs out of memory. But I never got that far. :-)

fib2.bc is another Fibonacci program. It defines a function fib(n) that prints out the nth Fibonacci number. "Run" the program (it just loads the function then waits), and then type fib(100). You get 354224848179261915075. Try bigger numbers until it crashes. The bigger the number, the longer it takes.

Fizz Buzz with bc

fizzbuzz.bc plays a the children's counting game "Fizz Buzz" with itself. If you've never played the game, this is how it goes: All the players stand in a circle. One of the players starts by calling "One!". Then next calls "Two!", and so on. Except there's a catch; if your number divides by 3 or contains the digit 3 you call "Fizz!" instead of the number and if your number divides by 5 or contains the digit 5 you call "Buzz". If your number satisfies both of these conditions you call "Fizz Buzz!". The first one to make a mistake pays a forfeit or drops out. Then the next player starts again with "One!"

Here are the first few turns:

Run the program - it's the fastest game of fizzbuzz you've ever seen (and it doesn't make mistakes).


© David Haworth
About this site (Impressum).
Don't say Greater Manchester, Merseyside or Cumbria when you mean Lancashire Valid XHTML 1.1 Valid CSS!