21
\$\begingroup\$

This question is probably harder than all of those "generate a sequence of numbers" tasks, because this requires TWO sequences working in unison.

Really looking forward to the answers!

In his book "Gödel, Escher, Bach: An Eternal Golden Braid", Douglas Hofstadter has a quite few sequences of numbers inside, all of them rely on the previous term in some way. For information on all of the sequences, see this Wikipedia page.

One pair of sequences that's really interesting is the Female and Male sequences, which are defined like so:

for n > 0.

Here's the Female sequence and the Male sequence.

Your task is, when given an integer n as input, return a list of the Female sequence and the Male sequence, with the amount of terms equal to n, in two lines of output, with the Female sequence on the first line and the Male sequence on the second.

Sample inputs and outputs: Input: 5 Output: [1, 1, 2, 2, 3] [0, 0, 1, 2, 2]

Input: 10 Output: [1, 1, 2, 2, 3, 3, 4, 5, 5, 6] [0, 0, 1, 2, 2, 3, 4, 4, 5, 6]

NOTE: The separation between lists signifies a line break.

This is code-golf, so shortest code in bytes wins. Also, put an explanation in as well for your code.

Leaderboard

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>

\$\endgroup\$
6
  • 6
    \$\begingroup\$ Can we return a pair of lists from a function, instead of printing the lists? \$\endgroup\$
    – Zgarb
    May 25, 2016 at 9:54
  • \$\begingroup\$ Other challenges involving Hofstadter's sequences: Q sequence, figure-figure sequence \$\endgroup\$ May 25, 2016 at 11:17
  • \$\begingroup\$ @Zgarb You can, as long as the two lists are in different lines. \$\endgroup\$
    – clismique
    May 26, 2016 at 11:55
  • 3
    \$\begingroup\$ @DerpfacePython There are no lines in a pair of lists; if a function returns a pair of lists, you can print them however you want. That being said, I'm not a huge fan of the lines requirement, even when printing the output. Cumbersome I/O formats are one of the things to avoid when writing challenges. \$\endgroup\$
    – Dennis
    May 26, 2016 at 16:57
  • 5
    \$\begingroup\$ It's no big deal for some approaches/languages, but it can make a big difference for others. In C, a lot of bytes could be saved by printing the sequences in columns rather than rows. In Python, the shortest approach I can think of is a recursive lambda similar to my recursive Julia answer that returns a pair of lists, but having to convert that into a string with a linefeed makes it a lot longer, even longer than the full program posted by Sp3000. Other approaches, such as a recursive solution that counts down instead of up, are completely ruled out since it is impossible to add the newline. \$\endgroup\$
    – Dennis
    May 27, 2016 at 15:38

21 Answers 21

14
\$\begingroup\$

Julia, 52 48 bytes

x->[n÷φ|(5n^2|4∈(2:3n).^2)for| =(+,-),n=1:x]

Try it online!

Background

In On Hofstadter's married functions, the author shows that

F/M formula

where φ denotes the golden ratio,

delta/epsilon formula

and Fn denotes the nth Fibonacci number.

Furthermore, in Advanced Problems and Solutions, H-187: Fibonacci is a square, the proposer shows that

Fibonacci/Lucas identity

where Ln denotes the nth Lucas number, and that – conversely – if

converse Fibonacci/Lucas identity

then n is a Fibonacci number and m is a Lucas number.

From this, we deduce that

delta/epsilon theorem

whenever n > 0.

How it works

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.

\$\endgroup\$
2
  • \$\begingroup\$ can this be expressed in a non-recursive/iterative way ?, what is the closed form for it ? \$\endgroup\$
    – Abr001am
    May 26, 2016 at 18:05
  • \$\begingroup\$ I've added an explanation. \$\endgroup\$
    – Dennis
    May 27, 2016 at 0:05
11
\$\begingroup\$

Python 2, 79 70 bytes

a=0,;b=1,
exec"a,b=b,a+(len(a)-b[a[-1]],);"*~-input()*2
print b,'\n',a

Iterative 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:

f=lambda n,k:n and n-f(f(n-1,k),k^1)or k
f=lambda n,k:[k][n:]or f(n-1,k)+[n-f(f(n-1,k)[-1],k^1)[-1]]

Both take n and a parameter k 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).

\$\endgroup\$
9
\$\begingroup\$

MATL, 23 bytes

1Oiq:"@XJth"yy0)Q)_J+hw

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.

1        % Push 1: seed for F sequence
O        % Push 0: seed for M sequence
iq:      % Input N. Generate range [1 2 ... N-1]
"        % For each (i.e. iterate N-1 times)
  @      %   Push current index, n (starting at 1 and ending at N-1)
  XJ     %   Copy to clipboard J
  th     %   Duplicate and concatenate. This generates a length-2 array
  "      %   For each (i.e. iterate twice)
    yy   %   Duplicate top two elements, i.e. F and M sequences
    0)   %     In the *first* iteration: get last entry of M, i.e M(n-1)
    Q)   %     Add 1 and index into F. This is F(M(n-1))
    _J+  %     Negate and add n. This is n-F(M(n-1)), that is, M(n)
    h    %     Concatenate to update M
    w    %     Swap top two elements, to bring F to top.
         %     In the *second* iteration the procedure is repeated to update F,
         %     and then the top two elements are swapped to bring M to top again,
         %     ready for the next iteration of the outer loop
         %   End for implicitly
         % End for implicitly
         % Display implicitly from bottom to top: first line is F, second is M
\$\endgroup\$
6
\$\begingroup\$

J, 47 bytes

f=:1:`(-m@f@<:)@.*
m=:0:`(-f@m@<:)@.*
(f,:m)@i.

Uses the recursive definition. The first two lines define the verbs f and m which represent the female and male functions, respectively. The last line is a verb that takes a single argument n and outputs the first n terms of the female and male sequences.

Usage

   (f,:m)@i. 5
1 1 2 2 3
0 0 1 2 2
   (f,:m)@i. 10
1 1 2 2 3 3 4 5 5 6
0 0 1 2 2 3 4 4 5 6
\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 75 bytes

g=n=>--n?([f,m]=g(n),m=[...m,n-f[m[n-1]]],[[...f,n-m[f[n-1]]],m]):[[1],[[0]]

I could save 2 bytes if I was allowed to return the Male sequence first:

g=n=>--n?([f,m]=g(n),[m=[...m,n-f[m[n-1]]],[...f,n-m[f[n-1]]]]):[[1],[[0]]
\$\endgroup\$
6
\$\begingroup\$

Haskell, 57 bytes

l#s=scanl(\a b->b-l!!a)s[1..]
v=w#1
w=v#0
(<$>[v,w]).take

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 value s and a list l for looking up all further elements (at index of the previous value). v = w#1 is the female and w = v#0 the male sequence. In the main function we take the first n elements of both v and w.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 22 20 bytes

ṙṪḢạL}ṭ
çƓḤ¤Ð¡1ṫ-Ṗ€G

Try it online!

How it works

çƓḤ¤Ð¡1ṫ-Ṗ€G  Main link. No user arguments. Left argument defaults to 0.
   ¤          Combine the two links to the left into a niladic chain.
 Ɠ              Read an integer from STDIN.
  Ḥ             Unhalve/double it.
ç   С1       Call the helper link that many times. Return all results.
              In the first call, the left and right argument are 0 and 1 resp.
              After each iteration, the left argument is set to the return value
              and the right argument to the prior value of the left one.
       ṫ-     Tail -1; keep the last two items of the list of results.
         Ṗ€   Discard the last item of each list.
           G  Grid; format the pair of lists.


ṙṪḢạL}ṭ       Helper link. Arguments: x, y (lists)

ṙ             Rotate x k units to the left, for each k in y.
 Ṫ            Tail; extract the last rotation.
  Ḣ           Head; extract the last element.
              This essentially computes x[y[-1]] (Python notation), avoiding
              Jelly's 1-based indexing.
    L}        Yield the length of y.
   ạ          Take the absolute difference of the results to both sides.
      ṭ       Tack; append the difference to y and return the result.
\$\endgroup\$
1
  • 5
    \$\begingroup\$ And this is the part where I feel proud of myself for making a challenge that makes Jelly use more than 10 bytes. \$\endgroup\$
    – clismique
    May 26, 2016 at 11:56
4
\$\begingroup\$

Python 2, 107 bytes

F=lambda n:n and n-M(F(n-1))or 1
M=lambda n:n and n-F(M(n-1))
n=range(input())
print map(F,n),'\n',map(M,n)

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.

\$\endgroup\$
4
\$\begingroup\$

Clojure, 132 131 bytes

(fn [n](loop[N 1 M[0]F[1]](if(< N n)(let[M(conj M(- N(F(peek M))))F(conj F(- N(M(peek F))))](recur(inc N)M F))(do(prn F)(prn M)))))

Simply builds the sequences iteratively up from zero to n.

Ungolfed version

(fn [n]
  (loop [N 1 M [0] F [1]]
    (if (< N n)
      (let [M (conj M (- N (F (peek M))))
            F (conj F (- N (M (peek F))))]
        (recur (inc N) M F))
      (do
        (prn F)
        (prn M)))))
\$\endgroup\$
2
  • \$\begingroup\$ Nice answer, welcome to the site! Is a trailing space or newline necessary? I'm counting 131 + a trailing whitespace. \$\endgroup\$
    – DJMcMayhem
    May 25, 2016 at 17:31
  • \$\begingroup\$ No, there's no need for a trailing whitespace. Sneaky vim added a newline at the end for wc to count. \$\endgroup\$
    – mark
    May 25, 2016 at 17:42
4
\$\begingroup\$

Julia, 54 bytes

f(n,x=1,y=0)=n>1?f(n-.5,[y;-x[y[k=end]+1]+k],x):[x y]'

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pyth, 24 bytes

It is probably impossible to use reduce to reduce the byte-count.

Straightforward implementation.

L&b-b'ytbL?b-by'tb1'MQyM

Try it online!

How it works

L&b-b'ytb  defines a function y, which is actually the male sequence.

L          def male(b):
 &b            if not b: return b
   -b          else: return b-
     'ytb            female(male(b-1))


L?b-by'tb1 defines a function ', which is actually the female sequence.

L          def female(b):
 ?b            if b:
   -by'tb          return b-male(female(b-1))
         1     else: return 1


'MQ        print(female(i) for i from 0 to input)
yMQ        print(male(i) for i from 0 to input)
\$\endgroup\$
8
  • \$\begingroup\$ Do I include the anagrammatized name or your original name in the leaderboard? Also, this code is awfully long for a Pyth program. \$\endgroup\$
    – clismique
    May 25, 2016 at 7:25
  • \$\begingroup\$ How long have you been here... how come you know that I changed my name? Put my new name there. \$\endgroup\$
    – Leaky Nun
    May 25, 2016 at 7:34
  • 1
    \$\begingroup\$ I've been here long enough to know that you changed your name. \$\endgroup\$
    – clismique
    May 25, 2016 at 7:35
  • \$\begingroup\$ @DerpfacePython Seeing that other answers are almost 4 times as long... I'd say my solution is not very long. \$\endgroup\$
    – Leaky Nun
    May 25, 2016 at 7:57
  • \$\begingroup\$ That's very true, but it's still long compared to other Pyth programs for other questions. \$\endgroup\$
    – clismique
    May 25, 2016 at 7:58
3
\$\begingroup\$

Brachylog, 65 bytes

:{:1-:0re.}fL:2aw,@Nw,L:3aw
0,1.|:1-:2&:3&:?--.
0.|:1-:3&:2&:?--.

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:

:{:1-:0re.}fL:{0,1.|:1-:2&:3&:?--.}aw,@Nw,L:{0.|:1-:3&:2&:?--.}aw

Note: This works with the Prolog transpiler, not the old Java one.

Explanation

Main predicate:

:{:1-:0re.}fL                Build a list L of integers from 0 to Input - 1
             :2aw            Apply predicate 2 to each element of L, write the resulting list
                 ,@Nw        Write a line break
                     ,L:3aw  Apply predicate 3 to each element of L, write the resulting list

Predicate 2 (female):

0,1.                         If Input = 0, unify Output with 1
    |                        Else
     :1-                     Subtract 1 from Input
        :2&                  Call predicate 2 with Input - 1 as argument
           :3&               Call predicate 3 with the Output of the previous predicate 2
              :?-            Subtract Input from the Output of the previous predicate 3
                 -.          Unify the Output with the opposite of the subtraction

Predicate 3 (male):

0.                           If Input = 0, unify Output with 0
  |                          Else
   :1-                       Subtract 1 from Input
      :3&                    Call predicate 3 with Input - 1 as argument
         :2&                 Call predicate 2 with the Output of the previous predicate 3
            :?-              Subtract Input from the Output of the previous predicate 3
               -.            Unify the Output with the opposite of the subtraction
\$\endgroup\$
2
  • \$\begingroup\$ Wait... which one's predicate 3? \$\endgroup\$
    – clismique
    May 25, 2016 at 7:54
  • \$\begingroup\$ @DerpfacePython whoops, fixed. Also note that predicate one is {:1-:0re.}, used to create the range list. \$\endgroup\$
    – Fatalize
    May 25, 2016 at 7:55
3
\$\begingroup\$

Pyth, 23 bytes

jCuaG-LHtPs@LGeGr1Q],1Z

Try it online: Demonstration

Explanation:

jCuaG-LHtPs@LGeGr1Q],1Z

  u                ],1Z    start with G = [[1, 0]]
                           (this will be the list of F-M pairs)
  u             r1Q        for each H in [1, 2, ..., Q-1]:
              eG              take the last pair of G [F(H-1), M(H-1)]
           @LG                lookup the pairs of these values:
                              [[F(F(H-1)), M(F(H-1))], [F(M(H-1)), M(M(H-1))]]
          s                   join them:
                              [F(F(H-1)), M(F(H-1)), F(M(H-1)), M(M(H-1))]
        tP                    get rid of the first and last element:
                              [M(F(H-1)), F(M(H-1))]
     -LH                      subtract these values from H
                              [H - M(F(H-1)), H - F(M(H-1))]
   aG                         and append this new pair to G
jC                         at the end: zip G and print each list on a line

Alternative solution that uses a function instead of reduce (also 23 bytes):

L?>b1-LbtPsyMytb,1ZjCyM
\$\endgroup\$
1
  • \$\begingroup\$ Nice. Very nice indeed. \$\endgroup\$
    – Leaky Nun
    May 26, 2016 at 23:14
3
\$\begingroup\$

Ruby, 104 92 97 82 bytes

f=->n,i{n>0?n-f[f[n-1,i],-i]:i>0?1:0}
->n{[1,-1].map{|k|p (0...n).map{|i|f[i,k]}}}

Edit: f and m are now one function thanks to HopefullyHelpful. I changed the second function to print f then m. The whitespace after p is significant, as otherwise the function prints (0...n) instead of the result of map.

The third function prints first an array of the first n terms of f, followed by an array of the first n terms of m

These functions are called like this:

> f=->n,i{n>0?n-f[f[n-1,i],-i]:i>0?1:0}
> s=->n{[1,-1].map{|k|p (0...n).map{|i|f[i,k]}}}
> s[10]
[1, 1, 2, 2, 3, 3, 4, 5, 5, 6]
[0, 0, 1, 2, 2, 3, 4, 4, 5, 6]
\$\endgroup\$
5
  • \$\begingroup\$ you can drop the p and parens. Output is not required to be printed. Also, you can dorp parens around the range. \$\endgroup\$ May 25, 2016 at 16:40
  • \$\begingroup\$ you can replace the 2 function with 1 that has 2 arguments n and i n>0?n-f(f(n-1,i),-i):i>0?1:0 \$\endgroup\$ May 26, 2016 at 6:02
  • \$\begingroup\$ @HopefullyHelpful Thanks a bunch :D \$\endgroup\$
    – Sherlock9
    May 26, 2016 at 20:10
  • \$\begingroup\$ @NotthatCharles Is the output not required to be printed? In Ruby, if I want that line break between f and m, 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]] \$\endgroup\$
    – Sherlock9
    May 26, 2016 at 20:14
  • \$\begingroup\$ oh, it does say "line break". Too bad. \$\endgroup\$ May 26, 2016 at 20:26
3
\$\begingroup\$

APL (Dyalog Unicode), 45 25 bytes

Anonymous tacit function. Requires ⎕IO←0, which is standard on many APL systems.

1 0∘.{×⍵:⍵-(~⍺)∇⍺∇⍵-1⋄⍺}⍳

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 one

1 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 argument

\$\endgroup\$
4
  • \$\begingroup\$ That works well, but assuming input is stored in a variable is disallowed by default. \$\endgroup\$
    – Dennis
    May 26, 2016 at 0:50
  • \$\begingroup\$ @Dennis It doesn't really. It is a tfn body. When I was new here, ngn told me I didn't have to count the tfn header (which would be two bytes, a single-char name + a newline, just like the source filename isn't counted, and anonymous fns are allowed. So too here, where the header is a 1-char name + space + a 1-char argument name (n) + plus a newline. \$\endgroup\$
    – Adám
    May 26, 2016 at 5:11
  • \$\begingroup\$ What exactly is a tfn? \$\endgroup\$
    – Dennis
    May 26, 2016 at 5:24
  • \$\begingroup\$ @Dennis Tfns are the traditional APL representation of functions. Consist of lines of code with almost none of the dfns's restrictions. E.g you can have proper control structures, and expressions with no result. Line "0" is a header which indicates the fn's syntax. \$\endgroup\$
    – Adám
    May 26, 2016 at 7:30
2
\$\begingroup\$

ES6, 89 85 83 bytes

2 bytes saved thanks to @Bálint

x=>{F=[n=1],M=[0];while(n<x){M.push(n-F[M[n-1]]);F.push(n-M[F[n++-1]])}return[F,M]}

Naïve implementation.

Explanation:

x => {
    F = [n = 1], //female and term number
    M = [0]; //male
    while (n < x) {
        M.push(n - F[M[n - 1]]); //naïve
        F.push(n - M[F[n++ - 1]]); //post-decrement means n++ acts as n in the calculation
    }
    return [F, M];
}
\$\endgroup\$
3
  • \$\begingroup\$ I think you can make it an anonymus function, and replace the &&-s with & \$\endgroup\$
    – Bálint
    May 25, 2016 at 7:58
  • \$\begingroup\$ You can't, && short-circuits, which is wanted, but I removed it anyway because brace syntax is equally short anyway \$\endgroup\$
    – ASCII-only
    May 25, 2016 at 7:59
  • \$\begingroup\$ Then you could do`F=[n=1] \$\endgroup\$
    – Bálint
    May 25, 2016 at 8:01
2
\$\begingroup\$

Mathematica, 69 62 bytes

Thanks to Sp3000 for suggesting a functional form which saved 14 bytes.

k_~f~0=1-k
k_~f~n_:=n-f[1-k,f[k,n-1]]
Print/@Array[f,{2,#},0]&

This defines a named helper function f and then evaluates to an unnamed function which solves the actual task of printing both sequences.

\$\endgroup\$
2
\$\begingroup\$

Perl 5.10, 85 80 bytes

Meh, dunno if I have more ideas to golf this down...

@a=1;@b=0;for(1..<>-1){push@a,$_-$b[$a[$_-1]];push@b,$_-$a[$b[$_-1]]}say"@a\n@b"

Try it online !

I had to add use 5.10.0 on Ideone in order for it to accept the say 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 ?

\$\endgroup\$
5
  • \$\begingroup\$ Explanation, please? \$\endgroup\$
    – clismique
    May 25, 2016 at 7:55
  • \$\begingroup\$ Pretty much the same as my JS answer \$\endgroup\$
    – ASCII-only
    May 25, 2016 at 8:09
  • \$\begingroup\$ @DerpfacePython It's a naive implementation actually. :) \$\endgroup\$
    – Sake
    May 25, 2016 at 8:10
  • \$\begingroup\$ I haven't tested, but I don't think you should need the parentheses around each pushed term, nor the final semicolon before the close-brace. \$\endgroup\$
    – msh210
    May 25, 2016 at 22:28
  • \$\begingroup\$ @msh210 Indeed, forgot about this. Saves 5 bytes in total, thanks! \$\endgroup\$
    – Sake
    May 26, 2016 at 14:31
2
\$\begingroup\$

Java, 169 bytes total

int f(int n,int i){return n>0?n-f(f(n-1,i),-i):i>0?1:0;}void p(int n,int i){if(n>0)p(n-1,i);System.out.print(i==0?"\n":f(n,i)+" ");}void p(int n){p(n,1);p(0,0);p(n,-1);}

F(), M() 56 bytes

int f(int n,int i){
    return n>0?n-f(f(n-1,i),-i):i>0?1:0;
}

recursive-for-loop and printing 77 Bytes

void p(int n,int i) {
    if(n>0) {
        p(n-1,i);
    }
    System.out.print(i==0?"\n":f(n,i)+" ");
}

outputting the lists in two different lines 37 Bytes

void p(int n) {
    p(n,1);
    p(0,0);
    p(n,-1);
}

input : p(10)
output :

1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9
\$\endgroup\$
1
\$\begingroup\$

C, 166 Bytes

#define P printf
#define L for(i=0;i<a;i++)
f(x);m(x);i;c(a){L P("%d ",f(i));P("\n");L P("%d ",m(i));}f(x){return x==0?1:x-m(f(x-1));}m(x){return x==0?0:x-f(m(x-1));}

Usage:

main()
{
    c(10);
}

Output:

1 1 2 2 3 3 4 5 5 6
0 0 1 2 2 3 4 4 5 6

Ungolfed (331 Bytes)

#include <stdio.h>

int female(int x);
int male(int x);
int i;
int count(a){
    for(i=0;i<a;i++){
        printf("%d ",female(i));
    }
    printf("\n");
    for(i=0;i<a;i++){
        printf("%d ",male(i));
    }
}
int female (int x){
    return x==0?1:x-male(female(x-1));
}
int male(x){
    return x==0?0:x-female(male(x-1));
}
int main()
{
    count(10);
}
\$\endgroup\$
0
\$\begingroup\$

8th, 195 bytes

Code

defer: M
: F dup not if 1 nip else dup n:1- recurse M n:- then ;
( dup not if 0 nip else dup n:1- recurse F n:- then ) is M
: FM n:1- dup ( F . space ) 0 rot loop cr ( M . space ) 0 rot loop cr ;

Usage

ok> 5 FM
1 1 2 2 3 
0 0 1 2 2 

ok> 10 FM
1 1 2 2 3 3 4 5 5 6 
0 0 1 2 2 3 4 4 5 6 

Explanation

This code uses recursion and deferred word

defer: M - The word M 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 ; - Define F recursively to generate female numbers according definition. Please note that M has not yet been defined

( dup not if 0 nip else dup n:1- recurse F n:- then ) is M - Define M 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 numbers

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.