-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhile loops.py
More file actions
145 lines (118 loc) · 2.71 KB
/
While loops.py
File metadata and controls
145 lines (118 loc) · 2.71 KB
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
140
141
142
143
144
# Topic 5: Question 1
def addNumbers(num):
total = 0
i = 1
while i <=num:
total += i
i+=1
return total
# Topic 5: Question 2
def addNumbers(start, end):
total = 0
while start <= end :
total += start
start+=1
return total
# Topic 5: Question 3
10
# Topic 5: Question 4
def countPages(num):
total = 0
i = 1
while i <= num:
page_no = str(i)
total += page_no.count('1')
i+=1
return total
# Topic 5: Question 5
'All give the same output.'
# Topic 5: Question 6
def factorial(num):
product = 1
i = num
while i > 0:
product = product * i
i -= 1
return product
# Topic 5: Question 7
def doubleFactorial(num):
product = 1
i = 0
k = 1
while k < num:
k = 2*i+1
product *= k
i += 1
return product
# Topic 5: Question 8
def primeNumbers(num):
primes = []
i = 2
# iterates through range from 2 to num(inclusive)
while i <= num : # add 'while' condition
k = 2
isPrime = True
# check if prime number
while k < i : # add 'while' condition
if i%k==0:
isPrime = False
k+=1 # update k
if isPrime:
primes.append(i)
i +=1 # update i
return primes
# Topic 5: Question 9
from math import floor
def sqApprox(num):
fnum = int(floor(num))
i = 0
while True:
if i * i <= fnum:
minsq = i
if i * i >= num:
maxsq = i
break
i = i + 1
assert minsq**2 <= num <= maxsq**2
return minsq, maxsq
# Topic 5: Question 10
def piApprox(rank):
value = 0
for k in range(1, 2*rank+1, 2):
sign = -(k % 4 - 2)
value += float(sign) / k
return 4 * value
# Topic 5: Question 11
def estimatePi():
import math
def factorial(n):
if n == 0:
return 1
else:
return math.factorial(n)
k = 1
Final = 0.31830987844
while Final > 1e-15:
b = (2 * math.sqrt(2) / 9801) * (factorial(4 * k) * (1103 + 26390 * k)) / (
(factorial(k) ** 4) * (396 ** (4 * k)))
Final = Final + b
k = k + 1
return 1 / Final
# Topic 5: Question 12
def primeFactorization(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
# Topic 5: Question 13
from fractions import gcd # Python versions below 3.5
from functools import reduce # Python version 3.x
def LCM(nums):
return reduce(lambda a,b: a*b // gcd(a,b), nums)