Skip to content

Commit 3fb9e5c

Browse files
committed
solved problem 38 to problem 41
1 parent 999233b commit 3fb9e5c

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Chapter 5/Problem5_38.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def collatz(x):
2+
while x != 1:
3+
print(x)
4+
if x % 2 == 0:
5+
x = int(x/2)
6+
else:
7+
x = (3 * x) + 1
8+
print(x)

Chapter 5/Problem5_39.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def exclamation(string):
2+
for letter in string:
3+
if letter in 'AEIOUaeiou':
4+
print(letter * 4, end = '')
5+
else:
6+
print(letter, end ='')
7+
print('!')

Chapter 5/Problem5_40.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def approxPi(error):
2+
divup = 4
3+
div = 1
4+
div1 =1
5+
soma1 = 0
6+
soma2 = 0
7+
step = 1
8+
step1 = 1
9+
times = 1
10+
while soma1 - soma2 != error:
11+
for i in range(times - 1):
12+
if step1 % 2 == 0:
13+
soma2 -= 4/div1
14+
else:
15+
soma2 += 4/div1
16+
div1 += 2
17+
step1 += 1
18+
for i in range(times):
19+
if step % 2 == 0:
20+
soma1 -= 4/div
21+
else:
22+
soma1 += 4/div
23+
div += 2
24+
step += 1
25+
times += 1
26+
div1 = 1
27+
div = 1
28+
step1 = 1
29+
step = 1
30+
if abs(soma1 - soma2) <= error:
31+
return soma1
32+
soma1 = 0
33+
soma2 = 0

Chapter 5/Problem5_41.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def poly(coefficients, x):
2+
soma = 0
3+
for i in range(len(coefficients)):
4+
if i == 0:
5+
soma += coefficients[i]
6+
else:
7+
soma += coefficients[i]*(x**i)
8+
return soma

0 commit comments

Comments
 (0)