19 uploaded to KEATS, which you can freely use.\bigskip |
19 uploaded to KEATS, which you can freely use.\bigskip |
20 |
20 |
21 |
21 |
22 \subsubsection*{Part 1 (3 Marks)} |
22 \subsubsection*{Part 1 (3 Marks)} |
23 |
23 |
24 \subsubsection*{Part 2 (3 Marks)} |
24 This part is about recursion. You are asked to implement a Scala |
|
25 program that tests examples of the \emph{$3n + 1$-conjecture}. This |
|
26 conjecture can be described as follows: Start with any positive number |
|
27 $n$ greater than $0$. If $n$ is even, divide it by $2$ to obtain $n / |
|
28 2$. If $n$ is odd, multiply it by $3$ and add $1$ to obtain $3n + |
|
29 1$. Repeat this process and you will always end up with $1$. For |
|
30 example if you start with $6$, respectively $9$, you obtain the series |
25 |
31 |
26 \subsubsection*{Part 3 (4 Marks)} |
32 \[ |
|
33 \begin{array}{@{}l@{\hspace{5mm}}l@{}} |
|
34 6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(=9 steps)}\\ |
|
35 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 & \text{(=20 steps)}\\ |
|
36 \end{array} |
|
37 \] |
|
38 |
|
39 \noindent |
|
40 As you can see, the numbers go up and down like a roller-coaster, but |
|
41 curiously they seem to always terminate in $1$. While it is |
|
42 relatively easy to test this conjecture with particular numbers, it is |
|
43 an interesting open problem to \emph{prove} that the conjecture is |
|
44 true for all numbers ($> 0$). Paul Erd\"o{}s, a famous mathematician |
|
45 you might have hard about, said about this conjecture: ``Mathematics |
|
46 may not be ready for such problems.'' and also offered \$500 cash |
|
47 prize for its solution. Jeffrey Lagarias, another mathematician, |
|
48 claimed that based only on known information about this problem, |
|
49 ``this is an extraordinarily difficult problem, completely out of |
|
50 reach of present day mathematics.'' There is also a |
|
51 \href{https://xkcd.com/710/}{xkcd} cartoon about this |
|
52 conjecture.\medskip |
|
53 |
|
54 \noindent |
|
55 \textbf{Tasks:} (1) You are asked to implement a recursive function that |
|
56 calculates the number of steps needed number until a series ends with |
|
57 $1$. In case of starting with $6$, it takes $9$ steps and in case of |
|
58 starting with $9$, it takes $20$ (see above). In order to try out this |
|
59 function with large numbers, you should use \texttt{Long} as argument |
|
60 type instead of \texttt{Int}. You can assume this function will be |
|
61 called with numbers between $1$ and $10$ million. |
|
62 |
|
63 (2) Then write a second function that takes an upper bound as argument |
|
64 and calculates the steps for all numbers in the range from 1 upto this |
|
65 bound, and returns the maximum of steps needed by a number in that |
|
66 range. This function should produce for ranges |
|
67 |
|
68 \begin{itemize} |
|
69 \item $1 - 10$ where $9$ takes 20 steps |
|
70 \item $1 - 100$ where $97$ takes 119 steps, |
|
71 \item $1 - 1,000$ where $871$ takes 179 steps, |
|
72 \item $1 - 10,000$ where $6,171$ takes 262 steps, |
|
73 \item $1 - 100,000$ where $77,031$ takes 351 steps, |
|
74 \item $1 - 1$ million where $837,799$ takes 525 steps, and |
|
75 \item $1 - 10$ million where $8,400,511$ takes 686 steps |
|
76 \end{itemize} |
|
77 |
|
78 |
|
79 \subsubsection*{Part 2 (4 Marks)} |
|
80 |
|
81 This part is about list processing---it's a variant of |
|
82 Buy-low-sell-high in Scala. (1) Given a list of prices for a commodity, |
|
83 for example |
|
84 |
|
85 \[ |
|
86 \texttt{List(28.0, 18.0, 20.0, 26.0, 24.0)} |
|
87 \] |
|
88 |
|
89 \noindent |
|
90 you need to write a function that returns a pair for when to buy and |
|
91 when to sell this commodity. In the example above it should return |
|
92 $\texttt{(1, 3)}$ because at index $1$ the price is lowest and then at |
|
93 index $3$ the price is highest. Note the prices are given as lists of |
|
94 \texttt{Float}s. |
|
95 |
|
96 (2) Write a function that requests a comma-separated value list from a |
|
97 Yahoo websevice that provides historical data for stock indices. For |
|
98 example if you query the URL |
|
99 |
|
100 \begin{center} |
|
101 \url{http://ichart.yahoo.com/table.csv?s=GOOG} |
|
102 \end{center} |
|
103 |
|
104 \noindent where \texttt{GOOG} stands for Google's stock market symbol |
|
105 then you will receive a comma-separated value list of the daily stock |
|
106 prices since Google was floated. You can also try this with other |
|
107 strock market symbols, for instance AAPL, MSFT, IBM, FB, YHOO, AMZN, |
|
108 BIDU and so on. This function should return a List of strings, where |
|
109 each string is one line in this comma-separated value list |
|
110 (representing one days data). Note that Yahoo generates its answer |
|
111 such that the newest data is at the front of this list, and the oldest |
|
112 data is at the end. |
|
113 |
|
114 (3) As you can see, the financial data from Yahoo is organised in 7 columns, |
|
115 for example |
|
116 |
|
117 {\small\begin{verbatim} |
|
118 Date,Open,High,Low,Close,Volume,Adj Close |
|
119 2016-11-04,750.659973,770.359985,750.560974,762.02002,2126900,762.02002 |
|
120 2016-11-03,767.25,769.950012,759.030029,762.130005,1914000,762.130005 |
|
121 2016-11-02,778.200012,781.650024,763.450012,768.700012,1872400,768.700012 |
|
122 2016-11-01,782.890015,789.48999,775.539978,783.609985,2404500,783.609985 |
|
123 .... |
|
124 \end{verbatim}} |
|
125 |
|
126 \noindent |
|
127 Write a function that ignores the first line (the header) and then |
|
128 extracts from each line the date (first column) and the Adjusted Close |
|
129 price (last column). The Adjusted Close price should be converted into |
|
130 a float. So the result of this function is a list of pairs where the |
|
131 first components are strings (the dates) and the second are floats |
|
132 (the adjusted close prices). |
|
133 |
|
134 (4) Write a function that takes a stock market symbol as argument (you |
|
135 can assume it is a valid one, like GOOG, AAPL, MSFT, IBM, FB, YHOO, |
|
136 AMZN, BIDU). The function calculates the dates when you should have |
|
137 bought Google shares (lowest price) and when you should have sold them |
|
138 (highest price).\medskip |
|
139 |
|
140 \noindent |
|
141 \textbf{Test Data:} |
|
142 In case of Google, the financial data records 3077 entries starting |
|
143 from 2004-08-19 until 2016-11-04 (which is the last entry on the day |
|
144 when I prepared the course work...on 6 November; remember stock |
|
145 markets are typically closed on weekends and no financial data is |
|
146 produced then; also I did not count the header line). The lowest |
|
147 for Goole was on 2004-09-03 with \$49.95513 per share and the |
|
148 highest on 2016-10-24 with \$813.109985 per share. |
|
149 |
|
150 |
|
151 |
|
152 \subsubsection*{Part 3 (3 Marks)} |
27 |
153 |
28 \end{document} |
154 \end{document} |
29 |
155 |
30 %%% Local Variables: |
156 %%% Local Variables: |
31 %%% mode: latex |
157 %%% mode: latex |