Thursday, March 3, 2011

This is a rewritten sequence program:
>>> def sequence(n):
...     while n != 1:
...             print n, 
...             if n % 2 == 0:
...                     n = n / 2
...             else:
...                     n = n * 3 + 1
... 
>>> sequence(3)
3 10 5 16 8 4 2
The program above also so the program working. 
This is the num_digits program that I've failed to put on here. This program also has three examples of the program working and getting an output.
>>> def num_digits(n):
...     count = 0
...     while n:
...             count = count + 1
...             n = n / 10
...     return count
... 
>>> num_digits(710)
3
>>> num_digits(999999999999999)
15
>>> num_digits(111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
96
>>> 
This program is basically self explanatory, it basically just counts the number places in the number. 
This is the num_zero_and_five_digits. This program counts the number of zeros and number of fives in the number:
>>> def num_zero_and_five_digits(n):
...     count = 0
...     while n:
...             digit = n % 10
...             if digit == 0 or digit == 5:
...                     count = count + 1
...             n = n / 10
...     return count
... 
>>> num_zero_and_five_digits(1055030250)
7

2 comments:

  1. This blog is called Mar. 3 forgot 2 add that

    ReplyDelete
  2. You don't have to date the blog entries yourself. That is done automatically.

    Also, getting back to the subject of how to post the larger files that you will be creating as you study progresses, I'm thinking we should try using dropbox.

    Let's talk about that on Monday.

    ReplyDelete