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

From testwiki
Jump to navigation Jump to search

Template:Algorithms -- 2009-2010 -- info.uvt.ro/Page header

Notes

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

Exercises

From the first problem set, exercises:

  • similar to problem 8 (for en), or 9 (for ro), but for the function i=0xii!; (which is the approximation of ex, 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

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: i=01ix2i(2i)!

From the second problem set, exercises:

Solutions

  • computing i=0xii! 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

Template:Algorithms -- 2009-2010 -- info.uvt.ro/Page footer