CS199 Computer Science for Beginners  -  Spring 2009

Exercise 3    Due: Thursday, April 23, 2009 at Midnight

See below for instructions about which Python programs you need to send me by e-mail.

Be sure to name your files: Smith-2.py (if your last name was Smith and you were submitting exercise 2).  Etc.

Be sure to put comments in your program at the beginning that include your name, the Programming Exercise number, and a brief description of what your program does.

Remember: my e-mail address is lmd with the “at” symbol followed by cs.pdx.edu.

 

Do the following exercises in class on Thursday, April 16, 2009:

Problem 1:

Try this:

x = 100000000000000000000 (1 followed by 20 zeroes)

Now evaluate x in the Python interpreter, like this:

x

What do you get?

x = x + 1

Now evaluate x.

x

What do you get?

[Notice that it is a long int.  Notice that the result from x = x+1 is exact; it’s the correct answer.]

Problem 2:

Try this:

y = 100000000000000000000.0 (1 followed by 20 zeroes, but as a floating point number)

Now evaluate y, like this:

y

What do you get?

y = y + 1

Now evaluate y

y

What do you get?

[Notice that the y is still a floating point number.  Note that y = y+1 had NO effect on y.  That’s because floating point numbers don’t use enough bits to represent that many significant digits.]

 

Problem 3:

Try this:

Y = y + 7

Y

Did the value of y change?

Try this:

Y = y + 99

Y

Did the value of y change?

You could experiment to find out a number, that when added to y, actually changes the value of y.  You would be experimenting to determine the precision of y.

Problem 4:

Try this:

1.0   - .9

This subtracts .9 from 1; the answer should be 0.1 but it’s actually just an approximately because the answer is represented as a floating point number.

Now try this:

1000 – 999.9

This subtracts 999.9 from 1000; the answer should also be 0.1 but it is also just an approximation because the answer is represented as a floating point number.  But notice, especially that these two answer (that should both be 0.1 are actually different!   They are close to 0.1 but they are different.  Welcome to floating point arithmetic, performed by a computer!

Problem 5:

Try this:

4.0 – 1.0

And this:

7.0 – 1.0

Sometimes floating point numbers are represented precisely and sometimes they are not.        

In class – Write a function called divisible_by(x, y) that determines whether or not x is divisible by y (with a remainder of 0).  Have the function return True or False.  Test your program using a couple of if statements.

Additional in-class problems, for Tuesday, April 21, 2009:

Ch. 4: True/False: 1, 2, 4, 5

Ch. 4: Multiple choice: 1, 2, 3, 5, 6

Ch. 4: Discussion: 1, 2

Programming assignment: please turn these programs in:

Write a program that accepts a positive integer number as input, and then determines whether or not the number is a prime number.   A number n is prime if it is only divisible by n and by 1.  For example, 7 is prime because it is only divisible by 7 and by 1.  But 6 is not prime because 6 is divisible by 2, by 3, by 6 and by 1.

Chapter 7, programming exercise 2.

Chapter 4, programming exercise 5.