fix(challenges): H problems
This commit is contained in:
@ -6,21 +6,27 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>The Hailstone sequence of numbers can be generated from a starting positive integer, n by:</p>
|
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
|
||||||
If n is 1 then the sequence ends.
|
<ul>
|
||||||
If n is even then the next n of the sequence <code> = n/2 </code>
|
<li>If n is <b>1</b> then the sequence ends.</li>
|
||||||
If n is odd then the next n of the sequence <code> = (3 * n) + 1 </code><p>The (unproven) <a href="https://en.wikipedia.org/wiki/Collatz conjecture" title="wp: Collatz conjecture">Collatz conjecture</a> is that the hailstone sequence for any starting number always terminates.</p>
|
<li>If n is <b>even</b> then the next n of the sequence <code>= n/2</code></li>
|
||||||
<p>The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.</p>
|
<li>If n is <b>odd</b> then the next n of the sequence <code>= (3 * n) + 1</code></li>
|
||||||
Task:
|
</ul>
|
||||||
Create a routine to generate the hailstone sequence for a number.
|
The (unproven) <a href="https://en.wikipedia.org/wiki/Collatz conjecture" title="wp: Collatz conjecture" target="_blank">Collatz conjecture</a> is that the hailstone sequence for any starting number always terminates.
|
||||||
Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code>
|
The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.
|
||||||
Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)See also:
|
|
||||||
<a href="http://xkcd.com/710" title="link: http://xkcd.com/710">xkcd</a> (humourous).
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
<ol>
|
||||||
|
<li>Create a routine to generate the hailstone sequence for a number.</li>
|
||||||
|
<li>Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code></li>
|
||||||
|
<li>Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)</li>
|
||||||
|
</ol>
|
||||||
|
<b>See also:</b>
|
||||||
|
<ul>
|
||||||
|
<li><a href="http://xkcd.com/710" title="link: http://xkcd.com/710" target="_blank">xkcd</a> (humourous).</li>
|
||||||
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -44,7 +50,7 @@ tests:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
// noprotect
|
// noprotect
|
||||||
function hailstoneSequence () {
|
function hailstoneSequence() {
|
||||||
const res = [];
|
const res = [];
|
||||||
// Good luck!
|
// Good luck!
|
||||||
|
|
||||||
|
@ -6,14 +6,13 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>A happy number is defined by the following process:</p>
|
A <a href="https://en.wikipedia.org/wiki/Happy_number" target="_blank">happy number</a> is defined by the following process:
|
||||||
<p>Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers.</p>
|
Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals <b>1</b> (where it will stay), or it loops endlessly in a cycle which does not include <b>1</b>. Those numbers for which this process ends in <b>1</b> are happy numbers, while those that do not end in <b>1</b> are unhappy numbers.
|
||||||
<p>Implement a function that returns true if the number is happy, or false if not.</p>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Implement a function that returns true if the number is happy, or false if not.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -58,7 +57,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function happy (number) {
|
function happy(number) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -6,15 +6,15 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>The <a href="http://mathworld.wolfram.com/HarshadNumber.html" title="link: http://mathworld.wolfram.com/HarshadNumber.html">Harshad</a> or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.</p><p>For example, 42 is a <a href="http://rosettacode.org/wiki/oeis:A005349" title="oeis:A005349">Harshad number</a> as 42 is divisible by (4 + 2) without remainder.</p>
|
The <a href="http://mathworld.wolfram.com/HarshadNumber.html" title="link: http://mathworld.wolfram.com/HarshadNumber.html" target="_blank">Harshad</a> or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.
|
||||||
|
For example, <b>42</b> is a <a href="http://rosettacode.org/wiki/oeis:A005349" title="oeis:A005349" target="_blank">Harshad number</a> as <b>42</b> is divisible by <b>(4 + 2)</b> without remainder.
|
||||||
Assume that the series is defined as the numbers in increasing order.
|
Assume that the series is defined as the numbers in increasing order.
|
||||||
Task:
|
|
||||||
<p>Implement a function to generate successive members of the Harshad sequence.</p><p>Use it to list the first twenty members of the sequence and list the first Harshad number greater than 1000.</p>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Implement a function to generate successive members of the Harshad sequence.
|
||||||
|
Use it to list the first twenty members of the sequence and list the first Harshad number greater than 1000.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -37,7 +37,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function isHarshadOrNiven () {
|
function isHarshadOrNiven() {
|
||||||
const res = {
|
const res = {
|
||||||
firstTwenty: [],
|
firstTwenty: [],
|
||||||
firstOver1000: undefined
|
firstOver1000: undefined
|
||||||
|
@ -6,10 +6,11 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
Task:
|
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values).
|
||||||
<p>Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values)</p>
|
<b>Related task:</b>
|
||||||
Related task:
|
<ul>
|
||||||
<a href="http://rosettacode.org/wiki/Associative arrays/Creation" title="Associative arrays/Creation">Associative arrays/Creation</a>
|
<li><a href="http://rosettacode.org/wiki/Associative arrays/Creation" title="Associative arrays/Creation" target="_blank">Associative arrays/Creation</a></li>
|
||||||
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
|
@ -6,30 +6,37 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>An <a href="https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join" title="wp: Join_(SQL)#Inner_join">inner join</a> is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the <a href="https://en.wikipedia.org/wiki/Nested loop join" title="wp: Nested loop join">nested loop join</a> algorithm, but a more scalable alternative is the <a href="https://en.wikipedia.org/wiki/hash join" title="wp: hash join">hash join</a> algorithm.</p>
|
An <a href="https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join" title="wp: Join_(SQL)#Inner_join" target="_blank">inner join</a> is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the <a href="https://en.wikipedia.org/wiki/Nested loop join" title="wp: Nested loop join">nested loop join</a> algorithm, but a more scalable alternative is the <a href="https://en.wikipedia.org/wiki/hash join" title="wp: hash join" target="_blank">hash join</a> algorithm.
|
||||||
<p>Implement the "hash join" algorithm, and demonstrate that it passes the test-case listed below.</p><p>You should represent the tables as data structures that feel natural in your programming language.</p>
|
The "hash join" algorithm consists of two steps:
|
||||||
<p>The "hash join" algorithm consists of two steps:</p>
|
<ol>
|
||||||
Hash phase: Create a <a href="https://en.wikipedia.org/wiki/Multimap" title="wp: Multimap">multimap</a> from one of the two tables, mapping from each join column value to all the rows that contain it.
|
<li><b>Hash phase:</b> Create a <a href="https://en.wikipedia.org/wiki/Multimap" title="wp: Multimap" target="_blank">multimap</a> from one of the two tables, mapping from each join column value to all the rows that contain it.</li>
|
||||||
The multimap must support hash-based lookup which scales better than a simple linear search, because that's the whole point of this algorithm.
|
<ul>
|
||||||
Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size.
|
<li>The multimap must support hash-based lookup which scales better than a simple linear search, because that's the whole point of this algorithm.</li>
|
||||||
Join phase: Scan the other table, and find matching rows by looking in the multimap created before.
|
<li>Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size.</li>
|
||||||
<p>In pseudo-code, the algorithm could be expressed as follows:</p>
|
</ul>
|
||||||
|
<li><b>Join phase:</b> Scan the other table, and find matching rows by looking in the multimap created before.</li>
|
||||||
|
</ol>
|
||||||
|
In pseudo-code, the algorithm could be expressed as follows:
|
||||||
<pre>
|
<pre>
|
||||||
let A = the first input table (or ideally, the larger one)
|
<b>let</b> <i>A</i> = the first input table (or ideally, the larger one)
|
||||||
let B = the second input table (or ideally, the smaller one)
|
<b>let</b> <i>B</i> = the second input table (or ideally, the smaller one)
|
||||||
let j<sub>A</sub> = the join column ID of table A
|
<b>let</b> <i>j<sub>A</sub></i> = the join column ID of table <i>A</i>
|
||||||
let j<sub>B</sub> = the join column ID of table B
|
<b>let</b> <i>j<sub>B</sub></i> = the join column ID of table <i>B</i>
|
||||||
let M<sub>B</sub> = a multimap for mapping from single values to multiple rows of table B (starts out empty)
|
<b>let</b> <i>M<sub>B</sub></i> = a multimap for mapping from single values to multiple rows of table <i>B</i> (starts out empty)
|
||||||
let C = the output table (starts out empty)
|
<b>let</b> <i>C</i> = the output table (starts out empty)
|
||||||
for each row b in table B:
|
<b>for each</b> row <i>b</i> in table <i>B</i>:
|
||||||
place b in multimap M<sub>B</sub> under key b(j<sub>B</sub>)
|
<b>place</b> <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>b(j<sub>B</sub>)</i>
|
||||||
for each row a in table A:
|
<b>for each</b> row <i>a</i> in table <i>A</i>:
|
||||||
for each row b in multimap M<sub>B</sub> under key a(j<sub>A</sub>):
|
<b>for each</b> row <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>a(j<sub>A</sub>)</i>:
|
||||||
let c = the concatenation of row a and row b
|
<b>let</b> <i>c</i> = the concatenation of row <i>a</i> and row <i>b</i>
|
||||||
place row c in table C</p>
|
<b>place</b> row <i>c</i> in table <i>C</i>
|
||||||
</pre>
|
</pre>
|
||||||
Test-case
|
</section>
|
||||||
<p>Input</p>
|
|
||||||
|
## Instructions
|
||||||
|
<section id='instructions'>
|
||||||
|
Implement the "hash join" algorithm as a function and demonstrate that it passes the test-case listed below. The function should accept two arrays of objects and return an array of combined objects.
|
||||||
|
<h4><b>Input</b></h4>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td style="padding: 4px; margin: 5px;">
|
<td style="padding: 4px; margin: 5px;">
|
||||||
@ -107,13 +114,13 @@ Test-case
|
|||||||
</td>
|
</td>
|
||||||
<td style="padding: 4px; margin: 5px;">
|
<td style="padding: 4px; margin: 5px;">
|
||||||
</td></tr></table>
|
</td></tr></table>
|
||||||
<p>Output</p>
|
<h4><b>Output</b></h4>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th style="padding: 4px; margin: 5px;"> A.Age </th>
|
<th style="padding: 4px; margin: 5px;"> A_age </th>
|
||||||
<th style="padding: 4px; margin: 5px;"> A.Name </th>
|
<th style="padding: 4px; margin: 5px;"> A_name </th>
|
||||||
<th style="padding: 4px; margin: 5px;"> B.Character </th>
|
<th style="padding: 4px; margin: 5px;"> B_character </th>
|
||||||
<th style="padding: 4px; margin: 5px;"> B.Nemesis
|
<th style="padding: 4px; margin: 5px;"> B_nemesis
|
||||||
</th></tr>
|
</th></tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td style="padding: 4px; margin: 5px;"> 27 </td>
|
<td style="padding: 4px; margin: 5px;"> 27 </td>
|
||||||
@ -157,13 +164,7 @@ Test-case
|
|||||||
<td style="padding: 4px; margin: 5px;"> Alan </td>
|
<td style="padding: 4px; margin: 5px;"> Alan </td>
|
||||||
<td style="padding: 4px; margin: 5px;"> Zombies
|
<td style="padding: 4px; margin: 5px;"> Zombies
|
||||||
</td></tr></table>
|
</td></tr></table>
|
||||||
<p></p><p></p><p>The order of the rows in the output table is not significant.</p>
|
The order of the rows in the output table is not significant.
|
||||||
<p>If you're using numerically indexed arrays to represent table rows (rather than referring to columns by name), you could represent the output rows in the form <code style="white-space:nowrap">[[27, "Jonah"], ["Jonah", "Whales"]]</code>.</p><hr>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Instructions
|
|
||||||
<section id='instructions'>
|
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -186,7 +187,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function hashJoin (hash1, hash2) {
|
function hashJoin(hash1, hash2) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@ -239,7 +240,7 @@ const bench2 = [{ friend: 'o8b', num: 8 }, { friend: 'ye', num: 2 }, { friend: '
|
|||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function hashJoin (hash1, hash2) {
|
function hashJoin(hash1, hash2) {
|
||||||
const hJoin = (tblA, tblB, strJoin) => {
|
const hJoin = (tblA, tblB, strJoin) => {
|
||||||
const [jA, jB] = strJoin.split('=');
|
const [jA, jB] = strJoin.split('=');
|
||||||
const M = tblB.reduce((a, x) => {
|
const M = tblB.reduce((a, x) => {
|
||||||
|
@ -6,18 +6,21 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p><a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp: Heron's formula">Hero's formula</a> for the area of a triangle given the length of its three sides <big> a,</big> <big>b,</big> and <big>c</big> is given by:</p><p><big>$$A = \sqrt{s(s-a)(s-b)(s-c)},$$</big></p><p>where <big>s</big> is half the perimeter of the triangle; that is,</p><p><big>$$s=\frac{a+b+c}{2}.$$</big></p>
|
<a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp: Heron's formula" target="_blank">Hero's formula</a> for the area of a triangle given the length of its three sides <big> a,</big> <big>b,</big> and <big>c</big> is given by:
|
||||||
<p><a href="http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" title="link: http://www.had2know.com/academics/heronian-triangles-generator-calculator.html">Heronian triangles</a> are triangles whose sides and area are all integers.</p>
|
<span style="margin-left: 2em;"><big>$A = \sqrt{s(s-a)(s-b)(s-c)},$</big></span>
|
||||||
<p> An example is the triangle with sides 3, 4, 5 whose area is 6 (and whose perimeter is 12). </p>
|
where <big>s</big> is half the perimeter of the triangle; that is,
|
||||||
<p>Note that any triangle whose sides are all an integer multiple of 3, 4, 5; such as 6, 8, 10, will also be a Heronian triangle.</p><p>Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor</p>
|
<span style="margin-left: 2em;"><big>$s=\frac{a+b+c}{2}.$</big></span>
|
||||||
<p>of all three sides is 1 (unity).</p><p>This will exclude, for example, triangle 6, 8, 10.</p>
|
<a href="http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" title="link: http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" target="_blank">Heronian triangles</a> are triangles whose sides and area are all integers.
|
||||||
Task:
|
An example is the triangle with sides <b>3, 4, 5</b> whose area is <b>6</b> (and whose perimeter is <b>12</b>).
|
||||||
<p>Implement a function based on Hero's formula that returns the first <code>n<sub>th</sub></code> ordered triangles in an array of arrays.</p>
|
Note that any triangle whose sides are all an integer multiple of <b>3, 4, 5</b>; such as <b>6, 8, 10,</b> will also be a Heronian triangle.
|
||||||
|
Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor
|
||||||
|
of all three sides is <b>1</b> (unity).
|
||||||
|
This will exclude, for example, triangle <b>6, 8, 10.</b>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Implement a function based on Hero's formula that returns the first <code>n<sub>th</sub></code> ordered triangles in an array of arrays.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -47,7 +50,7 @@ tests:
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
// noprotect
|
// noprotect
|
||||||
function heronianTriangle (n) {
|
function heronianTriangle(n) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
@ -81,7 +84,7 @@ const res = [
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
// noprotect
|
// noprotect
|
||||||
function heronianTriangle (n) {
|
function heronianTriangle(n) {
|
||||||
const list = [];
|
const list = [];
|
||||||
const result = [];
|
const result = [];
|
||||||
|
|
||||||
|
@ -6,23 +6,31 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>These two sequences of positive integers are defined as:</p>
|
These two sequences of positive integers are defined as:
|
||||||
<p><big>$$R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$$</big></p>
|
<span style="margin-left: 2em;"><big>$R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$</big></span>
|
||||||
<p>The sequence <big>$S(n)$</big> is further defined as the sequence of positive integers not present in <big>$R(n)$</big>.</p><p>Sequence <big>$R$</big> starts:</p>
|
The sequence <big>$S(n)$</big> is further defined as the sequence of positive integers not present in <big>$R(n)$</big>.
|
||||||
<p>1, 3, 7, 12, 18, ...</p>
|
Sequence <big>$R$</big> starts:
|
||||||
<p>Sequence <big>$S$</big> starts:</p>
|
<pre>1, 3, 7, 12, 18, ...</pre>
|
||||||
<p>2, 4, 5, 6, 8, ...</p>
|
Sequence <big>$S$</big> starts:
|
||||||
Task:
|
<pre>2, 4, 5, 6, 8, ...</pre>
|
||||||
Create two functions named ffr and ffs that when given n return R(n) or S(n) respectively.(Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
|
|
||||||
No maximum value for n should be assumed.
|
|
||||||
Sloane's <a href="http://oeis.org/A005228" title="link: http://oeis.org/A005228">A005228</a> and <a href="http://oeis.org/A030124" title="link: http://oeis.org/A030124">A030124</a>.
|
|
||||||
<a href="http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" title="link: http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html">Wolfram MathWorld</a>
|
|
||||||
Wikipedia: <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" title="wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences">Hofstadter Figure-Figure sequences</a>.
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Create two functions named <b>ffr</b> and <b>ffs</b> that when given <b>n</b> return <b>R(n)</b> or <b>S(n)</b> respectively. (Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
|
||||||
|
No maximum value for <b>n</b> should be assumed.
|
||||||
|
<b>References</b>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Sloane's <a href="http://oeis.org/A005228" title="link: http://oeis.org/A005228" target="_blank">A005228</a> and <a href="http://oeis.org/A030124" title="link: http://oeis.org/A030124" target="_blank">A030124</a>.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" title="link: http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" target="_blank">Wolfram MathWorld</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Wikipedia: <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" title="wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" target="_blank">Hofstadter Figure-Figure sequences</a>.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
@ -6,16 +6,14 @@ challengeType: 5
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
<p>The <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence" title="wp: Hofstadter_sequence#Hofstadter_Q_sequence">Hofstadter Q sequence</a> is defined as:</p>
|
The <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence" title="wp: Hofstadter_sequence#Hofstadter_Q_sequence" target="_blank">Hofstadter Q sequence</a> is defined as:
|
||||||
<p>$Q(1)=Q(2)=1, \\ Q(n)=Q\big(n-Q(n-1)\big)+Q\big(n-Q(n-2)), \quad n>2.$</p>
|
<span style="left-margin: 2em;">$Q(1)=Q(2)=1, \\ Q(n)=Q\big(n-Q(n-1)\big)+Q\big(n-Q(n-2)), \quad n>2.$</span>
|
||||||
<p>It is defined like the <a href="http://rosettacode.org/wiki/Fibonacci sequence" title="Fibonacci sequence">Fibonacci sequence</a>, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.</p>
|
It is defined like the <a href="http://rosettacode.org/wiki/Fibonacci sequence" title="Fibonacci sequence" target="_blank">Fibonacci sequence</a>, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.
|
||||||
Task:
|
|
||||||
Implement the Hofstadter Q Sequence equation into JavaScript
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Implement the Hofstadter Q Sequence equation as a function. The function should accept number, <code>n</code>, and return an integer.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -46,7 +44,7 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function hofstadterQ (n) {
|
function hofstadterQ(n) {
|
||||||
// Good luck!
|
// Good luck!
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user