Working with Numbers Part 1
Basics | Repeating | Problems 1 | Procedures | Problems 2 | Remainders | Problems 3 |
Basic Operations
Using Logo to perform multiplication, addition, division and subtraction is very similar to doing it in Excel or on a programmable calculator.
Type the problem into the command line and press return to get the answer. Spaces between parts of the problem don't matter but you can use brackets to simplify expressions.
Additionshow 12 + 3 + 6 show 12 + 3 + 6 print 12 + 3 + 6 |
Subtractionshow 12-6 print 6-12 print 6-(-12 ) |
||
Multiplicationshow 12*6 print 6*12 print 6*(-12 ) print 6*-12 |
Divisionshow 12/6 print 6/12 print 6/(-12 ) print 6/-12 |
||
To make the output look better you can modify the print statement like this: (print "The "answer "to "4 "times "165789 "is 4*165789 ) To get an ordinary word to print as a word you must put a " in front of it. |
Using make, repeat and repcount.
MakeMake is a very useful command. It enables you to tell the computer to give a particular variable a given value.
make "sum 4+5+6 You must put a " in front of a variable when you are changing its value and : in front of it when you use it in a formula. | |
RepeatRepeat tells the computer to do a certain job several times. make "sum 0 repeat 4 [show :sum make "sum :sum + 10 ] show :sum Repeat statements need [ square brackets ]. You need to make sure that :sum is set to 0 at the start of this program. make "product 1 repeat 8 [show :product make "product :product *5 ] show :product You need to make sure that :product is set to 1 at the start of this program. | |
Repcountrepcount tells the computer how many times a repeat loop has been done.
repeat 5 [show repcount] Repeat statements need [ square brackets ]. |
|
You can combine commands onto oneline, as shown below; this makes it easier to understand what is happenning. |
Problems 1
- Use Logo to find the nth triangular number.
- Use Logo to find the nth power of a number.
Start a Word document to record your solutions and answers to these problems. You can cut and paste into and out of Logo.
Triangular numbersTriangular numbers can be found by adding the whole numbers together, in order, starting from 1. Thus: Use Logo to find T5, T10 and T100 |
PowersThe nth power of a number is that number multiplied by itself n times. Use Logo to find 255, 225 and 12344 |
Writing Procedures.
A procedure is a set of several lines of instructions which the computer remembers and which you can call upon to do something.
We will write a procedure to calculate cubes.
You have already found cubes by typing them directly into the command line eg:
show 4*4*4
Procedures save time and effort in the long run and can do much more complicated things than single lines entered at the command line.
Calling up the EditorThe procedure editor is called by typing: edit "cube The editor screen will appear. Type this code in. It is a simple procedure which finds and prints the cube of a variable called :number which you send to the procedure when you call it.
to cube :number |
|
Saving your procedure.Now select file and save and exit. This will save your program to RAM. You must also use save in the main Logo screen if you want to save your program to disc. |
|
Testing procedures.Now test your procedure by calling it to calculate some cubes. Start with ones that you know:
cube 2 The first 5 answers are exact. Logo has given an integer (whole number) answer. The final answer is too big for Logo to cope with in integer format and so it has switched to standard form. This answer is no longer exact. If you want to find the exact value of a big cube then use must write a special procedure or use a program like derive |
|
Problems 2
- Write a procedure to find the sums of the squares of two numbers.
- Write a procedure to find the sums of the cubes of two numbers.
Start a Word document to record your solutions and answers to these problems. You can cut and paste into and out of Logo.
Sums of squaresWrite a procedure to find the sums of the squares of two numbers. For example: Here is the print out from my solution. It shows the sums of squares of the numbers 1&1 2&1 2&2 etc. up to 3. Here is a different solution which works on two numbers at a time. You tell the machine which numbers to work with. |
Sums of cubesWrite a procedure to find the sums of the cubes of two numbers. For example: Here is the print out from my solution. It shows the sums of cubes of the numbers 1&1 2&1 2&2 etc. up to 3. Here is a different solution to the same problem. |
Remainder, if and ifelse.
Remainder, if and ifelse are three useful commands which allow you to write a large number of useful programs.
remainderThis outputs the remainder of the first number divided by the second number. For example: print remainder 10 6 print remainder 6 10 print remainder 100 10 print remainder 55 10.2 |
Remainder only works with integers (whole numbers.) |
ifThis executes a piece of code only if a certain condition is true. For example: if (6*8=48) [print "true] if (6*8=59) [print "false] repeat 9 [if ((remainder repcount 2) = 0) [print repcount]] The first command printed true as 6*8 is indeed 48. The second command gave no output as 6*8 isn't 59. The third command prints the value of repcount in the repeated bracket if repcount is even, that is, if it leaves remainder 0 on division by 2. | |
ifelseThis executes one piece of code if a certain condition is true and does something else if it isn't. For example: ifelse (6*8=48) [print "true] [print "false] ifelse (6*8=59) [print "true][print "false] repeat 9 [ifelse ((remainder repcount 2) = 0) [(print repcount "is "even)][(print repcount "is "odd)] ] The first command printed true as 6*8 is indeed 48. The second command printed false as 6*8 isn't 59. The third command printed odd if repcount gave remainder 1 on division by 2 and even if repcount gave remainder 0 on division by 2. |
Problems 3
- Write a procedure to find the factors of a number.
- Write a procedure to find the prime factors of a number.
- Write a procedure to find the highest common factor, HCF, of two numbers.
- Write a procedure to find Pythagorean triples. (3,4 and 5 or 5, 12 and 13 are such numbers)
Start a Word document to record your solutions and answers to these problems. You can cut and paste into and out of Logo.
FactorsWrite a procedure to find the factors of a given number. For example: Here is the print out from my solution. It shows the factors of 24. There are lots of ways to do this but some methods are much more efficient than others. In computer speak efficient means fast and requiring the minimum of code, memory or processor time. |
Prime factors.Write a procedure to find the prime factors of a given number. For example: 3*17 = 51 Here is the print out from my solution. It shows the prime factors of 144 and 191. |
Final ChallengeWhat is the relationship between the pairs of numbers situated opposite each other in the diagram below? Fill in the other boxes. You are only allowed numbers below 100 and you can only have each number once. All the programming tricks that you need have been demonstrated on this webpage. |
last modified 4th February 2012