fix(challenges): Minor changes to D challenges. Add E challenges
This commit is contained in:
@ -6,13 +6,16 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Implement basic element-wise matrix-matrix and scalar-matrix operations.</p><p>Implement:</p>
|
||||
<p>::* addition</p>
|
||||
<p>::* subtraction</p>
|
||||
<p>::* multiplication</p>
|
||||
<p>::* division</p>
|
||||
<p>::* exponentiation</p>
|
||||
<p>The first parameter will be the operation to be performed, for example : "m_add" for matrix addition and "s_add" for scalar addition. The second and third parameters will be the matrices on which the operations are to be performed.
|
||||
Implement basic element-wise matrix-matrix and scalar-matrix operations.
|
||||
<b>Implement:</b>
|
||||
<ul>
|
||||
<li>addition</li>
|
||||
<li>subtraction</li>
|
||||
<li>multiplication</li>
|
||||
<li>division</li>
|
||||
<li>exponentiation</li>
|
||||
</ul>
|
||||
The first parameter will be the operation to be performed, for example, "m_add" for matrix addition and "s_add" for scalar addition. The second and third parameters will be the matrices on which the operations are to be performed.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,13 +6,19 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>An emirp (prime spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.</p>
|
||||
<p>Write a function that should be able to : Show the first <b>n</b> eprimes numbers.Show the eprimes numbers in a range.Show the number of eprimes in a range.Show the <b>n<sup>th</sup></b> eprimes number.<p>The function should have two parameters. The first will receive <b>n</b> or the range as an array. The second will receive a boolean, that specifies if the function returns the eprimes as an array or a single number(the number of primes in the range or the <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array or a number.
|
||||
An emirp (<b>prime</b> spelled backwards) are primes that when reversed (in their decimal representation) are a different prime.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that:
|
||||
<ul>
|
||||
<li>Shows the first <b>n</b> emirp numbers.</li>
|
||||
<li>Shows the emirp numbers in a range.</li>
|
||||
<li>Shows the number of emirps in a range.</li>
|
||||
<li>Shows the <b>n<sup>th</sup></b> emirp number.</li>
|
||||
</ul>
|
||||
The function should accept two parameters. The first will receive <b>n</b> or the range as an array. The second will receive a boolean, that specifies if the function returns the emirps as an array or a single number (the number of primes in the range or the <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array or a number.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,9 +6,10 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Task:
|
||||
<p>Calculate the Shannon entropy H of a given input string.</p><p>Given the discreet random variable $X$ that is a string of $N$ "symbols" (total characters) consisting of $n$ different characters (n=2 for binary), the Shannon entropy of X in bits/symbol is :</p>
|
||||
<p>$H_2(X) = -\sum_{i=1}^n \frac{count_i}{N} \log_2 \left(\frac{count_i}{N}\right)$</p><p>where $count_i$ is the count of character $n_i$.</p>
|
||||
Calculate the Shannon entropy H of a given input string.
|
||||
Given the discreet random variable $X$ that is a string of $N$ "symbols" (total characters) consisting of $n$ different characters (n=2 for binary), the Shannon entropy of X in bits/symbol is:
|
||||
$H_2(X) = -\sum_{i=1}^n \frac{count_i}{N} \log_2 \left(\frac{count_i}{N}\right)$
|
||||
where $count_i$ is the count of character $n_i$.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,20 +6,33 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.</p>
|
||||
<p>For example, in a sequence <big>$A$</big>:</p><p>:::: <big>$A_0 = -7$</big></p>
|
||||
<p>:::: <big>$A_1 = 1$</big></p>
|
||||
<p>:::: <big>$A_2 = 5$</big></p>
|
||||
<p>:::: <big>$A_3 = 2$</big></p>
|
||||
<p>:::: <big>$A_4 = -4$</big></p>
|
||||
<p>:::: <big>$A_5 = 3$</big></p>
|
||||
<p>:::: <big>$A_6 = 0$</big></p><p>3 is an equilibrium index, because:</p><p>:::: <big>$A_0 + A_1 + A_2 = A_4 + A_5 + A_6$</big></p><p>6 is also an equilibrium index, because:</p><p>:::: <big>$A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0$</big></p><p>(sum of zero elements is zero)</p><p>7 is not an equilibrium index, because it is not a valid index of sequence <big>$A$</big>.</p>
|
||||
<p>Write a function that, given a sequence, returns its equilibrium indices (if any).</p><p>Assume that the sequence may be very long.</p>
|
||||
An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.
|
||||
For example, in a sequence <big>$A$</big>:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$A_0 = -7$</big></li>
|
||||
<li><big>$A_1 = 1$</big></li>
|
||||
<li><big>$A_2 = 5$</big></li>
|
||||
<li><big>$A_3 = 2$</big></li>
|
||||
<li><big>$A_4 = -4$</big></li>
|
||||
<li><big>$A_5 = 3$</big></li>
|
||||
<li><big>$A_6 = 0$</big></li>
|
||||
</ul>
|
||||
3 is an equilibrium index, because:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$A_0 + A_1 + A_2 = A_4 + A_5 + A_6$</big></li>
|
||||
</ul>
|
||||
6 is also an equilibrium index, because:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0$</big></li>
|
||||
</ul>
|
||||
(sum of zero elements is zero)
|
||||
7 is not an equilibrium index, because it is not a valid index of sequence <big>$A$</big>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that, given a sequence, returns its equilibrium indices (if any).
|
||||
Assume that the sequence may be very long.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,53 +6,65 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.</p>
|
||||
<p>Method: </p>
|
||||
Take two numbers to be multiplied and write them down at the top of two columns.
|
||||
In the left-hand column repeatedly halve the last number, discarding any remainders, and write the result below the last in the same column, until you write a value of 1.
|
||||
In the right-hand column repeatedly double the last number and write the result below. stop when you add a result in the same row as where the left hand column shows 1.
|
||||
Examine the table produced and discard any row where the value in the left column is even.
|
||||
Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together
|
||||
<p>For example: 17 × 34</p>
|
||||
<p>17 34</p>
|
||||
<p>Halving the first column:</p>
|
||||
<p>17 34</p>
|
||||
<p>8</p>
|
||||
<p>4</p>
|
||||
<p>2</p>
|
||||
<p>1</p>
|
||||
<p>Doubling the second column:</p>
|
||||
<p>17 34</p>
|
||||
<p>8 68</p>
|
||||
<p>4 136</p>
|
||||
<p>2 272</p>
|
||||
<p>1 544</p>
|
||||
<p>Strike-out rows whose first cell is even:</p>
|
||||
<p>17 34</p>
|
||||
<p>8 <strike>68</strike></p>
|
||||
<p>4 <strike>136</strike></p>
|
||||
<p>2 <strike>272</strike></p>
|
||||
<p>1 544</p>
|
||||
<p>Sum the remaining numbers in the right-hand column:</p>
|
||||
<p>17 34</p>
|
||||
<p>8 --</p>
|
||||
<p>4 ---</p>
|
||||
<p>2 ---</p>
|
||||
<p>1 544</p>
|
||||
<p>====</p>
|
||||
<p>578</p>
|
||||
<p>So 17 multiplied by 34, by the Ethiopian method is 578.</p>
|
||||
Task:
|
||||
<p>The task is to define three named functions/methods/procedures/subroutines:</p>
|
||||
one to halve an integer,
|
||||
one to double an integer, and
|
||||
one to state if an integer is even.
|
||||
<p>Use these functions to create a function that does Ethiopian multiplication.</p>
|
||||
Ethiopian multiplication is a method of multiplying integers using only addition, doubling, and halving.
|
||||
<b>Method:</b>
|
||||
<ol>
|
||||
<li>Take two numbers to be multiplied and write them down at the top of two columns.</li>
|
||||
<li>In the left-hand column repeatedly halve the last number, discarding any remainders, and write the result below the last in the same column, until you write a value of 1.</li>
|
||||
<li>In the right-hand column repeatedly double the last number and write the result below. stop when you add a result in the same row as where the left hand column shows 1.</li>
|
||||
<li>Examine the table produced and discard any row where the value in the left column is even.</li>
|
||||
<li>Sum the values in the right-hand column that remain to produce the result of multiplying the original two numbers together</li>
|
||||
</ol>
|
||||
<b>For example:</b> 17 × 34
|
||||
<pre>
|
||||
17 34
|
||||
</pre>
|
||||
Halving the first column:
|
||||
<pre>
|
||||
17 34
|
||||
8
|
||||
4
|
||||
2
|
||||
1
|
||||
</pre>
|
||||
Doubling the second column:
|
||||
<pre>
|
||||
17 34
|
||||
8 68
|
||||
4 136
|
||||
2 272
|
||||
1 544
|
||||
</pre>
|
||||
Strike-out rows whose first cell is even:
|
||||
<pre>
|
||||
17 34
|
||||
8 <strike>68</strike>
|
||||
4 <strike>136</strike>
|
||||
2 <strike>272</strike>
|
||||
1 544
|
||||
</pre>
|
||||
Sum the remaining numbers in the right-hand column:
|
||||
<pre>
|
||||
17 34
|
||||
8 --
|
||||
4 ---
|
||||
2 ---
|
||||
1 544
|
||||
====
|
||||
578
|
||||
</pre>
|
||||
So 17 multiplied by 34, by the Ethiopian method is 578.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
The task is to define three named functions/methods/procedures/subroutines:
|
||||
<ol>
|
||||
<li>one to halve an integer,</li>
|
||||
<li>one to double an integer, and</li>
|
||||
<li>one to state if an integer is even.</li>
|
||||
</ol>
|
||||
Use these functions to create a function that does Ethiopian multiplication.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,28 +6,64 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value. It is an explicit method for solving initial value problems (IVPs), as described in <a href="https://en.wikipedia.org/wiki/Euler method" title="wp: Euler method">the wikipedia page</a>.</p><p>The ODE has to be provided in the following form:</p><p>:: <big>$\frac{dy(t)}{dt} = f(t,y(t))$</big></p><p>with an initial value</p><p>:: <big>$y(t_0) = y_0$</big></p><p>To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:</p><p>:: <big>$\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</big></p><p>then solve for $y(t+h)$:</p><p>:: <big>$y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</big></p><p>which is the same as</p><p>:: <big>$y(t+h) \approx y(t) + h \, f(t,y(t))$</big></p><p>The iterative solution rule is then:</p><p>:: <big>$y_{n+1} = y_n + h \, f(t_n, y_n)$</big></p><p>where <big>$h$</big> is the step size, the most relevant parameter for accuracy of the solution. A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.</p>
|
||||
<p>Example: Newton's Cooling Law</p><p>Newton's cooling law describes how an object of initial temperature <big>$T(t_0) = T_0$</big> cools down in an environment of temperature <big>$T_R$</big>:</p><p>:: <big>$\frac{dT(t)}{dt} = -k \, \Delta T$</big></p>
|
||||
<p>or</p>
|
||||
<p>:: <big>$\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$</big></p>
|
||||
<p>It says that the cooling rate <big>$\frac{dT(t)}{dt}$</big> of the object is proportional to the current temperature difference <big>$\Delta T = (T(t) - T_R)$</big> to the surrounding environment.</p><p>The analytical solution, which we will compare to the numerical approximation, is</p>
|
||||
<p>:: <big>$T(t) = T_R + (T_0 - T_R) \; e^{-k t}$</big></p>
|
||||
Task:
|
||||
<p>Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of:</p>
|
||||
<p>::* 2 s</p>
|
||||
<p>::* 5 s and </p>
|
||||
<p>::* 10 s </p>
|
||||
<p>and to compare with the analytical solution.</p>
|
||||
Initial values:
|
||||
<p>::* initial temperature <big>$T_0$</big> shall be 100 °C</p>
|
||||
<p>::* room temperature <big>$T_R$</big> shall be 20 °C</p>
|
||||
<p>::* cooling constant <big>$k$</big> shall be 0.07 </p>
|
||||
<p>::* time interval to calculate shall be from 0 s ──► 100 s</p>
|
||||
Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value. It is an explicit method for solving initial value problems (IVPs), as described in <a href="https://en.wikipedia.org/wiki/Euler method" title="wp: Euler method" target="_blank">the wikipedia page</a>.
|
||||
The ODE has to be provided in the following form:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$\frac{dy(t)}{dt} = f(t,y(t))$</big></li>
|
||||
</ul>
|
||||
with an initial value
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$y(t_0) = y_0$</big></li>
|
||||
</ul>
|
||||
To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</big></li>
|
||||
</ul>
|
||||
then solve for $y(t+h)$:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</big></li>
|
||||
</ul>
|
||||
which is the same as
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$y(t+h) \approx y(t) + h \, f(t,y(t))$</big></li>
|
||||
</ul>
|
||||
The iterative solution rule is then:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$y_{n+1} = y_n + h \, f(t_n, y_n)$</big></li>
|
||||
</ul>
|
||||
where <big>$h$</big> is the step size, the most relevant parameter for accuracy of the solution. A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.
|
||||
<b>Example: Newton's Cooling Law</b>
|
||||
Newton's cooling law describes how an object of initial temperature <big>$T(t_0) = T_0$</big> cools down in an environment of temperature <big>$T_R$</big>:
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$\frac{dT(t)}{dt} = -k \, \Delta T$</big></li>
|
||||
</ul>
|
||||
or
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$</big></li>
|
||||
</ul>
|
||||
It says that the cooling rate <big>$\frac{dT(t)}{dt}$</big> of the object is proportional to the current temperature difference <big>$\Delta T = (T(t) - T_R)$</big> to the surrounding environment.
|
||||
The analytical solution, which we will compare to the numerical approximation, is
|
||||
<ul style="list-style: none;">
|
||||
<li><big>$T(t) = T_R + (T_0 - T_R) \; e^{-k t}$</big></li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of:
|
||||
<ul>
|
||||
<li>2 s</li>
|
||||
<li>5 s and</li>
|
||||
<li>10 s</li>
|
||||
</ul>
|
||||
and to compare with the analytical solution.
|
||||
<b>Initial values:</b>
|
||||
<ul>
|
||||
<li>initial temperature <big>$T_0$</big> shall be 100 °C</li>
|
||||
<li>room temperature <big>$T_R$</big> shall be 20 °C</li>
|
||||
<li>cooling constant <big>$k$</big> shall be 0.07</li>
|
||||
<li>time interval to calculate shall be from 0s to 100 s</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,7 +6,8 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Write a function to calculate the binomial coefficient for the given value of n and k.</p><p>This formula is recommended:</p>
|
||||
Write a function to calculate the binomial coefficient for the given value of n and k.
|
||||
This formula is recommended:
|
||||
$\binom{n}{k} = \frac{n!}{(n-k)!k!} = \frac{n(n-1)(n-2)\ldots(n-k+1)}{k(k-1)(k-2)\ldots 1}$
|
||||
</section>
|
||||
|
||||
|
@ -6,45 +6,54 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Task:
|
||||
<p>Create an interpreter for a <a href="https://en.wikipedia.org/wiki/Markov algorithm" title="wp: Markov algorithm">Markov Algorithm</a>.</p><p>Rules have the syntax:</p>
|
||||
<p><ruleset> ::= ((<comment> | <rule>) <newline>+)*</p>
|
||||
<p><comment> ::= # {<any character>}</p>
|
||||
<p><rule> ::= <pattern> <whitespace> -> <whitespace> [.] <replacement></p>
|
||||
<p><whitespace> ::= (<tab> | <space>) [<whitespace>]</p>
|
||||
<p>There is one rule per line.</p><p>If there is a <b>.</b> (period) present before the <replacement>, then this is a terminating rule in which case the interpreter must halt execution.</p><p>A ruleset consists of a sequence of rules, with optional comments.</p>
|
||||
<p><big><big> Rulesets </big></big></p><p>Use the following tests on entries:</p>
|
||||
Ruleset 1:
|
||||
Create an interpreter for a <a href="https://en.wikipedia.org/wiki/Markov algorithm" title="wp: Markov algorithm" target="_blank">Markov Algorithm</a>.
|
||||
Rules have the syntax:
|
||||
<pre>
|
||||
This rules file is extracted from Wikipedia:
|
||||
http://en.wikipedia.org/wiki/Markov_AlgorithmA -> apple
|
||||
[ruleset] ::= (([comment] | [rule]) [newline]+)*
|
||||
[comment] ::= # {[any character]}
|
||||
[rule] ::= [pattern] [whitespace] -> [whitespace] [.] [replacement]
|
||||
[whitespace] ::= ([tab] | [space]) [[whitespace]]
|
||||
</pre>
|
||||
There is one rule per line.
|
||||
If there is a <b>.</b> (period) present before the [replacement], then this is a terminating rule in which case the interpreter must halt execution.
|
||||
A ruleset consists of a sequence of rules, with optional comments.
|
||||
<big><big>Rulesets</big></big>
|
||||
Use the following tests on entries:
|
||||
<b>Ruleset 1:</b>
|
||||
<pre>
|
||||
# This rules file is extracted from Wikipedia:
|
||||
# http://en.wikipedia.org/wiki/Markov_Algorithm
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
<p>Sample text of:</p>
|
||||
<p> <code> I bought a B of As from T S. </code></p>
|
||||
<p>Should generate the output:</p>
|
||||
<p> <code> I bought a bag of apples from my brother. </code></p>
|
||||
Ruleset 2:
|
||||
<p>A test of the terminating rule</p>
|
||||
Sample text of:
|
||||
<code>I bought a B of As from T S.</code>
|
||||
Should generate the output:
|
||||
<code>I bought a bag of apples from my brother.</code>
|
||||
<b>Ruleset 2:</b>
|
||||
A test of the terminating rule
|
||||
<pre>
|
||||
Slightly modified from the rules on WikipediaA -> apple
|
||||
# Slightly modified from the rules on Wikipedia
|
||||
A -> apple
|
||||
B -> bag
|
||||
S -> .shop
|
||||
T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule</pre>
|
||||
<p>Sample text of:</p>
|
||||
<p> <code>I bought a B of As from T S.</code></p>
|
||||
<p>Should generate:</p>
|
||||
<p> <code>I bought a bag of apples from T shop.</code></p>
|
||||
Ruleset 3:
|
||||
<p>This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.</p>
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
Sample text of:
|
||||
<code>I bought a B of As from T S.</code>
|
||||
Should generate:
|
||||
<code>I bought a bag of apples from T shop.</code>
|
||||
<b>Ruleset 3:</b>
|
||||
This tests for correct substitution order and may trap simple regexp based replacement routines if special regexp characters are not escaped.
|
||||
<pre>
|
||||
BNF Syntax testing rulesA -> apple
|
||||
#BNF Syntax testing rules
|
||||
A -> apple
|
||||
WWWW -> with
|
||||
Bgage -> ->.*
|
||||
B -> bag
|
||||
@ -55,22 +64,24 @@ T -> the
|
||||
the shop -> my brother
|
||||
a never used -> .terminating rule
|
||||
</pre>
|
||||
<p>Sample text of:</p>
|
||||
<p> <code>I bought a B of As W my Bgage from T S.</code></p>
|
||||
<p>Should generate:</p>
|
||||
<p> <code>I bought a bag of apples with my money from T shop.</code></p>
|
||||
Ruleset 4:
|
||||
<p>This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. It implements a general unary multiplication engine. (Note that the input expression must be placed within underscores in this implementation.)</p>
|
||||
Sample text of:
|
||||
<code>I bought a B of As W my Bgage from T S.</code>
|
||||
Should generate:
|
||||
<code>I bought a bag of apples with my money from T shop.</code>
|
||||
<b>Ruleset 4:</b>
|
||||
This tests for correct order of scanning of rules, and may trap replacement routines that scan in the wrong order. It implements a general unary multiplication engine. (Note that the input expression must be placed within underscores in this implementation.)
|
||||
<pre>
|
||||
## Unary Multiplication Engine, for testing Markov Algorithm implementations
|
||||
## By Donal Fellows.
|
||||
Unary addition engine_+1 -> _1+
|
||||
### Unary Multiplication Engine, for testing Markov Algorithm implementations
|
||||
### By Donal Fellows.
|
||||
# Unary addition engine
|
||||
_+1 -> _1+
|
||||
1+1 -> 11+
|
||||
Pass for converting from the splitting of multiplication into ordinary
|
||||
addition1! -> !1
|
||||
# Pass for converting from the splitting of multiplication into ordinary
|
||||
# addition
|
||||
1! -> !1
|
||||
,! -> !+
|
||||
_! -> _
|
||||
Unary multiplication by duplicating left side, right side times1*1 -> x,@y
|
||||
# Unary multiplication by duplicating left side, right side times1*1 -> x,@y
|
||||
1x -> xX
|
||||
X, -> 1,1
|
||||
X1 -> 1X
|
||||
@ -78,39 +89,46 @@ _x -> _X
|
||||
,x -> ,X
|
||||
y1 -> 1y
|
||||
y_ -> _
|
||||
Next phase of applying1@1 -> x,@y
|
||||
# Next phase of applying1@1 -> x,@y
|
||||
1@_ -> @_
|
||||
,@_ -> !_
|
||||
++ -> +
|
||||
Termination cleanup for addition_1 -> 1
|
||||
# Termination cleanup for addition_1 -> 1
|
||||
1+_ -> 1
|
||||
_+_ ->
|
||||
</pre>
|
||||
<p>Sample text of:</p>
|
||||
<p> <code> _1111*11111_ </code></p>
|
||||
<p>should generate the output:</p>
|
||||
<p> <code> 11111111111111111111 </code></p>
|
||||
Ruleset 5:
|
||||
<p>A simple <a href="http://en.wikipedia.org/wiki/Turing_machine" title="link: http://en.wikipedia.org/wiki/Turing_machine">Turing machine</a>,</p>
|
||||
<p>implementing a three-state <a href="http://en.wikipedia.org/wiki/Busy_beaver" title="link: http://en.wikipedia.org/wiki/Busy_beaver">busy beaver</a>.</p><p>The tape consists of 0s and 1s, the states are A, B, C and H (for Halt), and the head position is indicated by writing the state letter before the character where the head is.</p>
|
||||
<p>All parts of the initial tape the machine operates on have to be given in the input.</p><p>Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.</p>
|
||||
Sample text of:
|
||||
<code>_1111*11111_</code>
|
||||
should generate the output:
|
||||
<code>11111111111111111111</code>
|
||||
<b>Ruleset 5:</b>
|
||||
A simple <a href="http://en.wikipedia.org/wiki/Turing_machine" title="link: http://en.wikipedia.org/wiki/Turing_machine" target="_blank">Turing machine</a>,
|
||||
implementing a three-state <a href="http://en.wikipedia.org/wiki/Busy_beaver" title="link: http://en.wikipedia.org/wiki/Busy_beaver" target="_blank">busy beaver</a>.The tape consists of <b>0</b>s and <b>1</b>s, the states are <b>A</b>, <b>B</b>, <b>C</b> and <b>H</b> (for <b>H</b>alt), and the head position is indicated by writing the state letter before the character where the head is. All parts of the initial tape the machine operates on have to be given in the input.
|
||||
Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.
|
||||
<pre>
|
||||
Turing machine: three-state busy beaver
|
||||
# state A, symbol 0 => write 1, move right, new state BA0 -> 1B
|
||||
state A, symbol 1 => write 1, move left, new state C0A1 -> C01
|
||||
#Turing machine: three-state busy beaver
|
||||
#
|
||||
# state A, symbol 0 => write 1, move right, new state B
|
||||
A0 -> 1B
|
||||
# state A, symbol 1 => write 1, move left, new state C
|
||||
0A1 -> C01
|
||||
1A1 -> C11
|
||||
state B, symbol 0 => write 1, move left, new state A0B0 -> A01
|
||||
# state B, symbol 0 => write 1, move left, new state A
|
||||
0B0 -> A01
|
||||
1B0 -> A11
|
||||
state B, symbol 1 => write 1, move right, new state BB1 -> 1B
|
||||
state C, symbol 0 => write 1, move left, new state B0C0 -> B01
|
||||
# state B, symbol 1 => write 1, move right, new state B
|
||||
B1 -> 1B
|
||||
# state C, symbol 0 => write 1, move left, new state B
|
||||
0C0 -> B01
|
||||
1C0 -> B11
|
||||
state C, symbol 1 => write 1, move left, halt0C1 -> H01
|
||||
# state C, symbol 1 => write 1, move left, halt
|
||||
0C1 -> H01
|
||||
1C1 -> H11
|
||||
</pre>
|
||||
<p>This ruleset should turn</p>
|
||||
<p> <code> 000000A000000 </code></p>
|
||||
<p>into</p>
|
||||
<p> <code> 00011H1111000 </code></p>
|
||||
This ruleset should turn
|
||||
<code>000000A000000</code>
|
||||
into
|
||||
<code>00011H1111000</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,30 +6,22 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Write a function to implement a Brain**** interpreter. The function will take a string as a parameter and should return a string as the output. More details are given below : </p>
|
||||
<p>RCBF is a set of <a href="http://rosettacode.org/wiki/Brainf***" title="Brainf***">Brainf***</a> compilers and interpreters written for Rosetta Code in a variety of languages.</p><p>Below are links to each of the versions of RCBF.</p><p>An implementation need only properly implement the following instructions:</p>
|
||||
<p>{|</p>
|
||||
<p>!Command</p>
|
||||
<p>!Description</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>></code> || Move the pointer to the right</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code><</code> || Move the pointer to the left</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>+</code> || Increment the memory cell under the pointer</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>-</code> || Decrement the memory cell under the pointer</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>.</code> || Output the character signified by the cell at the pointer</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>,</code> || Input a character and store it in the cell at the pointer</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>[</code> || Jump past the matching <code>]</code> if the cell under the pointer is 0</p>
|
||||
<p>|-</p>
|
||||
<p>| style="text-align:center"| <code>]</code> || Jump back to the matching <code>[</code> if the cell under the pointer is nonzero</p>
|
||||
<p>|}</p>
|
||||
<p>Any cell size is allowed, EOF (<u>E</u>nd-<u>O</u>-<u>F</u>ile) support is optional, as is whether you have bounded or unbounded memory.</p>
|
||||
Write a function to implement a Brain**** interpreter. The function will take a string as a parameter and should return a string as the output. More details are given below:
|
||||
RCBF is a set of <a href="http://rosettacode.org/wiki/Brainf***" title="Brainf***">Brainf***</a> compilers and interpreters written for Rosetta Code in a variety of languages.
|
||||
Below are links to each of the versions of RCBF.
|
||||
An implementation need only properly implement the following instructions:
|
||||
|
||||
| Command | Description |
|
||||
| --- | --- |
|
||||
| <code>></code> | Move the pointer to the right |
|
||||
| <code><</code> | Move the pointer to the left |
|
||||
| <code>+</code> | Increment the memory cell under the pointer |
|
||||
| <code>-</code> | Decrement the memory cell under the pointer |
|
||||
| <code>.</code> | Output the character signified by the cell at the pointer |
|
||||
| <code>,</code> | Input a character and store it in the cell at the pointer |
|
||||
| <code>[</code> | Jump past the matching <code>]</code> if the cell under the pointer is 0 |
|
||||
| <code>]</code> | Jump back to the matching <code>[</code> if the cell under the pointer is nonzero |
|
||||
Any cell size is allowed, EOF (<u>E</u>nd-<u>O</u>-<u>F</u>ile) support is optional, as is whether you have bounded or unbounded memory.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,7 +6,15 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime.</p> The generator should be able to : Show the first <b>n</b> prime numbers.Show the prime numbers in a range.Show the number of primes in a range.Show the <b>n<sup>th</sup></b> prime number.<p>The function should have two parameters. The first will receive <b>n</b> or the range as an array. The second will receive a boolean, that specifies if the function returns the prime numbers as an array or a single number(the number of primes in the range or the <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array.
|
||||
Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime.
|
||||
The generator should be able to:
|
||||
<ul>
|
||||
<li>Show the first <b>n</b> prime numbers.</li>
|
||||
<li>Show the prime numbers in a range.</li>
|
||||
<li>Show the number of primes in a range.</li>
|
||||
<li>Show the <b>n<sup>th</sup></b> prime number.</li>
|
||||
</ul>
|
||||
The function should have two parameters. The first will receive <b>n</b> or the range as an array. The second will receive a boolean, that specifies if the function returns the prime numbers as an array or a single number(the number of primes in the range or the <b>n<sup>th</sup></b> prime). According to the parameters the function should return an array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
Reference in New Issue
Block a user