Search
Procedural Control Flow
Untitled (the Wolfram Language for Students - Personal Use Only : www.wolfram.com)

Procedural control flow

Mathematica also has procedural control flow (perhaps more familiar from e.g. Julia)

Procedural_Control_Flow_1.gif

Procedural_Control_Flow_2.png

Procedural_Control_Flow_3.png

Which can be used for many if-else clauses

Procedural_Control_Flow_4.png

Procedural_Control_Flow_5.png

The simplest loop is the Do loop, which repeats the contents n times

Procedural_Control_Flow_6.png

Procedural_Control_Flow_7.png

Procedural_Control_Flow_8.png

Procedural_Control_Flow_9.png

Procedural_Control_Flow_10.png

Procedural_Control_Flow_11.png

Procedural_Control_Flow_12.png

Procedural_Control_Flow_13.png

Procedural_Control_Flow_14.png

Procedural_Control_Flow_15.png

Procedural_Control_Flow_16.png

We also have traditional for loops

Procedural_Control_Flow_17.png

Procedural_Control_Flow_18.png

Procedural_Control_Flow_19.png

Procedural_Control_Flow_20.png

Procedural_Control_Flow_21.png

Procedural_Control_Flow_22.png

Procedural_Control_Flow_23.png

Procedural_Control_Flow_24.png

Procedural_Control_Flow_25.png

Procedural_Control_Flow_26.png

Procedural_Control_Flow_27.png

Procedural_Control_Flow_28.png

Procedural_Control_Flow_29.png

Procedural_Control_Flow_30.png

Procedural_Control_Flow_31.png

Procedural_Control_Flow_32.png

Procedural_Control_Flow_33.png

Procedural_Control_Flow_34.png

Procedural_Control_Flow_35.png

Procedural_Control_Flow_36.png

Procedural_Control_Flow_37.png

Application: merge sort

Merge sort is a recursive algorithm
Given a list, we:
1. find a midpoint
2. split the list at its midpoint into a left list and right list
3. sort the left and right lists using merge sort (recursion)
4. merge the two lists

Procedural_Control_Flow_38.png

Procedural_Control_Flow_39.png

Procedural_Control_Flow_40.png

Procedural_Control_Flow_41.png

Procedural_Control_Flow_42.png

Procedural_Control_Flow_43.png

The runtime complexity of merge sort is given by the recurrence
t(n)=2t(n/2)+O(n)

Procedural_Control_Flow_44.png

Procedural_Control_Flow_45.png

The runtime complexity is n log(n)

Functional-style merge sort

Procedural_Control_Flow_46.gif

Procedural_Control_Flow_47.png

Procedural_Control_Flow_48.png

Finding all primes less than n

Algorithm known as the Sieve of Eratosthenes

Choose a number n
We will find all primes less than n
1. List all numbers 2 through n
2. Starting with, p=2, mark all multiples of p, beginning with Procedural_Control_Flow_49.png
    i.e. mark Procedural_Control_Flow_50.png up to n
3. Repeat step 2 with p the next unmarked number
4. Terminate when Procedural_Control_Flow_51.png

Procedural_Control_Flow_52.png

Procedural_Control_Flow_53.png

Procedural_Control_Flow_54.png

Let’s check our work...

Procedural_Control_Flow_55.png

Procedural_Control_Flow_56.png

We can also use a functional approach

Procedural_Control_Flow_57.png

Procedural_Control_Flow_58.png

Procedural_Control_Flow_59.png