You are on page 1of 2

Question

3: Charging a capacitor Question 3: Charging a capacitor


Write a function that calculates the evolution of the voltage and
current in this circuit.

def simulate_circuit(V0, R, C, stop, ts):


t = 0
Vc = 0
simulation = []
while Vc <= stop*V0:
Equations: t += ts
Vc = V0*(1-exp(-t/(R*C)))
I = V0/R*exp(-t/(R*C))
*+ simulation.append([t, I, Vc])
!" = !% ∗ 1 − ) ,- return simulation

!% *+
.= ∗ ) ,-
/

Question 3: Charging a capacitor Question 4: Sieve of Eratosthenes


Save out your nested list in a csv file. In this file, each line contains Write a function find_primes(a) that prints out all prime
the information of the simulation at a moment in time, separated numbers between 2 and a given number a.
by a semicolon “;”.
def find_primes(a):
numbers = range(2, a + 1)
mask = [1] * len(numbers)
def write_simulation(simulation, path):
file = open(path, 'w')
message = ''
for i in range(len(numbers)):
for measure in simulation: if mask[i] == 1:
message += str(measure[0]) + ';' + str(measure[1]) + ';' + str(measure[2])
message += '\n'
file.write(message)
multiples = numbers[i] ** 2
file.close() while multiples <= a:
mask[multiples - 2] = 0
multiples += numbers[i]

for i in range(len(numbers)):
if (mask[i] == 1):
print(numbers[i])
Question 4: Sieve of Eratosthenes
Write a function find_primes(a) that prints out all prime
numbers between 2 and a given number a.

def find_primes(a):

A
numbers = range(2, a + 1)
mask = [1] * len(numbers)

for i in range(len(numbers)):
if mask[i] == 1:

multiples = numbers[i] ** 2
while multiples <= a:
B
mask[multiples - 2] = 0
multiples += numbers[i]

for i in range(len(numbers)):
if (mask[i] == 1):
print(numbers[i])
C

You might also like