• Keine Ergebnisse gefunden

Local Variables

N/A
N/A
Protected

Academic year: 2022

Aktie "Local Variables"

Copied!
120
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Departement Informatik

Basics of CS

Complexity and Primality Testing

Hans-Joachim Böckenhauer Dennis Komm

(2)

Functions

Scope and Lifetime of Variables

(3)

Local Variables

Parameters of a function are only valid within this function

def f(x):

return x + 5 print(x)

NameError: name ’x’ is not defined

(4)

Local Variables

Parameters of a function are only valid within this function

def f(x):

return x + 5 print(x)

NameError: name ’x’ is not defined

(5)

Local Variables

The same is true for variables that are defined in a function

def f(x):

y = 5

return x + y print(y)

NameError: name ’y’ is not defined

(6)

Local Variables

The same is true for variables that are defined in a function

def f(x):

y = 5

return x + y print(y)

NameError: name ’y’ is not defined

(7)

Local Variables

Such variables (parameters) are calledlocal variables

Variables defined outside of a function are calledglobal variables Field of validity is calledscopeof the variable

Time in which it is defined is calledlifetimeof the variable

def f(x): if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

def f(x): if x < 0:

return-100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

(8)

Local Variables

Such variables (parameters) are calledlocal variables

Variables defined outside of a function are calledglobal variables Field of validity is calledscopeof the variable

Time in which it is defined is calledlifetimeof the variable

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

def f(x): if x < 0:

return-100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

(9)

Local Variables

Such variables (parameters) are calledlocal variables

Variables defined outside of a function are calledglobal variables Field of validity is calledscopeof the variable

Time in which it is defined is calledlifetimeof the variable

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

scopeofx

def f(x): if x < 0:

return-100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

(10)

Local Variables

Such variables (parameters) are calledlocal variables

Variables defined outside of a function are calledglobal variables Field of validity is calledscopeof the variable

Time in which it is defined is calledlifetimeof the variable

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

scopeofx

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

(11)

Local Variables

Such variables (parameters) are calledlocal variables

Variables defined outside of a function are calledglobal variables Field of validity is calledscopeof the variable

Time in which it is defined is calledlifetimeof the variable

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

scopeofx

def f(x):

if x < 0:

return -100 y = x + 1 if y < 10:

y += 10 else:

y -= 20 return y

scopeofy

(12)

Global Variables

Global variables can be accessed within a function

x = 1

def f(): y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(13)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(14)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

global variablex

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(15)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

local variableygets value

which depends on global variablex

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(16)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

output global variablex

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(17)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

output global variablex output local variabley

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(18)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

output global variablex output local variabley output global variablex

x = 1

def f(y): z = x + y return z print(x) print(f(2)) print(x)

(19)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

(20)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

global variablex

(21)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

parametery

(22)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

local variablezgets value

that depends on global variablex and parametery

(23)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

output global variablex

(24)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

output global variablex output local variablez

(25)

Global Variables

Global variables can be accessed within a function

x = 1

def f():

y = x + 1 print(y) return print(x) f() print(x)

x = 1

def f(y):

z = x + y return z print(x) print(f(2)) print(x)

output global variablex output local variablez output global variablex

(26)

Local Variables

Local and global variables can have the same name

Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f(): x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(27)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f(): x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(28)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(29)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

global variablex

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(30)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

local variablex

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(31)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

local variablexis returned

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(32)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

output global variablex

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(33)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

output global variablex output local variablex

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(34)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

output global variablex output local variablex output global variablex

x = 1

def f(x): x = x + 1 return x print(x) print(f(2)) print(x)

(35)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

(36)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

global variablex

(37)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

parameterx

(38)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

parameterxgets new value that depends on its

current value

(39)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

parameterxis returned

(40)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

output global variablex

(41)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

output global variablex output parameterx

(42)

Local Variables

Local and global variables can have the same name Shadowing

Not forbidden, but should be avoided here whenever possible

x = 1

def f():

x = 2 return x print(x) print(f()) print(x)

x = 1

def f(x):

x = x + 1 return x print(x) print(f(2)) print(x)

output global variablex output parameterx output global variablex

(43)

Time Complexity of Algorithms

Primality Testing

(44)

Exercise – Primality Testing

Write a function that

takes an integerxas parameter

calculates whetherxis prime by search for a divisor between1andx

uses the%operator

depending on that either returnsTrue orFalse

(45)

Primality Test

def primetest(x):

if x < 2:

return False d = 2

while d < x:

if x % d == 0:

return False d += 1

return True

(46)

Primality Test

How long does it take the algorithm to produce the output?

What is itstime complexity?

This depends on the number of loop iterations An absolute value does not make sense here The loop is iterated (roughly)xtimes (ifxis prime) ï Time complexity grows withx

. . .but how fast?

(47)

Primality Test

How long does it take the algorithm to produce the output?

What is itstime complexity?

This depends on the number of loop iterations

An absolute value does not make sense here The loop is iterated (roughly)xtimes (ifxis prime) ï Time complexity grows withx

. . .but how fast?

(48)

Primality Test

How long does it take the algorithm to produce the output?

What is itstime complexity?

This depends on the number of loop iterations An absolute value does not make sense here The loop is iterated (roughly)xtimes (ifxis prime) ï Time complexity grows withx

. . .but how fast?

(49)

Primality Test

How long does it take the algorithm to produce the output?

What is itstime complexity?

This depends on the number of loop iterations An absolute value does not make sense here The loop is iterated (roughly)xtimes (ifxis prime) ï Time complexity grows withx. . .but how fast?

(50)

Time Complexity – Function of input size

We measure the time complexity as a function of the input size

The input of our algorithm is a single numberx In our computer, numbers are represented in binary Ignoring leading zeros, fornbits we obtain

2n−1 is10. . .00

| {z }

n

, 2n−1+ 1is10. . .01

| {z }

n

, . . . , and2n−1is11. . .11

| {z }

n

A number that is encoded withnbits has size around2n

(51)

Time Complexity – Function of input size

We measure the time complexity as a function of the input size The input of our algorithm is a single numberx

In our computer, numbers are represented in binary

Ignoring leading zeros, fornbits we obtain 2n−1 is10. . .00

| {z }

n

, 2n−1+ 1is10. . .01

| {z }

n

, . . . , and2n−1is11. . .11

| {z }

n

A number that is encoded withnbits has size around2n

(52)

Time Complexity – Function of input size

We measure the time complexity as a function of the input size The input of our algorithm is a single numberx

In our computer, numbers are represented in binary Ignoring leading zeros, fornbits we obtain

2n−1 is10. . .00

| {z }

n

, 2n−1+ 1is10. . .01

| {z }

n

, . . . , and2n−1is11. . .11

| {z }

n

A number that is encoded withnbits has size around2n

(53)

Time Complexity – Function of input size

We measure the time complexity as a function of the input size The input of our algorithm is a single numberx

In our computer, numbers are represented in binary Ignoring leading zeros, fornbits we obtain

2n−1 is10. . .00

| {z }

n

, 2n−1+ 1is10. . .01

| {z }

n

, . . . , and2n−1is11. . .11

| {z }

n

A number that is encoded withnbits has size around2n

(54)

Time Complexity – Technology Model

Random Access Machine

Execution model:Instructions are executed one after the other (on one processor core)

Memory model: Constant access time

Fundamental operations:Computations (+,−,·, . . .) comparisons, assignment / copy, flow control (jumps)

Unit cost model: Fundamental operations provide a cost of1

(55)

Time Complexity – Technology Model

Random Access Machine

Execution model:Instructions are executed one after the other (on one processor core)

Memory model: Constant access time

Fundamental operations:Computations (+,−,·, . . .) comparisons, assignment / copy, flow control (jumps)

Unit cost model: Fundamental operations provide a cost of1

(56)

Time Complexity – Technology Model

Random Access Machine

Execution model:Instructions are executed one after the other (on one processor core)

Memory model: Constant access time

Fundamental operations:Computations (+,−,·, . . .) comparisons, assignment / copy, flow control (jumps)

Unit cost model: Fundamental operations provide a cost of1

(57)

Time Complexity – Technology Model

Random Access Machine

Execution model:Instructions are executed one after the other (on one processor core)

Memory model: Constant access time

Fundamental operations:Computations (+,−,·, . . .) comparisons, assignment / copy, flow control (jumps)

Unit cost model: Fundamental operations provide a cost of1

(58)

Time Complexity – Note

We are not completely accurate here

Numbers can have arbitrarily large values

The time needed to add twon-bit numbers depends onn

Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

Logarithmic cost modeltakes this into account, but is not used here We assume that arithmetic operations can be done in constant time

(59)

Time Complexity – Note

We are not completely accurate here

Numbers can have arbitrarily large values

The time needed to add twon-bit numbers depends onn

Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

Logarithmic cost modeltakes this into account, but is not used here We assume that arithmetic operations can be done in constant time

(60)

Time Complexity – Note

We are not completely accurate here

Numbers can have arbitrarily large values

The time needed to add twon-bit numbers depends onn

Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

Logarithmic cost modeltakes this into account, but is not used here We assume that arithmetic operations can be done in constant time

(61)

Time Complexity – Note

We are not completely accurate here

Numbers can have arbitrarily large values

The time needed to add twon-bit numbers depends onn

Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

Logarithmic cost modeltakes this into account, but is not used here

We assume that arithmetic operations can be done in constant time

(62)

Time Complexity – Note

We are not completely accurate here

Numbers can have arbitrarily large values

The time needed to add twon-bit numbers depends onn

Encoding of a floating point number does not directly correspond to its size Surely an addition is faster than a multiplication

Logarithmic cost modeltakes this into account, but is not used here We assume that arithmetic operations can be done in constant time

(63)

Time Complexity of Our Primality Test

Supposexis a prime number, encoded usingnbits

Number of loop iterations grows with size ofx≈2n Loop is iterated around2ntimes

We would like to count thefundamental operations Algorithm executes five operations per iteration In total roughly5·2noperations

We would like to know how time complexity behaves whenn grows Ignore constant5

(64)

Time Complexity of Our Primality Test

Supposexis a prime number, encoded usingnbits Number of loop iterations grows with size ofx≈2n Loop is iterated around2ntimes

We would like to count thefundamental operations Algorithm executes five operations per iteration In total roughly5·2noperations

We would like to know how time complexity behaves whenn grows Ignore constant5

(65)

Time Complexity of Our Primality Test

Supposexis a prime number, encoded usingnbits Number of loop iterations grows with size ofx≈2n Loop is iterated around2ntimes

We would like to count thefundamental operations Algorithm executes five operations per iteration In total roughly5·2noperations

We would like to know how time complexity behaves whenn grows Ignore constant5

(66)

Time Complexity of Our Primality Test

Supposexis a prime number, encoded usingnbits Number of loop iterations grows with size ofx≈2n Loop is iterated around2ntimes

We would like to count thefundamental operations Algorithm executes five operations per iteration In total roughly5·2noperations

We would like to know how time complexity behaves whenn grows Ignore constant5

(67)

Time Complexity of Algorithms

Asymptotic Upper Bounds

(68)

Asymptotic Upper Bounds

The exact time complexity can usually not be predicted even for small inputs We are interested inupper bounds

We consider the asymptotic behavior of the algorithm And ignore all constant factors

Example

Linear growth with gradient5is as good as linear growth with gradient1 Quadratic growth with coefficient10is as good as quadratic growth with coefficient1

(69)

Asymptotic Upper Bounds

The exact time complexity can usually not be predicted even for small inputs We are interested inupper bounds

We consider the asymptotic behavior of the algorithm And ignore all constant factors

Example

Linear growth with gradient5is as good as linear growth with gradient1 Quadratic growth with coefficient10is as good as quadratic growth with coefficient1

(70)

Asymptotic Upper Bounds

Big-ONotation

The set O(2n) contains all functions that do not grow faster thanc·2nfor some constantc

The setO(g(n)) contains all functions f(n) that do not grow faster than c·g(n)for some constantc, wheref andg are positive

Use asymptotic notation to specify the time complexity of algorithms

We writeO(n2)and mean that the algorithm behaves for largen liken2: when the input length is doubled, the time taken multiplies by four (at most)

(71)

Asymptotic Upper Bounds

Big-ONotation

The set O(2n) contains all functions that do not grow faster thanc·2nfor some constantc

The set O(g(n)) contains all functions f(n) that do not grow faster than c·g(n)for some constantc, wheref andg are positive

Use asymptotic notation to specify the time complexity of algorithms

We writeO(n2)and mean that the algorithm behaves for largen liken2: when the input length is doubled, the time taken multiplies by four (at most)

(72)

Asymptotic Upper Bounds

Big-ONotation

The set O(2n) contains all functions that do not grow faster thanc·2nfor some constantc

The set O(g(n)) contains all functions f(n) that do not grow faster than c·g(n)for some constantc, wheref andg are positive

Use asymptotic notation to specify the time complexity of algorithms

We writeO(n2)and mean that the algorithm behaves for largenliken2: when the input length is doubled, the time taken multiplies by four (at most)

(73)

Asymptotic Upper Bounds – Illustration

g(n) =n2

n

(74)

Asymptotic Upper Bounds – Illustration

g(n) =n2 f(n)∈ O(g(n))

n0 n

(75)

Asymptotic Upper Bounds – Illustration

g(n) =n2 f(n)∈ O(g(n))

h(n)∈ O(g(n))

n0 n

(76)

Asymptotic Upper Bounds – Examples

O(g(n)) ={f:N→R+|es existierenc >0undn0∈N so dass für allenn0 gilt, dassf(n)≤c·g(n)}

f(n) f ∈ O(?) Example 3n+ 4

O(n) c= 4, n0 = 4

2n

O(n) c= 2, n0 = 0

n2+ 100n

O(n2) c= 2, n0 = 100

n+√ n

O(n) c= 2, n0 = 1

(77)

Asymptotic Upper Bounds – Examples

O(g(n)) ={f:N→R+|es existierenc >0undn0∈N so dass für allenn0 gilt, dassf(n)≤c·g(n)}

f(n) f ∈ O(?) Example 3n+ 4 O(n) c= 4, n0 = 4 2n

O(n) c= 2, n0 = 0

n2+ 100n

O(n2) c= 2, n0 = 100

n+√ n

O(n) c= 2, n0 = 1

(78)

Asymptotic Upper Bounds – Examples

O(g(n)) ={f:N→R+|es existierenc >0undn0∈N so dass für allenn0 gilt, dassf(n)≤c·g(n)}

f(n) f ∈ O(?) Example 3n+ 4 O(n) c= 4, n0 = 4 2n O(n) c= 2, n0 = 0 n2+ 100n

O(n2) c= 2, n0 = 100

n+√ n

O(n) c= 2, n0 = 1

(79)

Asymptotic Upper Bounds – Examples

O(g(n)) ={f:N→R+|es existierenc >0undn0∈N so dass für allenn0 gilt, dassf(n)≤c·g(n)}

f(n) f ∈ O(?) Example 3n+ 4 O(n) c= 4, n0 = 4 2n O(n) c= 2, n0 = 0 n2+ 100n O(n2) c= 2, n0 = 100 n+√

n

O(n) c= 2, n0 = 1

(80)

Asymptotic Upper Bounds – Examples

O(g(n)) ={f:N→R+|es existierenc >0undn0∈N so dass für allenn0 gilt, dassf(n)≤c·g(n)}

f(n) f ∈ O(?) Example 3n+ 4 O(n) c= 4, n0 = 4 2n O(n) c= 2, n0 = 0 n2+ 100n O(n2) c= 2, n0 = 100 n+√

n O(n) c= 2, n0 = 1

(81)

Time Complexity of Algorithms

Time Complexity Analysis

(82)

Small n

1 2 3 4 5 6

0 20 40 60

logn n n2

n4 2n

(83)

Larger n

2 4 6 8 10 12 14 16 18 20 22 24

0 200 000 400 000 600 000 800 000

logn nn2 n4 2n

(84)

“Large” n

10 20 30 40 50 60 70 80 90 100

0 2·1019 4·1019 6·1019 8·1019 1·1020

logn nn2 n4 2n

(85)

Faster Primality Testing

First Attempt

(86)

Faster Primality Testing

Goal

Time complexity better thanΩ(2n)

Observation

Ifx is not divisible by2, then it also is not divisible by4,6,8, etc. We then only have to check odd numbers

Algorithm only has to test half the numbers Loop is only iterated aroundx/2times

(87)

Faster Primality Testing

Goal

Time complexity better thanΩ(2n) Observation

Ifx is not divisible by2, then it also is not divisible by4,6,8, etc.

We then only have to check odd numbers Algorithm only has to test half the numbers Loop is only iterated aroundx/2times

(88)

Faster Primality Testing

Goal

Time complexity better thanΩ(2n) Observation

Ifx is not divisible by2, then it also is not divisible by4,6,8, etc.

We then only have to check odd numbers Algorithm only has to test half the numbers Loop is only iterated aroundx/2times

(89)

Faster Primality Testing

def primetest2(x):

if x < 2 or (x > 2 and x % 2 == 0):

return False d = 3d = 3 while d < x:

if x % d == 0:

return False d += 2d += 2 return True

(90)

Faster Primality Testing

What is the gain?

Loop is iterated roughlyx/2times instead ofxtimes Time complexity improves by a factor of2

Again assumex is encoded usingnbits

Around5·2n/2 = 2.5·2nfundamental operations in total Time complexity is still inO(2n)

ï No asymptotic improvement

(91)

Faster Primality Testing

What is the gain?

Loop is iterated roughlyx/2times instead ofxtimes Time complexity improves by a factor of2

Again assumex is encoded usingnbits

Around5·2n/2 = 2.5·2nfundamental operations in total Time complexity is still inO(2n)

ï No asymptotic improvement

(92)

Faster Primality Testing

What is the gain?

Loop is iterated roughlyx/2times instead ofxtimes Time complexity improves by a factor of2

Again assumexis encoded usingnbits

Around5·2n/2 = 2.5·2nfundamental operations in total

Time complexity is still inO(2n) ï No asymptotic improvement

(93)

Faster Primality Testing

What is the gain?

Loop is iterated roughlyx/2times instead ofxtimes Time complexity improves by a factor of2

Again assumexis encoded usingnbits

Around5·2n/2 = 2.5·2nfundamental operations in total Time complexity is still inO(2n)

ï No asymptotic improvement

(94)

Faster Primality Testing

What is the gain?

Loop is iterated roughlyx/2times instead ofxtimes Time complexity improves by a factor of2

Again assumexis encoded usingnbits

Around5·2n/2 = 2.5·2nfundamental operations in total Time complexity is still inO(2n)

ï No asymptotic improvement

(95)

Faster Primality Testing

Second Attempt

(96)

Faster Primality Testing

Observation

Ifxwithx>2is not a prime number, thenxis divisible by a numberawith 1< a <x

Thenxis also divisible by a numberbwith

a·b=x and 1< b <x

It cannot be the case that

a >

x and b >x, since then

a·b >x

(97)

Faster Primality Testing

Observation

Ifxwithx>2is not a prime number, thenxis divisible by a numberawith 1< a <x

Thenxis also divisible by a numberbwith

a·b=x and 1< b <x

It cannot be the case that

a >

x and b >x, since then

a·b >x

(98)

Faster Primality Testing

Observation

Ifxwithx>2is not a prime number, thenxis divisible by a numberawith 1< a <x

Thenxis also divisible by a numberbwith

a·b=x and 1< b <x

It cannot be the case that

a >

x and b >x, since then

a·b >x

(99)

Faster Primality Testing

Including Modules

(100)

Including Modules

So far all functions have been defined in a single file

Modules

Distribute functions over multiple files Files cannot “see” each other

Functions can beimported Structured code

(101)

Including Modules

So far all functions have been defined in a single file Modules

Distribute functions over multiple files Files cannot “see” each other

Functions can beimported Structured code

(102)

Including Modules

Filefunctions.py

def square_root(n):

i = 1

while i * i < n: # Computer root of next larger square number i += 1

return i

Fileapplications.py

print(square_root(81))

(103)

Including Modules

Filefunctions.py

def square_root(n):

i = 1

while i * i < n: # Computer root of next larger square number i += 1

return i

Fileapplications.py

print(square_root(81))

(104)

Including Modules

Filefunctions.py

def square_root(n):

i = 1

while i * i < n: # Computer root of next larger square number i += 1

return i

Fileapplications.py

from functions import square_root print(square_root(81))

(105)

Including Modules

Filefunctions.py

def square_root(n):

i = 1

while i * i < n: # Computer root of next larger square number i += 1

return i

Fileapplications.py

from functions import * print(square_root(81))

(106)

Including Modules

A large number of modules already exists

For instance, there is a modulemathwhich includes a functionsqrt()to compute square roots

print(sqrt(9))

NameError: name ’sqrt’ is not defined

(107)

Including Modules

A large number of modules already exists

For instance, there is a modulemathwhich includes a functionsqrt()to compute square roots

print(sqrt(9))

NameError: name ’sqrt’ is not defined

(108)

Including Modules

A large number of modules already exists

For instance, there is a modulemathwhich includes a functionsqrt()to compute square roots

print(sqrt(9))

NameError: name ’sqrt’ is not defined

(109)

Including Modules

A large number of modules already exists

For instance, there is a modulemathwhich includes a functionsqrt()to compute square roots

from math import sqrt print(sqrt(9))

Output: 3

(110)

Faster Primality Testing

def primetest3(x):

if x < 2 or (x > 2 and x % 2 == 0):

return False d = 3

while d < x:

if x % d == 0:

return False d += 2

return True

(111)

Faster Primality Testing

from math import sqrt

def primetest3(x):

if x < 2 or (x > 2 and x % 2 == 0):

return False d = 3

wurzelx = sqrt(x) while d <= wurzelx:

if x % d == 0:

return False d += 2

return True

(112)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm? Loop is iterated√

x/2times Time complexity “grows” with√

x Time complexity is inO(√

2n) =O(2n/2) =O(1.415n)

(113)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm?

Loop is iterated√

x/2times Time complexity “grows” with√

x Time complexity is inO(√

2n) =O(2n/2) =O(1.415n)

(114)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm?

Loop is iterated√

x/2times

Time complexity “grows” with√ x Time complexity is inO(√

2n) =O(2n/2) =O(1.415n)

(115)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm?

Loop is iterated√

x/2times Time complexity “grows” with√

x

Time complexity is inO(√

2n) =O(2n/2) =O(1.415n)

(116)

Faster Primality Testing

What is the gain this time?

What is the time complexity of this algorithm?

Loop is iterated√

x/2times Time complexity “grows” with√

x Time complexity is inO(√

2n) =O(2n/2) =O(1.415n)

(117)

Faster Primality Testing

5 10 15 20 25 30 35 40 45

0 2 000 000 4 000 000 6 000 000 8 000 000 10 000 000

Referenzen

ÄHNLICHE DOKUMENTE

• Access to information on postcode addresses – and timely information on changed postcodes – is crucial to new entrants (and other users). • In many countries (save Sweden and

In particular, we discuss contributions of statistics to the field of artificial intelligence concerning methodological development, planning and design of studies, assessment of

Als aller erstes stellte Deutschland die zwei Logoentwürfe vor und alle Länder stimmten ab, welches ab sofort als offizielles Logo für unser Erasmus + Projekt

Wo du dich heute auch siehst, wir sind so froh, dass du hier bist, und wir hoffen, dass du im August dabei bist, um mehr über Mose und die Reise, die Gott für uns geplant hat,

(2004) Cape honeybee (Apis mellifera capensis Eschscholtz) and Varroa mite (Varroa de- structor Anderson &amp; Trueman) threats to honey- bees and beekeeping in Africa, Int...

In line with previous research, potentially BEM-eligible employees who reported that health promotion measures were implemented in their company in the last two years more

Hegel’s idea of subjectivity liberating itself in the course of history served, in Ferdinand Christian Baur’s “Christianity and the Christian Church,”17 as the paradigm for

Once we understand the real reason for using intentional predicates, we notice that there is no more reason for the assumption that the states which we ascribe by means of