Cycles on the torus












18












$begingroup$


Challenge



This challenge will have you write a program that takes in two integers n and m and outputs the number non-intersecting loops on the n by m torus made by starting at (0,0) and only taking steps up and to the right. You can think of torus as the grid with wraparound both at the top and the bottom and on the sides.



This is code-golf so fewest bytes wins.



Example



For example, if the input is n=m=5, one valid walk is



(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (2,4) -> 
(2,0) -> (3,0) -> (4,0) -> (4,1) -> (4,2) -> (4,3) ->
(0,3) -> (1,3) -> (1,4) ->
(1,0) -> (1,1) -> (2,1) -> (3,1) -> (3,2) -> (3,3) -> (3,4) -> (4,4) ->
(0,4) -> (0,0)


as shown in the graphic.



A loop on the torus.



Some example input/outputs



f(1,1) = 2 (up or right)
f(1,2) = 2 (up or right-right)
f(2,2) = 4 (up-up, up-right-up-right, right-right, right-up-right-up)
f(2,3) = 7
f(3,3) = 22
f(2,4) = 13
f(3,4) = 66
f(4,4) = 258









share|improve this question











$endgroup$








  • 1




    $begingroup$
    I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
    $endgroup$
    – Arnauld
    19 hours ago










  • $begingroup$
    I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
    $endgroup$
    – Erik the Outgolfer
    18 hours ago












  • $begingroup$
    @EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
    $endgroup$
    – Arnauld
    17 hours ago










  • $begingroup$
    @Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
    $endgroup$
    – Erik the Outgolfer
    17 hours ago










  • $begingroup$
    @EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
    $endgroup$
    – Arnauld
    17 hours ago
















18












$begingroup$


Challenge



This challenge will have you write a program that takes in two integers n and m and outputs the number non-intersecting loops on the n by m torus made by starting at (0,0) and only taking steps up and to the right. You can think of torus as the grid with wraparound both at the top and the bottom and on the sides.



This is code-golf so fewest bytes wins.



Example



For example, if the input is n=m=5, one valid walk is



(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (2,4) -> 
(2,0) -> (3,0) -> (4,0) -> (4,1) -> (4,2) -> (4,3) ->
(0,3) -> (1,3) -> (1,4) ->
(1,0) -> (1,1) -> (2,1) -> (3,1) -> (3,2) -> (3,3) -> (3,4) -> (4,4) ->
(0,4) -> (0,0)


as shown in the graphic.



A loop on the torus.



Some example input/outputs



f(1,1) = 2 (up or right)
f(1,2) = 2 (up or right-right)
f(2,2) = 4 (up-up, up-right-up-right, right-right, right-up-right-up)
f(2,3) = 7
f(3,3) = 22
f(2,4) = 13
f(3,4) = 66
f(4,4) = 258









share|improve this question











$endgroup$








  • 1




    $begingroup$
    I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
    $endgroup$
    – Arnauld
    19 hours ago










  • $begingroup$
    I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
    $endgroup$
    – Erik the Outgolfer
    18 hours ago












  • $begingroup$
    @EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
    $endgroup$
    – Arnauld
    17 hours ago










  • $begingroup$
    @Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
    $endgroup$
    – Erik the Outgolfer
    17 hours ago










  • $begingroup$
    @EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
    $endgroup$
    – Arnauld
    17 hours ago














18












18








18


3



$begingroup$


Challenge



This challenge will have you write a program that takes in two integers n and m and outputs the number non-intersecting loops on the n by m torus made by starting at (0,0) and only taking steps up and to the right. You can think of torus as the grid with wraparound both at the top and the bottom and on the sides.



This is code-golf so fewest bytes wins.



Example



For example, if the input is n=m=5, one valid walk is



(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (2,4) -> 
(2,0) -> (3,0) -> (4,0) -> (4,1) -> (4,2) -> (4,3) ->
(0,3) -> (1,3) -> (1,4) ->
(1,0) -> (1,1) -> (2,1) -> (3,1) -> (3,2) -> (3,3) -> (3,4) -> (4,4) ->
(0,4) -> (0,0)


as shown in the graphic.



A loop on the torus.



Some example input/outputs



f(1,1) = 2 (up or right)
f(1,2) = 2 (up or right-right)
f(2,2) = 4 (up-up, up-right-up-right, right-right, right-up-right-up)
f(2,3) = 7
f(3,3) = 22
f(2,4) = 13
f(3,4) = 66
f(4,4) = 258









share|improve this question











$endgroup$




Challenge



This challenge will have you write a program that takes in two integers n and m and outputs the number non-intersecting loops on the n by m torus made by starting at (0,0) and only taking steps up and to the right. You can think of torus as the grid with wraparound both at the top and the bottom and on the sides.



This is code-golf so fewest bytes wins.



Example



For example, if the input is n=m=5, one valid walk is



(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (2,4) -> 
(2,0) -> (3,0) -> (4,0) -> (4,1) -> (4,2) -> (4,3) ->
(0,3) -> (1,3) -> (1,4) ->
(1,0) -> (1,1) -> (2,1) -> (3,1) -> (3,2) -> (3,3) -> (3,4) -> (4,4) ->
(0,4) -> (0,0)


as shown in the graphic.



A loop on the torus.



Some example input/outputs



f(1,1) = 2 (up or right)
f(1,2) = 2 (up or right-right)
f(2,2) = 4 (up-up, up-right-up-right, right-right, right-up-right-up)
f(2,3) = 7
f(3,3) = 22
f(2,4) = 13
f(3,4) = 66
f(4,4) = 258






code-golf combinatorics grid






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







Peter Kagey

















asked yesterday









Peter KageyPeter Kagey

941519




941519








  • 1




    $begingroup$
    I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
    $endgroup$
    – Arnauld
    19 hours ago










  • $begingroup$
    I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
    $endgroup$
    – Erik the Outgolfer
    18 hours ago












  • $begingroup$
    @EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
    $endgroup$
    – Arnauld
    17 hours ago










  • $begingroup$
    @Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
    $endgroup$
    – Erik the Outgolfer
    17 hours ago










  • $begingroup$
    @EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
    $endgroup$
    – Arnauld
    17 hours ago














  • 1




    $begingroup$
    I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
    $endgroup$
    – Arnauld
    19 hours ago










  • $begingroup$
    I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
    $endgroup$
    – Erik the Outgolfer
    18 hours ago












  • $begingroup$
    @EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
    $endgroup$
    – Arnauld
    17 hours ago










  • $begingroup$
    @Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
    $endgroup$
    – Erik the Outgolfer
    17 hours ago










  • $begingroup$
    @EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
    $endgroup$
    – Arnauld
    17 hours ago








1




1




$begingroup$
I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
$endgroup$
– Arnauld
19 hours ago




$begingroup$
I was willing to bet that this sequence was on OEIS for at least $m=n$, but apparently it's not (yet).
$endgroup$
– Arnauld
19 hours ago












$begingroup$
I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
$endgroup$
– Erik the Outgolfer
18 hours ago






$begingroup$
I think a torus also has a left-right wraparound. Should we assume it only has up-down wraparound instead? The example image doesn't seem to imply as such.
$endgroup$
– Erik the Outgolfer
18 hours ago














$begingroup$
@EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
$endgroup$
– Arnauld
17 hours ago




$begingroup$
@EriktheOutgolfer The image does show the orange path wrapping around from right to left, doesn't it?
$endgroup$
– Arnauld
17 hours ago












$begingroup$
@Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
$endgroup$
– Erik the Outgolfer
17 hours ago




$begingroup$
@Arnauld Yes, but it doesn't look consistent with the challenge's description ("You can think of torus as the grid with wraparound both at the top and the bottom.")
$endgroup$
– Erik the Outgolfer
17 hours ago












$begingroup$
@EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
$endgroup$
– Arnauld
17 hours ago




$begingroup$
@EriktheOutgolfer That's true. And now that you mention it, the blue path is wrong. It should first wraparound from right to left and then from top to bottom.
$endgroup$
– Arnauld
17 hours ago










5 Answers
5






active

oldest

votes


















10












$begingroup$


Python 2, 87 bytes





f=lambda m,n,z=0,l=:z==0if z in l else sum(f(m,n,(z+d)%m%(n*1j),l+[z])for d in(1,1j))


Try it online!



The interesting thing here is using a complex number z to store the coordinate of the current position. We can move up by adding 1 and move right by adding 1j. To my surprise, modulo works on complex numbers in a way that lets us handle the wrapping for each dimension separately: doing %m acts on the real part, and %(n*1j) acts on the imaginary part.






share|improve this answer









$endgroup$













  • $begingroup$
    Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
    $endgroup$
    – Arnauld
    19 hours ago












  • $begingroup$
    @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
    $endgroup$
    – xnor
    19 hours ago






  • 1




    $begingroup$
    @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
    $endgroup$
    – xnor
    19 hours ago












  • $begingroup$
    This approach saves 5 bytes in JS. :)
    $endgroup$
    – Arnauld
    18 hours ago



















6












$begingroup$

JavaScript (ES6), 67 bytes



This shorter version is derived from a Python 3.8 alternate version found by @xnor. However, this works only for $mtimes n<32$ in JS.



Takes input as (m)(n).





m=>n=>(g=(k,l)=>l>>k&1?!k:g((k+m)%(m*n),l|=1<<k)+g(k-~k%m-k%m,l))``


Try it online!



To have it work for any input, we could use BigInts for 73 bytes:



m=>n=>(g=(k,l=k)=>l&(b=1n<<k)?!k:g((k+m)%(m*n),l|=b)+g(k-~k%m-k%m,l))(0n)


Try it online!





JavaScript (ES6),  76 73  72 bytes



Takes input as (m)(n).





m=>n=>(g=(x,y)=>g[x+=y*m]?!x:g(-~x%m,y,g[x]=1)+g(x%m,-~y%n)+--g[x])(0,0)


Try it online!



Commented



m => n => (         // m = width; n = height
g = ( // g is a recursive function taking:
x, y // the current coordinates (x, y) on the torus
) => //
g[ // the surrounding object of g is also used for storage
x += y * m // turn x into a key for the current coordinates
] ? // if this cell was already visited:
!x // return 1 if we're back to (0, 0), or 0 otherwise
: // else:
g( // first recursive call:
-~x % m, // move to the right
y, // leave y unchanged
g[x] = 1 // mark the current cell as visited by setting the flag g[x]
) + // add the result of
g( // a second recursive call:
x % m, // restore x in [0...m-1]
-~y % n // move up
) + //
--g[x] // clear the flag on the current cell
)(0, 0) // initial call to g with (x, y) = (0, 0)





share|improve this answer











$endgroup$





















    2












    $begingroup$


    Jelly, 28 bytes



    ạƝ§=1Ȧ
    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL


    A monadic Link accepting a list, [m,n], which yields the count.



    TIO-jt1qe1v9 ...although there is little point, it's way too inefficient.

    (I can't even run [2,3] locally with 16GB ram)!



    How?



    Brute force - creates coordinates of a tiled version big enough then filters the power-set of these points to those paths with neighbours only increasing by one in a single direction, then filters to those starting at a minimal coordinate (i.e. the origin) and, at the same time, removes this start coordinate from each. Then uses modulo arithmetic to wrap back to a torus and filters out any containing duplicate coordinates (i.e. those containing intersections) and, finally, filters to those with minimal ending coordinates (i.e. ending back at the origin) and yields the result's length.



    ạƝ§=1Ȧ - Link 1: all neighbours differ by 1 in exactly one direction
    Ɲ - for neighbours:
    ạ - absolute difference
    § - sum each
    =1 - equal to one (vectorises)
    Ȧ - any and all? (falsey if empty or contains a falsey value when flattened)

    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL - Main Link: list of integers, [m,n]
    ² - square (vectorises) -> [m*m, n*n]
    ‘ - increment (vectorises) -> [m*m+1, n*n+1]
    / - reduce with:
    p - Cartesian product
    ’ - decrement (vectorises) -> all the coordinates of an m*m by n*n grid
    - including [0, 0] and [m*m, n*n]
    ŒP - power-set -> all paths going either up OR right at each step, but not
    - necessarily by only 1, and
    - necessarily both up and right (e.g. [...[1,3],[5,7],[6,2],...])
    Ƈ - filter keep those for which:
    Ç - call last Link (1) as a monad
    - ...now all remaining paths do only go in steps
    - of one up or one right
    ÐṂ - filter keep those minimal under:
    Ḣ - head - removes the 1st coordinate from each and yields them for the filter
    - ...so only those which started at [0,0] but without it
    %⁸ - modulo by the left argument ([m,n]) (vectorises)
    Ƈ - filter keep those for which:
    Ƒ - is invariant when:
    Q - de-duplicated
    - ...so no repetitions of torus coordinates (and we already removed
    - the first [0,0] which must be present exactly twice)
    ÐṂ - filter keep those minimal under:
    Ṫ - tail
    - ...so only those which ended at [0,0]
    L - length





    share|improve this answer









    $endgroup$





















      2












      $begingroup$

      Haskell, 88 80 bytes



      n#m|let(x!y)a|elem(x,y)a=0^(x+y)|b<-(x,y):a=(mod(x+1)n!y)b+(x!mod(y+1)m)b=0!0$


      Try it online!



      Simple brute force: try all up/right combinations, dropping those which intersect (we keep all positions we've visited in list a) and counting those which eventually hit positition (0,0) again.



      The base case of the recursion is when we visit a position a second time (elem(x,y)a). The result is 0^0 = 1 when the position is (0,0) and counts towards the numbers of loops or 0 (0^x, with x non-zero) otherwise and doesn't increase the number of loops.



      Edit: -8 bytes thanks to @xnor.






      share|improve this answer











      $endgroup$









      • 1




        $begingroup$
        The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
        $endgroup$
        – xnor
        11 hours ago



















      0












      $begingroup$


      Jelly, 44 bytes



      ×ƝṪ2*Ḥ_2Rḃ€2ċⱮؽ%³¬ẠƲƇịØ.Ṛ,Ø.¤ZÄZ%€ʋ€³ŒQẠ$€S


      Try it online!



      Works for 4,4, but has complexity $O({2^m}^n)$ so won’t scale that well.



      This works by finding all possible routes of up to $mn$ length, filtering out those that don’t end at 0,0, and then excluding those that pass the same point twice.






      share|improve this answer









      $endgroup$













        Your Answer





        StackExchange.ifUsing("editor", function () {
        return StackExchange.using("mathjaxEditing", function () {
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        });
        });
        }, "mathjax-editing");

        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "200"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181203%2fcycles-on-the-torus%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10












        $begingroup$


        Python 2, 87 bytes





        f=lambda m,n,z=0,l=:z==0if z in l else sum(f(m,n,(z+d)%m%(n*1j),l+[z])for d in(1,1j))


        Try it online!



        The interesting thing here is using a complex number z to store the coordinate of the current position. We can move up by adding 1 and move right by adding 1j. To my surprise, modulo works on complex numbers in a way that lets us handle the wrapping for each dimension separately: doing %m acts on the real part, and %(n*1j) acts on the imaginary part.






        share|improve this answer









        $endgroup$













        • $begingroup$
          Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
          $endgroup$
          – Arnauld
          19 hours ago












        • $begingroup$
          @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
          $endgroup$
          – xnor
          19 hours ago






        • 1




          $begingroup$
          @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
          $endgroup$
          – xnor
          19 hours ago












        • $begingroup$
          This approach saves 5 bytes in JS. :)
          $endgroup$
          – Arnauld
          18 hours ago
















        10












        $begingroup$


        Python 2, 87 bytes





        f=lambda m,n,z=0,l=:z==0if z in l else sum(f(m,n,(z+d)%m%(n*1j),l+[z])for d in(1,1j))


        Try it online!



        The interesting thing here is using a complex number z to store the coordinate of the current position. We can move up by adding 1 and move right by adding 1j. To my surprise, modulo works on complex numbers in a way that lets us handle the wrapping for each dimension separately: doing %m acts on the real part, and %(n*1j) acts on the imaginary part.






        share|improve this answer









        $endgroup$













        • $begingroup$
          Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
          $endgroup$
          – Arnauld
          19 hours ago












        • $begingroup$
          @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
          $endgroup$
          – xnor
          19 hours ago






        • 1




          $begingroup$
          @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
          $endgroup$
          – xnor
          19 hours ago












        • $begingroup$
          This approach saves 5 bytes in JS. :)
          $endgroup$
          – Arnauld
          18 hours ago














        10












        10








        10





        $begingroup$


        Python 2, 87 bytes





        f=lambda m,n,z=0,l=:z==0if z in l else sum(f(m,n,(z+d)%m%(n*1j),l+[z])for d in(1,1j))


        Try it online!



        The interesting thing here is using a complex number z to store the coordinate of the current position. We can move up by adding 1 and move right by adding 1j. To my surprise, modulo works on complex numbers in a way that lets us handle the wrapping for each dimension separately: doing %m acts on the real part, and %(n*1j) acts on the imaginary part.






        share|improve this answer









        $endgroup$




        Python 2, 87 bytes





        f=lambda m,n,z=0,l=:z==0if z in l else sum(f(m,n,(z+d)%m%(n*1j),l+[z])for d in(1,1j))


        Try it online!



        The interesting thing here is using a complex number z to store the coordinate of the current position. We can move up by adding 1 and move right by adding 1j. To my surprise, modulo works on complex numbers in a way that lets us handle the wrapping for each dimension separately: doing %m acts on the real part, and %(n*1j) acts on the imaginary part.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        xnorxnor

        91.9k18187445




        91.9k18187445












        • $begingroup$
          Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
          $endgroup$
          – Arnauld
          19 hours ago












        • $begingroup$
          @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
          $endgroup$
          – xnor
          19 hours ago






        • 1




          $begingroup$
          @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
          $endgroup$
          – xnor
          19 hours ago












        • $begingroup$
          This approach saves 5 bytes in JS. :)
          $endgroup$
          – Arnauld
          18 hours ago


















        • $begingroup$
          Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
          $endgroup$
          – Arnauld
          19 hours ago












        • $begingroup$
          @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
          $endgroup$
          – xnor
          19 hours ago






        • 1




          $begingroup$
          @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
          $endgroup$
          – xnor
          19 hours ago












        • $begingroup$
          This approach saves 5 bytes in JS. :)
          $endgroup$
          – Arnauld
          18 hours ago
















        $begingroup$
        Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
        $endgroup$
        – Arnauld
        19 hours ago






        $begingroup$
        Nicely done. FWIW, my best attempt without using a complex number is 91 bytes in Python 3.8.
        $endgroup$
        – Arnauld
        19 hours ago














        $begingroup$
        @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
        $endgroup$
        – xnor
        19 hours ago




        $begingroup$
        @Arnauld Interesting idea with the k:=x+y*m. It makes me wonder if it would be shorter to use k directly for (x,y), using x+y*m rather than x+y*1j. Too bad Python 3 doesn't allow complex modulus.
        $endgroup$
        – xnor
        19 hours ago




        1




        1




        $begingroup$
        @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
        $endgroup$
        – xnor
        19 hours ago






        $begingroup$
        @Arnauld 87 bytes: tio.run/##XckxDoIwFIDh3VN0wfTRR0JbBkMsJ@ASGGkkry0NspgYr14lDhaHf/…
        $endgroup$
        – xnor
        19 hours ago














        $begingroup$
        This approach saves 5 bytes in JS. :)
        $endgroup$
        – Arnauld
        18 hours ago




        $begingroup$
        This approach saves 5 bytes in JS. :)
        $endgroup$
        – Arnauld
        18 hours ago











        6












        $begingroup$

        JavaScript (ES6), 67 bytes



        This shorter version is derived from a Python 3.8 alternate version found by @xnor. However, this works only for $mtimes n<32$ in JS.



        Takes input as (m)(n).





        m=>n=>(g=(k,l)=>l>>k&1?!k:g((k+m)%(m*n),l|=1<<k)+g(k-~k%m-k%m,l))``


        Try it online!



        To have it work for any input, we could use BigInts for 73 bytes:



        m=>n=>(g=(k,l=k)=>l&(b=1n<<k)?!k:g((k+m)%(m*n),l|=b)+g(k-~k%m-k%m,l))(0n)


        Try it online!





        JavaScript (ES6),  76 73  72 bytes



        Takes input as (m)(n).





        m=>n=>(g=(x,y)=>g[x+=y*m]?!x:g(-~x%m,y,g[x]=1)+g(x%m,-~y%n)+--g[x])(0,0)


        Try it online!



        Commented



        m => n => (         // m = width; n = height
        g = ( // g is a recursive function taking:
        x, y // the current coordinates (x, y) on the torus
        ) => //
        g[ // the surrounding object of g is also used for storage
        x += y * m // turn x into a key for the current coordinates
        ] ? // if this cell was already visited:
        !x // return 1 if we're back to (0, 0), or 0 otherwise
        : // else:
        g( // first recursive call:
        -~x % m, // move to the right
        y, // leave y unchanged
        g[x] = 1 // mark the current cell as visited by setting the flag g[x]
        ) + // add the result of
        g( // a second recursive call:
        x % m, // restore x in [0...m-1]
        -~y % n // move up
        ) + //
        --g[x] // clear the flag on the current cell
        )(0, 0) // initial call to g with (x, y) = (0, 0)





        share|improve this answer











        $endgroup$


















          6












          $begingroup$

          JavaScript (ES6), 67 bytes



          This shorter version is derived from a Python 3.8 alternate version found by @xnor. However, this works only for $mtimes n<32$ in JS.



          Takes input as (m)(n).





          m=>n=>(g=(k,l)=>l>>k&1?!k:g((k+m)%(m*n),l|=1<<k)+g(k-~k%m-k%m,l))``


          Try it online!



          To have it work for any input, we could use BigInts for 73 bytes:



          m=>n=>(g=(k,l=k)=>l&(b=1n<<k)?!k:g((k+m)%(m*n),l|=b)+g(k-~k%m-k%m,l))(0n)


          Try it online!





          JavaScript (ES6),  76 73  72 bytes



          Takes input as (m)(n).





          m=>n=>(g=(x,y)=>g[x+=y*m]?!x:g(-~x%m,y,g[x]=1)+g(x%m,-~y%n)+--g[x])(0,0)


          Try it online!



          Commented



          m => n => (         // m = width; n = height
          g = ( // g is a recursive function taking:
          x, y // the current coordinates (x, y) on the torus
          ) => //
          g[ // the surrounding object of g is also used for storage
          x += y * m // turn x into a key for the current coordinates
          ] ? // if this cell was already visited:
          !x // return 1 if we're back to (0, 0), or 0 otherwise
          : // else:
          g( // first recursive call:
          -~x % m, // move to the right
          y, // leave y unchanged
          g[x] = 1 // mark the current cell as visited by setting the flag g[x]
          ) + // add the result of
          g( // a second recursive call:
          x % m, // restore x in [0...m-1]
          -~y % n // move up
          ) + //
          --g[x] // clear the flag on the current cell
          )(0, 0) // initial call to g with (x, y) = (0, 0)





          share|improve this answer











          $endgroup$
















            6












            6








            6





            $begingroup$

            JavaScript (ES6), 67 bytes



            This shorter version is derived from a Python 3.8 alternate version found by @xnor. However, this works only for $mtimes n<32$ in JS.



            Takes input as (m)(n).





            m=>n=>(g=(k,l)=>l>>k&1?!k:g((k+m)%(m*n),l|=1<<k)+g(k-~k%m-k%m,l))``


            Try it online!



            To have it work for any input, we could use BigInts for 73 bytes:



            m=>n=>(g=(k,l=k)=>l&(b=1n<<k)?!k:g((k+m)%(m*n),l|=b)+g(k-~k%m-k%m,l))(0n)


            Try it online!





            JavaScript (ES6),  76 73  72 bytes



            Takes input as (m)(n).





            m=>n=>(g=(x,y)=>g[x+=y*m]?!x:g(-~x%m,y,g[x]=1)+g(x%m,-~y%n)+--g[x])(0,0)


            Try it online!



            Commented



            m => n => (         // m = width; n = height
            g = ( // g is a recursive function taking:
            x, y // the current coordinates (x, y) on the torus
            ) => //
            g[ // the surrounding object of g is also used for storage
            x += y * m // turn x into a key for the current coordinates
            ] ? // if this cell was already visited:
            !x // return 1 if we're back to (0, 0), or 0 otherwise
            : // else:
            g( // first recursive call:
            -~x % m, // move to the right
            y, // leave y unchanged
            g[x] = 1 // mark the current cell as visited by setting the flag g[x]
            ) + // add the result of
            g( // a second recursive call:
            x % m, // restore x in [0...m-1]
            -~y % n // move up
            ) + //
            --g[x] // clear the flag on the current cell
            )(0, 0) // initial call to g with (x, y) = (0, 0)





            share|improve this answer











            $endgroup$



            JavaScript (ES6), 67 bytes



            This shorter version is derived from a Python 3.8 alternate version found by @xnor. However, this works only for $mtimes n<32$ in JS.



            Takes input as (m)(n).





            m=>n=>(g=(k,l)=>l>>k&1?!k:g((k+m)%(m*n),l|=1<<k)+g(k-~k%m-k%m,l))``


            Try it online!



            To have it work for any input, we could use BigInts for 73 bytes:



            m=>n=>(g=(k,l=k)=>l&(b=1n<<k)?!k:g((k+m)%(m*n),l|=b)+g(k-~k%m-k%m,l))(0n)


            Try it online!





            JavaScript (ES6),  76 73  72 bytes



            Takes input as (m)(n).





            m=>n=>(g=(x,y)=>g[x+=y*m]?!x:g(-~x%m,y,g[x]=1)+g(x%m,-~y%n)+--g[x])(0,0)


            Try it online!



            Commented



            m => n => (         // m = width; n = height
            g = ( // g is a recursive function taking:
            x, y // the current coordinates (x, y) on the torus
            ) => //
            g[ // the surrounding object of g is also used for storage
            x += y * m // turn x into a key for the current coordinates
            ] ? // if this cell was already visited:
            !x // return 1 if we're back to (0, 0), or 0 otherwise
            : // else:
            g( // first recursive call:
            -~x % m, // move to the right
            y, // leave y unchanged
            g[x] = 1 // mark the current cell as visited by setting the flag g[x]
            ) + // add the result of
            g( // a second recursive call:
            x % m, // restore x in [0...m-1]
            -~y % n // move up
            ) + //
            --g[x] // clear the flag on the current cell
            )(0, 0) // initial call to g with (x, y) = (0, 0)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 16 hours ago

























            answered 20 hours ago









            ArnauldArnauld

            78.1k795326




            78.1k795326























                2












                $begingroup$


                Jelly, 28 bytes



                ạƝ§=1Ȧ
                ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL


                A monadic Link accepting a list, [m,n], which yields the count.



                TIO-jt1qe1v9 ...although there is little point, it's way too inefficient.

                (I can't even run [2,3] locally with 16GB ram)!



                How?



                Brute force - creates coordinates of a tiled version big enough then filters the power-set of these points to those paths with neighbours only increasing by one in a single direction, then filters to those starting at a minimal coordinate (i.e. the origin) and, at the same time, removes this start coordinate from each. Then uses modulo arithmetic to wrap back to a torus and filters out any containing duplicate coordinates (i.e. those containing intersections) and, finally, filters to those with minimal ending coordinates (i.e. ending back at the origin) and yields the result's length.



                ạƝ§=1Ȧ - Link 1: all neighbours differ by 1 in exactly one direction
                Ɲ - for neighbours:
                ạ - absolute difference
                § - sum each
                =1 - equal to one (vectorises)
                Ȧ - any and all? (falsey if empty or contains a falsey value when flattened)

                ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL - Main Link: list of integers, [m,n]
                ² - square (vectorises) -> [m*m, n*n]
                ‘ - increment (vectorises) -> [m*m+1, n*n+1]
                / - reduce with:
                p - Cartesian product
                ’ - decrement (vectorises) -> all the coordinates of an m*m by n*n grid
                - including [0, 0] and [m*m, n*n]
                ŒP - power-set -> all paths going either up OR right at each step, but not
                - necessarily by only 1, and
                - necessarily both up and right (e.g. [...[1,3],[5,7],[6,2],...])
                Ƈ - filter keep those for which:
                Ç - call last Link (1) as a monad
                - ...now all remaining paths do only go in steps
                - of one up or one right
                ÐṂ - filter keep those minimal under:
                Ḣ - head - removes the 1st coordinate from each and yields them for the filter
                - ...so only those which started at [0,0] but without it
                %⁸ - modulo by the left argument ([m,n]) (vectorises)
                Ƈ - filter keep those for which:
                Ƒ - is invariant when:
                Q - de-duplicated
                - ...so no repetitions of torus coordinates (and we already removed
                - the first [0,0] which must be present exactly twice)
                ÐṂ - filter keep those minimal under:
                Ṫ - tail
                - ...so only those which ended at [0,0]
                L - length





                share|improve this answer









                $endgroup$


















                  2












                  $begingroup$


                  Jelly, 28 bytes



                  ạƝ§=1Ȧ
                  ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL


                  A monadic Link accepting a list, [m,n], which yields the count.



                  TIO-jt1qe1v9 ...although there is little point, it's way too inefficient.

                  (I can't even run [2,3] locally with 16GB ram)!



                  How?



                  Brute force - creates coordinates of a tiled version big enough then filters the power-set of these points to those paths with neighbours only increasing by one in a single direction, then filters to those starting at a minimal coordinate (i.e. the origin) and, at the same time, removes this start coordinate from each. Then uses modulo arithmetic to wrap back to a torus and filters out any containing duplicate coordinates (i.e. those containing intersections) and, finally, filters to those with minimal ending coordinates (i.e. ending back at the origin) and yields the result's length.



                  ạƝ§=1Ȧ - Link 1: all neighbours differ by 1 in exactly one direction
                  Ɲ - for neighbours:
                  ạ - absolute difference
                  § - sum each
                  =1 - equal to one (vectorises)
                  Ȧ - any and all? (falsey if empty or contains a falsey value when flattened)

                  ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL - Main Link: list of integers, [m,n]
                  ² - square (vectorises) -> [m*m, n*n]
                  ‘ - increment (vectorises) -> [m*m+1, n*n+1]
                  / - reduce with:
                  p - Cartesian product
                  ’ - decrement (vectorises) -> all the coordinates of an m*m by n*n grid
                  - including [0, 0] and [m*m, n*n]
                  ŒP - power-set -> all paths going either up OR right at each step, but not
                  - necessarily by only 1, and
                  - necessarily both up and right (e.g. [...[1,3],[5,7],[6,2],...])
                  Ƈ - filter keep those for which:
                  Ç - call last Link (1) as a monad
                  - ...now all remaining paths do only go in steps
                  - of one up or one right
                  ÐṂ - filter keep those minimal under:
                  Ḣ - head - removes the 1st coordinate from each and yields them for the filter
                  - ...so only those which started at [0,0] but without it
                  %⁸ - modulo by the left argument ([m,n]) (vectorises)
                  Ƈ - filter keep those for which:
                  Ƒ - is invariant when:
                  Q - de-duplicated
                  - ...so no repetitions of torus coordinates (and we already removed
                  - the first [0,0] which must be present exactly twice)
                  ÐṂ - filter keep those minimal under:
                  Ṫ - tail
                  - ...so only those which ended at [0,0]
                  L - length





                  share|improve this answer









                  $endgroup$
















                    2












                    2








                    2





                    $begingroup$


                    Jelly, 28 bytes



                    ạƝ§=1Ȧ
                    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL


                    A monadic Link accepting a list, [m,n], which yields the count.



                    TIO-jt1qe1v9 ...although there is little point, it's way too inefficient.

                    (I can't even run [2,3] locally with 16GB ram)!



                    How?



                    Brute force - creates coordinates of a tiled version big enough then filters the power-set of these points to those paths with neighbours only increasing by one in a single direction, then filters to those starting at a minimal coordinate (i.e. the origin) and, at the same time, removes this start coordinate from each. Then uses modulo arithmetic to wrap back to a torus and filters out any containing duplicate coordinates (i.e. those containing intersections) and, finally, filters to those with minimal ending coordinates (i.e. ending back at the origin) and yields the result's length.



                    ạƝ§=1Ȧ - Link 1: all neighbours differ by 1 in exactly one direction
                    Ɲ - for neighbours:
                    ạ - absolute difference
                    § - sum each
                    =1 - equal to one (vectorises)
                    Ȧ - any and all? (falsey if empty or contains a falsey value when flattened)

                    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL - Main Link: list of integers, [m,n]
                    ² - square (vectorises) -> [m*m, n*n]
                    ‘ - increment (vectorises) -> [m*m+1, n*n+1]
                    / - reduce with:
                    p - Cartesian product
                    ’ - decrement (vectorises) -> all the coordinates of an m*m by n*n grid
                    - including [0, 0] and [m*m, n*n]
                    ŒP - power-set -> all paths going either up OR right at each step, but not
                    - necessarily by only 1, and
                    - necessarily both up and right (e.g. [...[1,3],[5,7],[6,2],...])
                    Ƈ - filter keep those for which:
                    Ç - call last Link (1) as a monad
                    - ...now all remaining paths do only go in steps
                    - of one up or one right
                    ÐṂ - filter keep those minimal under:
                    Ḣ - head - removes the 1st coordinate from each and yields them for the filter
                    - ...so only those which started at [0,0] but without it
                    %⁸ - modulo by the left argument ([m,n]) (vectorises)
                    Ƈ - filter keep those for which:
                    Ƒ - is invariant when:
                    Q - de-duplicated
                    - ...so no repetitions of torus coordinates (and we already removed
                    - the first [0,0] which must be present exactly twice)
                    ÐṂ - filter keep those minimal under:
                    Ṫ - tail
                    - ...so only those which ended at [0,0]
                    L - length





                    share|improve this answer









                    $endgroup$




                    Jelly, 28 bytes



                    ạƝ§=1Ȧ
                    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL


                    A monadic Link accepting a list, [m,n], which yields the count.



                    TIO-jt1qe1v9 ...although there is little point, it's way too inefficient.

                    (I can't even run [2,3] locally with 16GB ram)!



                    How?



                    Brute force - creates coordinates of a tiled version big enough then filters the power-set of these points to those paths with neighbours only increasing by one in a single direction, then filters to those starting at a minimal coordinate (i.e. the origin) and, at the same time, removes this start coordinate from each. Then uses modulo arithmetic to wrap back to a torus and filters out any containing duplicate coordinates (i.e. those containing intersections) and, finally, filters to those with minimal ending coordinates (i.e. ending back at the origin) and yields the result's length.



                    ạƝ§=1Ȧ - Link 1: all neighbours differ by 1 in exactly one direction
                    Ɲ - for neighbours:
                    ạ - absolute difference
                    § - sum each
                    =1 - equal to one (vectorises)
                    Ȧ - any and all? (falsey if empty or contains a falsey value when flattened)

                    ²‘p/’ŒPÇƇḢÐṂ%⁸QƑƇṪÐṂL - Main Link: list of integers, [m,n]
                    ² - square (vectorises) -> [m*m, n*n]
                    ‘ - increment (vectorises) -> [m*m+1, n*n+1]
                    / - reduce with:
                    p - Cartesian product
                    ’ - decrement (vectorises) -> all the coordinates of an m*m by n*n grid
                    - including [0, 0] and [m*m, n*n]
                    ŒP - power-set -> all paths going either up OR right at each step, but not
                    - necessarily by only 1, and
                    - necessarily both up and right (e.g. [...[1,3],[5,7],[6,2],...])
                    Ƈ - filter keep those for which:
                    Ç - call last Link (1) as a monad
                    - ...now all remaining paths do only go in steps
                    - of one up or one right
                    ÐṂ - filter keep those minimal under:
                    Ḣ - head - removes the 1st coordinate from each and yields them for the filter
                    - ...so only those which started at [0,0] but without it
                    %⁸ - modulo by the left argument ([m,n]) (vectorises)
                    Ƈ - filter keep those for which:
                    Ƒ - is invariant when:
                    Q - de-duplicated
                    - ...so no repetitions of torus coordinates (and we already removed
                    - the first [0,0] which must be present exactly twice)
                    ÐṂ - filter keep those minimal under:
                    Ṫ - tail
                    - ...so only those which ended at [0,0]
                    L - length






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 11 hours ago









                    Jonathan AllanJonathan Allan

                    52.6k535170




                    52.6k535170























                        2












                        $begingroup$

                        Haskell, 88 80 bytes



                        n#m|let(x!y)a|elem(x,y)a=0^(x+y)|b<-(x,y):a=(mod(x+1)n!y)b+(x!mod(y+1)m)b=0!0$


                        Try it online!



                        Simple brute force: try all up/right combinations, dropping those which intersect (we keep all positions we've visited in list a) and counting those which eventually hit positition (0,0) again.



                        The base case of the recursion is when we visit a position a second time (elem(x,y)a). The result is 0^0 = 1 when the position is (0,0) and counts towards the numbers of loops or 0 (0^x, with x non-zero) otherwise and doesn't increase the number of loops.



                        Edit: -8 bytes thanks to @xnor.






                        share|improve this answer











                        $endgroup$









                        • 1




                          $begingroup$
                          The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                          $endgroup$
                          – xnor
                          11 hours ago
















                        2












                        $begingroup$

                        Haskell, 88 80 bytes



                        n#m|let(x!y)a|elem(x,y)a=0^(x+y)|b<-(x,y):a=(mod(x+1)n!y)b+(x!mod(y+1)m)b=0!0$


                        Try it online!



                        Simple brute force: try all up/right combinations, dropping those which intersect (we keep all positions we've visited in list a) and counting those which eventually hit positition (0,0) again.



                        The base case of the recursion is when we visit a position a second time (elem(x,y)a). The result is 0^0 = 1 when the position is (0,0) and counts towards the numbers of loops or 0 (0^x, with x non-zero) otherwise and doesn't increase the number of loops.



                        Edit: -8 bytes thanks to @xnor.






                        share|improve this answer











                        $endgroup$









                        • 1




                          $begingroup$
                          The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                          $endgroup$
                          – xnor
                          11 hours ago














                        2












                        2








                        2





                        $begingroup$

                        Haskell, 88 80 bytes



                        n#m|let(x!y)a|elem(x,y)a=0^(x+y)|b<-(x,y):a=(mod(x+1)n!y)b+(x!mod(y+1)m)b=0!0$


                        Try it online!



                        Simple brute force: try all up/right combinations, dropping those which intersect (we keep all positions we've visited in list a) and counting those which eventually hit positition (0,0) again.



                        The base case of the recursion is when we visit a position a second time (elem(x,y)a). The result is 0^0 = 1 when the position is (0,0) and counts towards the numbers of loops or 0 (0^x, with x non-zero) otherwise and doesn't increase the number of loops.



                        Edit: -8 bytes thanks to @xnor.






                        share|improve this answer











                        $endgroup$



                        Haskell, 88 80 bytes



                        n#m|let(x!y)a|elem(x,y)a=0^(x+y)|b<-(x,y):a=(mod(x+1)n!y)b+(x!mod(y+1)m)b=0!0$


                        Try it online!



                        Simple brute force: try all up/right combinations, dropping those which intersect (we keep all positions we've visited in list a) and counting those which eventually hit positition (0,0) again.



                        The base case of the recursion is when we visit a position a second time (elem(x,y)a). The result is 0^0 = 1 when the position is (0,0) and counts towards the numbers of loops or 0 (0^x, with x non-zero) otherwise and doesn't increase the number of loops.



                        Edit: -8 bytes thanks to @xnor.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 5 hours ago

























                        answered 17 hours ago









                        niminimi

                        32.3k32388




                        32.3k32388








                        • 1




                          $begingroup$
                          The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                          $endgroup$
                          – xnor
                          11 hours ago














                        • 1




                          $begingroup$
                          The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                          $endgroup$
                          – xnor
                          11 hours ago








                        1




                        1




                        $begingroup$
                        The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                        $endgroup$
                        – xnor
                        11 hours ago




                        $begingroup$
                        The base cases can be combined into |elem(x,y)a=0^(x+y), and (0!0) can be 0!0$.
                        $endgroup$
                        – xnor
                        11 hours ago











                        0












                        $begingroup$


                        Jelly, 44 bytes



                        ×ƝṪ2*Ḥ_2Rḃ€2ċⱮؽ%³¬ẠƲƇịØ.Ṛ,Ø.¤ZÄZ%€ʋ€³ŒQẠ$€S


                        Try it online!



                        Works for 4,4, but has complexity $O({2^m}^n)$ so won’t scale that well.



                        This works by finding all possible routes of up to $mn$ length, filtering out those that don’t end at 0,0, and then excluding those that pass the same point twice.






                        share|improve this answer









                        $endgroup$


















                          0












                          $begingroup$


                          Jelly, 44 bytes



                          ×ƝṪ2*Ḥ_2Rḃ€2ċⱮؽ%³¬ẠƲƇịØ.Ṛ,Ø.¤ZÄZ%€ʋ€³ŒQẠ$€S


                          Try it online!



                          Works for 4,4, but has complexity $O({2^m}^n)$ so won’t scale that well.



                          This works by finding all possible routes of up to $mn$ length, filtering out those that don’t end at 0,0, and then excluding those that pass the same point twice.






                          share|improve this answer









                          $endgroup$
















                            0












                            0








                            0





                            $begingroup$


                            Jelly, 44 bytes



                            ×ƝṪ2*Ḥ_2Rḃ€2ċⱮؽ%³¬ẠƲƇịØ.Ṛ,Ø.¤ZÄZ%€ʋ€³ŒQẠ$€S


                            Try it online!



                            Works for 4,4, but has complexity $O({2^m}^n)$ so won’t scale that well.



                            This works by finding all possible routes of up to $mn$ length, filtering out those that don’t end at 0,0, and then excluding those that pass the same point twice.






                            share|improve this answer









                            $endgroup$




                            Jelly, 44 bytes



                            ×ƝṪ2*Ḥ_2Rḃ€2ċⱮؽ%³¬ẠƲƇịØ.Ṛ,Ø.¤ZÄZ%€ʋ€³ŒQẠ$€S


                            Try it online!



                            Works for 4,4, but has complexity $O({2^m}^n)$ so won’t scale that well.



                            This works by finding all possible routes of up to $mn$ length, filtering out those that don’t end at 0,0, and then excluding those that pass the same point twice.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 6 hours ago









                            Nick KennedyNick Kennedy

                            56127




                            56127






























                                draft saved

                                draft discarded




















































                                If this is an answer to a challenge…




                                • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                  Explanations of your answer make it more interesting to read and are very much encouraged.


                                • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                More generally…




                                • …Please make sure to answer the question and provide sufficient detail.


                                • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181203%2fcycles-on-the-torus%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to label and detect the document text images

                                Vallis Paradisi

                                Tabula Rosettana