-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw2.py
140 lines (86 loc) · 1.93 KB
/
hw2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Name: Travis Goodroad
# Evergreen Login: Gootra28
# Computer Science Foundations
# Programming as a Way of Life
# Homework 2
# You may do your work by editing this file, or by typing code at the
# command line and copying it into the appropriate part of this file when
# you are done. When you are done, running this file should compute and
# print the answers to all the problems.
###
### Problem 1
###
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 1 solution follows:"
n = 100
total = 0
i = 1
while i <= n :
total = total + i
i = i + 1
print total
###
### Problem 2
###
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 2 solution follows:"
T = range( 2, 11 )
for i in T:
print 1.0/i
###
### Problem 3
###
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 3 solution follows:"
n = 10
triangular = 0
for i in range (1, n + 1):
triangular = triangular +i
print triangular
print n*(n+1)/2
###
### Problem 4
###
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 4 solution follows:"
n = 10
total = 1
for i in range ( 1 , n + 1 ) :
total = total * i
print total
###
### Problem 5
###
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 5 solution follows:"
# ... write your code and comments here (and remove this line)
n = 10
while n > 0:
total = 1
for i in range ( 1 , n + 1 ) :
total = total * i
print total
n = n - 1
# DO NOT CHANGE THE FOLLOWING LINE
print "Problem 6 solution follows:"
n = 1
total2 = 1.0
while n <= 10:
total = 1
for i in range ( 1 , n + 1 ) :
total = total * i
total2 = total2 + ( 1.0 / total)
n = n + 1
print total2
### Collaboration
### Ross Carrigan
### Ian Hooper
# ... List your collaborators and other sources of help here (websites, books, etc.),
# ... as a comment (on a line starting with "#").
#
###
### Reflection
###
# about 4 hours
# ... and tutorials linked to from the homework page. Did the readings, tutorials,
# yes the lecture on loops really helped =]