var QUESTION_ID=80608,OVERRIDE_USER=49561;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
Antworten:
Jelly,
2220 bytesTry it online!
How it works
quelle
Julia,
5248 bytesTry it online!
Background
In On Hofstadters Ehefeiern zeigt der Autor das
worin φ den goldenen Schnitt bezeichnet ,
und F n bezeichnet die n- te Fibonacci-Zahl .
Darüber hinaus zeigt der Antragsteller in Advanced Problems and Solutions, H-187: Fibonacci ist ein Quadrat , dass
Dabei bezeichnet L n die n- te Lucas-Zahl und umgekehrt, wenn
dann ist n eine Fibonacci-Zahl und m ist eine Lucas-Zahl.
Daraus leiten wir ab
wann immer n> 0 .
Wie es funktioniert
Given input x, we construct a 2 by x matrix, where | is addition in the first column and subtraction in the second, and n iterates over the integers between 1 and x in the rows.
The first term of both F(n - 1) and M(n - 1) is simply
n÷φ
.We compute δ(n) and ε(n) by calculating 5n² | 4 and testing if the result belongs to the array of squares of the integers between 2 and 3n. This tests both for squareness and, since 1 is not in the range, for n > 1 if | is subtraction.
Finally we add or subtract the Boolean resulting from
5n^2|4∈(2:3n).^2
to or from the previously computed integer.quelle
Python 2,
7970 bytesIterative rather than recursive, because why not. The first line has a trailing space - if that's not okay it can be fixed for an extra byte. -9 bytes thanks to @Dennis.
Here are some combined lambdas which didn't really help:
Both take
n
and a parameterk
either 0 or 1, specifying male/female. The first lambda returns the nth element, and the second lambda returns the first n elements (with exponential runtime).quelle
MATL, 23 bytes
Try it online!
Explanation
This works iteratively. Each sequence is kept in an array. For each index n the new term of each sequence is computed and attached to the corresponding array. A
for
loop with N−1 terms is used, where N is the input number.The update for sequence M needs to be done first. This is because sequence F is always greater than or equal to sequence M for the same index, so if we tried to update F first we would need a term of M not computed yet.
The two update equations are the same interchanging F and M. Thus the code for the updating is reused by applying a
for
loop with two iterations and swapping the sequences in the stack.quelle
J, 47 bytes
Uses the recursive definition. The first two lines define the verbs
f
andm
which represent the female and male functions, respectively. The last line is a verb that takes a single argumentn
and outputs the firstn
terms of the female and male sequences.Usage
quelle
JavaScript (ES6), 75 bytes
I could save 2 bytes if I was allowed to return the Male sequence first:
quelle
Haskell, 57 bytes
Usage example:
(<$>[v,w]).take $ 5
->[[1,1,2,2,3],[0,0,1,2,2]]
The helper function
#
builds an infinite list with starting values
and a listl
for looking up all further elements (at index of the previous value).v = w#1
is the female andw = v#0
the male sequence. In the main function we take the firstn
elements of bothv
andw
.quelle
Python 2, 107 bytes
Try it online
Larger input values cause a RuntimeError (too much recursion). If this is a problem, I can write a version where the error doesn't happen.
quelle
Julia, 54 bytes
Try it online!
quelle
Pyth, 24 bytes
It is probably impossible to use
reduce
to reduce the byte-count.Straightforward implementation.
Try it online!
How it works
quelle
Brachylog, 65 bytes
My attempt at combining both predicates for male and female into one actually made the code longer.
You could use the following one liner which has the same number of bytes:
Note: This works with the Prolog transpiler, not the old Java one.
Explanation
Main predicate:
Predicate 2 (female):
Predicate 3 (male):
quelle
{:1-:0re.}
, used to create the range list.Clojure,
132131 bytesSimply builds the sequences iteratively up from zero to n.
Ungolfed version
quelle
Pyth, 23 bytes
Try it online: Demonstration
Explanation:
Alternative solution that uses a function instead of reduce (also 23 bytes):
quelle
Ruby,
104929782 bytesEdit:
f
andm
are now one function thanks to HopefullyHelpful. I changed the second function to printf
thenm
. The whitespace afterp
is significant, as otherwise the function prints(0...n)
instead of the result ofmap
.The third function prints first an array of the first n terms of
f
, followed by an array of the first n terms ofm
These functions are called like this:
quelle
n
andi
n>0?n-f(f(n-1,i),-i):i>0?1:0
f
andm
, I need to print it. Otherwise, I just get an array like[[1, 1, 2, 2, 3, 3, 4, 5, 5, 6], [0, 0, 1, 2, 2, 3, 4, 4, 5, 6]]
APL (Dyalog Unicode),
4525 bytesAnonymous tacit function. Requires
⎕IO←0
, which is standard on many APL systems.Try it online!
This works by combining F and M into a single dyadic function with a Boolean left argument which selects the function to apply. We use 1 for F and 0 for M so that we can use this selector as return value for F (0) and M (0). We then observe that both functions need to call themselves first (on the argument minus one) and then the other function on the result of that, so first we recurse with the given selector and then with the logically negated selector.
⍳
ɩndices; zero through argument minus one1 0∘.{
…}
outer (Cartesian) "product" (but with the below function instead of multiplication) using[1,0]
as left arguments (⍺
) and the indices as right arguments (⍵
):×⍵
if the right argument is strictly positive (lit. the sign of the right argument):⍵-1
subtract one from the right argument⍺∇
call self with that as right argument and the left argument as left argument(~⍺)∇
call self with that as right arg and the logical negation of the left arg as left arg⍵-
subtract that from the right argument and return the result⋄
else:⍺
return the left argumentquelle
n
) + plus a newline.ES6,
898583 bytes2 bytes saved thanks to @Bálint
Naïve implementation.
Explanation:
quelle
&&
short-circuits, which is wanted, but I removed it anyway because brace syntax is equally short anywayMathematica,
6962 bytesThanks to Sp3000 for suggesting a functional form which saved 14 bytes.
This defines a named helper function
f
and then evaluates to an unnamed function which solves the actual task of printing both sequences.quelle
Perl 5.10,
8580 bytesMeh, dunno if I have more ideas to golf this down...
Try it online !
I had to add
use 5.10.0
on Ideone in order for it to accept thesay
function, but it doesn't count towards the byte count.It's a naive implementation of the algorithm,
@a
being the "female" list and@b
the "male" list.Crossed-out 85 is still 85 ?
quelle
push
ed term, nor the final semicolon before the close-brace.Java, 169 bytes total
F(), M() 56 bytes
recursive-for-loop and printing 77 Bytes
outputting the lists in two different lines 37 Bytes
input : p(
10
)output :
quelle
C, 166 Bytes
Usage:
Output:
Ungolfed (331 Bytes)
quelle
8th, 195 bytes
Code
Usage
Explanation
This code uses recursion and deferred word
defer: M
- The wordM
is declared to be defined later. This is a deferred word: F dup not if 1 nip else dup n:1- recurse M n:- then ;
- DefineF
recursively to generate female numbers according definition. Please note thatM
has not yet been defined( dup not if 0 nip else dup n:1- recurse F n:- then ) is M
- DefineM
recursively to generate male numbers according definition: FM n:1- dup ( F . space ) 0 rot loop cr ( M . space ) 0 rot loop cr ;
- Word used to print sequences of female and male numbersquelle