12
This is a new kind of challenge inspired by the Recover the mutated source code problem.
You should write two programs or functions both in the same language. The first one should solve Task #1 and the second one should solve Task #2.
Your score will be the sum of the longer program and the Levenshtein distance between the two programs source code. Lower score is better so you should try to make the two solutions similar while keeping the lengths of the programs short.
Task #1
You are given a positive integer N
and you should output the Collatz sequence of N
separated by spaces or newline. Trailing separator is allowed.
The first element of the Collatz sequence is N
. The rest of the elements are generated based on their successor \$a_{i-1}\$:
$$ a_i = \begin{cases} \frac{a_{i-1}}{2} & \text{ if } a_{i-1} \text{ is even} \\ 3a_{i-1} + 1 & \text{ if } a_{i-1} \text{ is odd} \end{cases} $$
As soon as the sequence reaches 1
no new elements are generated.
Input => Output examples:
6 => 6 3 10 5 16 8 4 2 1
8 => 8 4 2 1
1 => 1
Task #2
A pair of twin primes is a pair of positive integer whose difference is 2 and they are both primes.
You are given a positive integer N
and you should output the smallest pair of twin primes where both primes are bigger than N
The first number should be the smaller one and the two primes should be separated by spaces or newline. Trailing separator is allowed.
Input => Output examples:
6 => 11 13
42 => 59 61
1 => 3 5
Snippet for calculating the score
(Modification of the one in the Recover the mutated source code problem.)
var f=document.getElementById("f"),g=document.getElementById("s");function h(){var a=f.value,e=g.value,m=Math.max(a.length,e.length);if(10000<a.length)a="<span style='color:red'>First program is too long!</span>";else if(10000<e.length)a="<span style='color:red'>Second program is too long!</span>";else{if(0===a.length)a=e.length;else if(0===e.length)a=a.length;else{var d=[],b;for(b=0;b<=e.length;b++)d[b]=[b];var c;for(c=0;c<=a.length;c++)d[0][c]=c;for(b=1;b<=e.length;b++)for(c=1;c<=a.length;c++)d[b][c]=e.charAt(b-1)===a.charAt(c-1)?d[b-1][c-1]:Math.min(d[b-1][c-1]+1,Math.min(d[b][c-1]+1,d[b-1][c]+ 1));a=d[e.length][a.length]}a="Distance = "+a+" Longer length = "+m+" Score = <strong>"+(a+m)+"</strong>"}document.getElementById("d").innerHTML=a}f.onkeyup=h;g.onkeyup=h;
First program<textarea id=f rows=2 cols=80 style="width:100%"></textarea>Second program<textarea id=s rows=2 cols=80 style="width:100%"></textarea><p id=d></p>
Edit
In the answers' header let's use the format
[Language], [longer length] + [distance] = [final score]
.
E.g.
Python 2, 60 + 32 = 92
Collatz link is incorrect – ASCII-only – 2019-01-19T01:22:02.990
Thanks, fixed it! – Wisław – 2019-01-19T01:32:26.950
primes, -3 score. probably worth keeping golfed version in the answer as a side note in case there's a different, better golf (what exactly does pair do, btw? like why do i have to do
,,
, and why does onlyÐ,
work there) – ASCII-only – 2019-01-19T01:42:18.983-1 more (edit: never mind, first number should be smaller. score 24 for now – ASCII-only – 2019-01-19T01:57:08.350
22? – ASCII-only – 2019-01-19T02:02:38.600
Thanks! Guess I was more focused on trying to golf each individual challenge rather than minimize the score. I am not quite familiar with the Levenshtein distance :)
Pair
‚
pops a, b and pushes [a,b]. Note that the pair instruction is not a comma :). The comma is actually the print instruction,
. Compare these programs: two prints and [pair](https://tio.run/##yy9OTMpM/f8/2uVwq5@LnV2BcqzL4Z5HDbP@/zc1NQIA "05AB1E – Try It Online). Note that the two different characters look identical in the tio.run font. :)24 is the shortest correct one i think – ASCII-only – 2019-01-19T02:19:08.477
score 23 if it is allowed to have output being a list of a list – Wisław – 2019-01-19T02:22:33.430
Let us continue this discussion in chat.
– ASCII-only – 2019-01-19T02:24:22.750