Algorithms -- 2009-2010 -- info.uvt.ro/Laboratory 2

From Wikiversity
Jump to navigation Jump to search

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, 7, evaluation, tools, references.

Notes[edit]

  • Laboratory / seminar problem set 1:
  • Laboratory / seminar 2 problem set:

Exercises[edit]

From the first problem set, exercises:

  • similar to problem 8 (for en), or 9 (for ro), but for the function ; (which is the approximation of , where x in near to 0;)
  • problem 9 (for en), or 10 (for ro) (Fibonacci sequence, and the fraction between two consecutive terms);

From the second problem set, exercises:

  • 1, set operations:
    • membership;
    • union;
    • intersection;

Assignment[edit]

For submission please follow: assignment 2.

From the first problem set, exercises:

  • problem 9 (for en), or 10 (for ro), point d, random number generation using congruential method; (see wikipedia:Linear congruential generator;)
  • like in the context of the problem 8 (for en), or 9 (for ro): approximation of the function:

From the second problem set, exercises:

Solutions[edit]

  • computing with a given precision:
p = input ("precision = ")
x = input ("x (between 0 and 1) = ")

pt = 1 # previous term
ct = x # current term
cti = 2 # current term index
s = pt + ct # the sum

while abs (pt - ct) > p :
    pt = ct
    ct = float (pt * x) / float (cti)
    cti += 1
    s += ct

print s

# the result for x = 0.5 should be around to 1.6487
  • approximation of the ratio between two consecutive Fibonacci terms:
p = 0.00001 # precission

pt = 1 # previous fibonacci term
ct = 1 # current fibonacci term
cr = float (ct) / float (pt) # current ratio
pr = cr + 2 * p # previous precission

while abs (pr - cr) > p :
    pr = cr
    a = pt
    pt = ct
    ct = ct + a
    cr = float (ct) / float (pt)

print cr

# the result should around 1.618

Ciprian Dorin Crăciun, ccraciun@info.uvt.ro