• Nie Znaleziono Wyników

Solutions

N/A
N/A
Protected

Academic year: 2021

Share "Solutions"

Copied!
4
0
0

Pełen tekst

(1)

Solutions for tasks from ex. 4 Podstawowe pojęcia i narzędzia informatyki

Tasks Task 1

Sort given functions in ascending order according to their ranks of order:

2 n! , n 2 , (2009!)n 2 , n 3 − n 2 , n 3 , n 3 + n 2 , 2 n , n 2009! , n! , n log n , log n.

Are there any functions of the same rank ? Find and prove relation between functions:

— 2n w odniesieniu do (20!)n,

— n 2 − n w odniesieniu do n 2 + n,

— log 2 n w odniesieniu do n,

— 3 n w odniesieniu do 2 n

— n! w odniesieniu do 2 n Solution

Order:

log n , n log n , n 2 , (2009!)n 2 , n 3 − n 2 , n 3 , n 3 + n 2 , n 2009! , 2 n , n! , 2 n!

a) 2n ∈ Θ((20!)n). Because ∃ c 1 = (20!) 1 , c 2 = 1 such, that ∀n ­ 1 c 1 · (20!)n = 1

(20!) · (20!)n = n ¬ 2n ¬ 1 · (20!)n = c 2 · ((20!)n.

b) n 2 − n ∈ Θ(n 2 + n). Because ∃ c 1 = 1 2 , c 2 = 1 such, that (condition for which n it is true is absolutely required!) ∀n ­ 3

c 1 · (n 2 + n) = 1

2 · (n 2 + n)

(1)

¬ n 2 − n ¬ 1 · (n 2 + n) = c 2 · (n 2 + n).

Where inequality (1) holds ∀n ­ 3, due to : 1

2 · (n 2 + n) ¬ n 2 − n 1

2 n 2 + 1

2 n ¬ n 2 − n 3

2 n ¬ 1

2 n 2 , (n ­ 0) stąd 3

2 ¬ 1

2 n

3 ¬ n

c) log 2 n ∈ O(n). Because ∃ c 1 = 1 such, that ∀n ­ 1 log 2 n

(1)

¬ n = 1 · n = c 1 · n.

Where inequality (1) holds ∀n ­ 1, due to:

n = log 2 2 n ­ log 2 n, (is equivalent to the inequality) 2 n ­ n, which is true, because for n = 1 we have 2 n = 2, n = 1, and for all consecutives n > 1 in sequence 2 n we multiply value of the previous element from the sequence by 2 while for sequence n we add 1 to the the previous element from the sequence. It implies that for all n > 1 element from sequence 2 n must be greater than element from sequence n.

1

(2)

Solutions for tasks from ex. 4 Podstawowe pojęcia i narzędzia informatyki

d) 3 n ∈ Ω(2 n ). Because ∃ c 1 = 1 such, that ∀n ­ 1 c 1 · 2 n = 1 · 2 n

(1)

¬ 3 n

Where inequality (1) holds ∀n ­ 1, because for n = 1 we have 2 n = 2, 3 n = 3, and for all consecutives n > 1 in sequence 2 n we multiply value of the previous element from the sequence by 2 while for sequence 3 n we multiply value of the previous element from the sequence by 3. It implies that for all n > 1 element from sequence 3 n must be greater than element from sequence 2 n .

e) n! ∈ Ω(2 n ) Because ∃ c 1 = 1 2 such, that ∀n ­ 1

c 1 · 2 n = 1

2 · 2 n (1) ¬ n!

Where inequality (1) holds ∀n ­ 1, because for n = 1 we have 1 2 · 2 n = 1, n! = 1, and for all consecutives n > 1 in sequence 2 n we multiply value of the previous element from the sequence by 2 while for sequence n! we multiply value of the previous element from the sequence by n − 1 ­ 2. It implies that for all n > 1 element from sequence n! must be greater than element from sequence 2 n .

Task 2

For given number n ∈ N generate all prime numbers which are less or equal n. Compare the complexity of two given algorithms:

— for all numbers 2 ¬ k ¬ n sequentially we check whether it is a prime number

— we use method called Sieve of Eratosthenes http://en.wikipedia.org/

wiki/Sieve_of_Eratosthenes from the list 2...n we cross out consecu- tively: numbers which are divisible by 2, numbers which are divisible by 3,..and so one up to

n.

Solution

Answer : a) n 2 . b) n ln ln n (ln n = log e n)

It is sufficient to give the answer like the above. Below we show how to formally show that they are in equal to n 2 and n ln ln n.

a) For each number from 2 to n we must perform some operation. So our algorithm must contain a loop:

for i:=2 to n

In this loop our main aim is to check whether given number is a prime. In other words we must check if it divide by some number from 2, ..., n − 1. so there must be another loop when we could check whether this number can be divided:

for j:=1 to i-1

check whether it can be divided

Bringing it all together we get something like:

2

(3)

Solutions for tasks from ex. 4 Podstawowe pojęcia i narzędzia informatyki

for i:=2 to n { for j:=2 to i-1 {

check whether it can be divided }

{

our algorithm perform 1 + 2 + 3 + ... + n − 1 operations (respectively to check divisibility for 3, 4, 5, ..., n). Sequence 1 + 2 + 3 + ... + n − 1 sum up to

n(n−1)

2 = n

2

2 −n . We know that n

2

2 −n ∈ Θ(n 2 ), then the rank of order of the complexity of algorithm is equal to n 2 .

b) We have a table from which we consequently cross out complex num- bers. For each number from 2 to n we must perform some operation. So our algorithm must contain a loop:

for i:=2 to n

In this loop our main aim is to check whether given number is a complex.

If it is we move on to the next number, if not we cross out from our table all numbers which are multiples of given prime (starting from square of this number). So for each prime number i we perform n i operations. Given algorithm will apper as something like:

if i is a prime number for (j:= i*i; j<=n; j+=i)

mark j’s number as a complex

Bringing it all together we get something like:

for i:=2 to n

if i is a prime number for (j:= i*i; j<=n; j+=i)

mark j’s number as a complex

For each prime number i algorithm perform n i operations. Total numbers of operations will be then equal to

X

n

n i

Excluding n in frotn of the sum n · X

n

1

i , where i is a prime number.

From Number Theory we know that P n 1 i = ln ln n, then the rank of order of the complexity of algorithm is equal to n ln ln n.

Remark! As to solve this task it was necessary to know some extra in- formation from number theory it is not a task that could apper onm test.

The task from the test will definately be more similar to the task 3.

Task 3

There is a polynomial W (x) = a 0 + a 1 x + a 2 x 2 + . . . + a n−1 x n−1 + a n x n . We want to find the value of this polynomial in point x 0 , we can do it in two ways:

3

(4)

Solutions for tasks from ex. 4 Podstawowe pojęcia i narzędzia informatyki

— calculate direct from the formula W (x 0 ) = a 0 + a 1 x 0 + a 2 x 2 + . . . + a n x n

— use Horner scheme transforming the polynomial into form W (x) = a 0 + x(a 1 + a 2 x + . . . + a n−1 x n−2 + a n x n−1 ) =

= a 0 + x(a 1 + x(a 2 + . . . + a n−1 x n−3 + a n x n−2 )) =

= a 0 + x(a 1 + x(a 2 + . . . + x(a n−1 x + a n ) . . .))

Find the time complexity of both algorithms when we choose the dominating operation to be a)Adding b) Multiplying c)Any arithmetic operation.

Solution

We are interested in ranks of order of the function, not their exact values:

a) In I case n. In II case n.

b) In I case n 2 (as a sum 1 + 2 + ... + n). In II case n.

c) In I case n 2 . In II case n.

Task 4 Sortings n 2 .

Binary search log 2 n (because in each step we divide the set of solutions into two parts).

Determining F n - iterative n operations, we won’t analyse the complexity of recurrence algorithms.

4

Cytaty

Powiązane dokumenty

Realizacji, w których pojawiają się postaci zwierząt jest mnóstwo, ale w  zdecydowanej większości wypadków możemy mówić jedynie o  wykorzysta- niu kostiumu,

This creates a design challenge for both the luminance efficiency of the phosphor and electrodes (high power efficiency and dissipate heat generated). The VFD HUD implemented rare

To obtain ∆p you can use the momentum representation of the momentum operator and apply it to the state

No precise date can be attributed to the invention of the thermometer, although it is probable that between the years 1593 and 1603 Galileo Galilei worked on an in- strument to

Sketch the graph of the func- tion f n (x) and the graph of the derivative.. (4) Compute the derivative of the

– In the situation presented in c) – the conditions of the task realization by the engine are formulated in the most tolerant way, irrespective of the usability limitations (also

Changing a fraction from having a denominator that is irrational to an equivalent fraction where the denominator is rational (rationalizing the denominator) is not always a

(The companion WB-sequences are the X 0 and Y 0 sequences; their properties generalize those of the usual companion Lucas.. the V n -sequence.) Secondly it is proved that the