essential maths

on the bbc and electron computers

basic routines for programming

czes kosniowski


First published 1984 by:
Sunshine Books (an imprint of Scot Press Ltd.)
12-13 Little Newport Street,
London WC2R 3LD

Copyright (c) Czes Kosniowski, 1984

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording and/or otherwise, without prior written permission of the Publishers.

British Library Cataloguing in Publication Data
Kosniowski, Czes
   Essential maths on the BBC and Electron computers.
   1.   Mathematics -- Data processing
   2.   Electron (Computer) -- Programming
   3.   BBC Microcomputer -- Programming
   1.   Title
   510'.28'542   QA76.95

ISBN 0-946408-34-3

Cover design by Grad Graphic Design Ltd.
Illustration by Ian Craig.
Typeset and printed in England by Commercial Colour Press, London E7.


CONTENTS

Page
Prefacevii
1Simple functions?1
2Trigonometry7
3Earth trigonometry29
4Powers35
5Sequences63
6Number Bases79
7Days and Weeks93
8Greatest Common Divisor107
9Primes111
10Odds and Ends125
11Matrices135
12Codes153
13Random!169
14Meaningful Data189
Summary203


Contents in detail

CHAPTER 1


Simple functions
Displaying numbers neatly, rounding off numbers, bank balances, overdrawn bank balances, colourful balances.

CHAPTER 2

Trigonometry
Scale drawings, the trigonometry functions, inverse functions, non right-angled triangles, refraction, reflection.

CHAPTER 3

Earth Trigonometry
The Earth, lines of longitude and latitude, calculating distances.

CHAPTER 4

Powers
Square roots, imaginary numbers, quadratic equations, solving other equations, Newton's method, exponential functions, logarithmic functions, roots of other functions.

CHAPTER 5

Sequences
Arithmetic sequences, geometric sequences, calculating interest, double or quit, Fibonacci sequences.

CHAPTER 6

Number Bases
Decimal representation, coefficients, binary numbers, hexadecimals, base converter, Acorn numbers, small numbers, floating points.

CHAPTER 7

Days and Weeks
Calculating dates -- Zeller's formula, calendar, date management.

CHAPTER 8

Greatest Common Divisor
Common factors, greatest common divisor, the Euclidean algorithm.

CHAPTER 9

Primes
Prime and composite numbers, testing primes, Sieve of Erastosthenes, large primes, Mersenne numbers, probabilistic primality testing, pesudoprimes.

CHAPTER 10

Odds and Ends
Pythagorean triplets and multi-precision powers.

CHAPTER 11

Matrices
Introducing matrices, adding matrices and how it can help, matrix multiplication, and why, zero matrices, identity matrices, inverses of matrices, simultaneous equations.

CHAPTER 12

Codes
Substitution codes, matrix codes, public key codes, encoding and decoding messages.

CHAPTER 13

Random!
Heads and tales, of dice and men, playing cards, non-equally likely events.

CHAPTER 14

Meaningful Data
Handling large amounts of numerical data, the mean, max, min and spread, standard deviation and variance, confidence intervals.

Preface


This book is written for owners of a BBC microcomputer or an Electron, who would like to know that little bit more about some mathematical techniques. You probably know what program you want to write but maybe you are not quite sure of the mathematics needed. Is it COS, ABS, or SGN that you need?
   All the mathematical functions that you find on the BBC/Electron are described and their use is illustrated in short programs. You can 'lift' these programs and utilize them within your own programs.
   But this book is not just an introduction to these basic mathematical functions. It contains background information and programs on such diverse subjects as codes and cryptography, random numbers, sequences, trigonometry, prime numbers, and statistical data analysis. You can utilize this information in both serious and games programming.
   Many thanks to Ann, Kora and Inga for bearing with me during the writing of this book.

Czes Kosniowski
Newcastle upon Tyne, January 1984


Program notes

The programs have been written in MODEs 0 to 6, to make them accessible to both the BBC and Electron microcomputers. Some minor changes to the programs may be necessary if you have a BBC micro with an old operating system. Lines involving an INPUT statement with a semicolon may need the semicolon to be changed to a comma. For instance, the line

   100 INPUT "What is your name ";N$

may need to be changed to the following

   100 INPUT "What is your name ",N$

Note: The symbol ^ sometimes appears as ­ in certain MODEs.

When entering programs from the listings in this book, please ensure that they are keyed in correctly. The quality of the hardcopy from the printer may make some characters look similar -- in particular, a comma may look very like a full stop at first glance.