Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Question

Tweeted twitter.com/StackCodeGolf/status/948579861609746433
Post Reopened by user202729, Οurous, Steadybox, Wheat Wizard , pajonk
Fixed examples; added 294 characters in body
Source Link
DonielF
  • 773
  • 6
  • 11

You may use any delimiter to differentiate between the pairs, between each number in the pairs, and between each loop, provided that they are three distinct chars or strings. Above, I divided the numbers using commas, the pairs using ->’s, and the loops using boring instructions. In my examples below, I will use bracketsparentheses around each pair, commas between each number within a pair, and new lines between each loop.

Credit to @WheatWizard’s code for fixing up my examples list. As I said in an earlier draft, I was sure I was missing a few since I was doing it by hand, but boy was I missing some.

Input: 2
Output: {(2}{)(1,1})

{2,1}{(3)(1,2})
{3}{(1,2})(2,1)
{(3}{)(1,2)(2,1})
{(4},{)(2,2})(1,{3)
(1,3})(3,1)
{(4},{)(2,2})(1,{3)(3,1})
{(4)(2,2})(3,{1)(1,3})
{2(3,1)(1,3)
(4)(2},{2)(3,1})
{(5}{)(2,3}{)(1,4})
{5}{(1,4)(3,2}{)
(2,3)(1,4)(3,2)(4,1})
{(5)(2,3}{)(1,4})(3,2)(4,1)
{(2,3)(4,1)
(5)(2}{,3)(4,1})
{(6}{)(3,3}{)(1,5})
{6}{3(1,3}{5)(4,2)(2,4)
(4,2)(2,4)
(1,5)(4,2)(5,1})(2,4)
{(4,2}{)(5,1)(2,4})
{(6}{)(3,3}{)(1,5}{)(4,2}{)(5,1})
{(6}{)(3,3}{)(5,1}{)(2,4}{)(1,5})
{(2,4)(1,5}{)(4,2}{)
(5,1)(2,4}
{)(1,5}{)(4,2}{)
(2,4)(4,2)
(5,1}{)(2,4})(4,2)
(6)(3,3)(5,1)
{2(7)(3,5}{4)(1,6)
(1,6)(4,3})(2,5)
{(2,5}{)(5,2})
{(3,4}{5)(1,2}
{7}{6)(4,3}{)(2,5)(5,2)(6,1})
{(7}{)(3,4}{)(1,6}
{7}{)(4,3}{)(2,5}{)(5,2)(6,1)
(3,4)(1,6})(4,3)(6,1)
{(7}{)(3,4}{5)(1,2}{6)(4,3)(6,1})
{(7}{)(3,4}{)(5,2}{)(2,5}{)(1,6})
{7}{(2,5)(1,6)(4,3}{2)
(3,5}{4)(5,2}{6)(2,5)(1}
{,6)(4,3)(6,1}{)
(7)(3,4}{)(5,2)(2,5)(1,6}{)(4,3}
{)(6,1}{)
(5,2)(2,5)
(3,4}{)(5,2}
{)(6,1)
(7)(3,6}{4)(5,3}{2)(6,5}1)

(I may have missed some loops in the higher numbers, so if you catch any that I missed, feel free to let me know. I drafted these lists by hand, not by code, and I’ve edited some mistakes I’ve caught.)

You may use any delimiter to differentiate between the pairs, between each number in the pairs, and between each loop, provided that they are three distinct chars or strings. Above, I divided the numbers using commas, the pairs using ->’s, and the loops using boring instructions. In my examples below, I will use brackets around each pair, commas between each number within a pair, and new lines between each loop.

Input: 2
Output: {2}{1,1}

{2,1}{1,2}
{3}{1,2}
{3}{2,1}
{4},{2,2},{1,3}
{4},{2,2},{3,1}
{2,2},{1,3}
{2,2},{3,1}
{5}{2,3}{1,4}
{5}{3,2}{4,1}
{2,3}{1,4}
{3,2}{4,1}
{6}{3,3}{1,5}
{6}{3,3}{5,1}
{4,2}{2,4}
{6}{3,3}{1,5}{4,2}{5,1}
{6}{3,3}{5,1}{2,4}{1,5}
{1,5}{4,2}{2,4}
{1,5}{4,2}{5,1}{2,4}
{2,5}{4,3}
{2,5}{5,2}
{3,4}{5,2}
{7}{4,3}{6,1}
{7}{3,4}{1,6}
{7}{4,3}{2,5}{1,6}
{7}{3,4}{5,2}{6,1}
{7}{3,4}{5,2}{2,5}{1,6}
{7}{4,3}{2,5}{5,2}{6,1}
{6,1}{3,4}{1,6}{4,3}
{6,1}{3,4}{5,2}
{1,6}{4,3}{2,5}

(I may have missed some loops in the higher numbers, so if you catch any that I missed, feel free to let me know. I drafted these lists by hand, not by code, and I’ve edited some mistakes I’ve caught.)

You may use any delimiter to differentiate between the pairs, between each number in the pairs, and between each loop, provided that they are three distinct chars or strings. Above, I divided the numbers using commas, the pairs using ->’s, and the loops using boring instructions. In my examples below, I will use parentheses around each pair, commas between each number within a pair, and new lines between each loop.

Credit to @WheatWizard’s code for fixing up my examples list. As I said in an earlier draft, I was sure I was missing a few since I was doing it by hand, but boy was I missing some.

Input: 2
Output: (2)(1,1)

(3)(1,2)
(1,2)(2,1)
(3)(1,2)(2,1)
(4)(2,2)(1,3)
(1,3)(3,1)
(4)(2,2)(1,3)(3,1)
(4)(2,2)(3,1)(1,3)
(3,1)(1,3)
(4)(2,2)(3,1)
(5)(2,3)(1,4)
(1,4)(3,2)
(2,3)(1,4)(3,2)(4,1)
(5)(2,3)(1,4)(3,2)(4,1)
(2,3)(4,1)
(5)(2,3)(4,1)
(6)(3,3)(1,5)
(1,5)(4,2)(2,4)
(4,2)(2,4)
(1,5)(4,2)(5,1)(2,4)
(4,2)(5,1)(2,4)
(6)(3,3)(1,5)(4,2)(5,1)
(6)(3,3)(5,1)(2,4)(1,5)
(2,4)(1,5)(4,2)
(5,1)(2,4)(1,5)(4,2)
(2,4)(4,2)
(5,1)(2,4)(4,2)
(6)(3,3)(5,1)
(7)(3,4)(1,6)
(1,6)(4,3)(2,5)
(2,5)(5,2)
(3,4)(1,6)(4,3)(2,5)(5,2)(6,1)
(7)(3,4)(1,6)(4,3)(2,5)(5,2)(6,1)
(3,4)(1,6)(4,3)(6,1)
(7)(3,4)(1,6)(4,3)(6,1)
(7)(3,4)(5,2)(2,5)(1,6)
(2,5)(1,6)(4,3)
(3,4)(5,2)(2,5)(1,6)(4,3)(6,1)
(7)(3,4)(5,2)(2,5)(1,6)(4,3)(6,1)
(5,2)(2,5)
(3,4)(5,2)(6,1)
(7)(3,4)(5,2)(6,1)
added 45 characters in body
Source Link
DonielF
  • 773
  • 6
  • 11
{6}{3,3}{1,5}
{6}{3,3}{5,1}
{4,2}{2,4}
{6}{3,3}{1,5}{4,2}{5,1}
{6}{3,3}{5,1}{2,4}{1,5}
{1,5}{4,2}{2,4}
{1,5}{4,2}{5,1}{2,4}
{6}{3,3}{1,5}
{6}{3,3}{5,1}
{4,2}{2,4}
{6}{3,3}{1,5}{4,2}{5,1}
{6}{3,3}{5,1}{2,4}{1,5}
{6}{3,3}{1,5}
{6}{3,3}{5,1}
{4,2}{2,4}
{6}{3,3}{1,5}{4,2}{5,1}
{6}{3,3}{5,1}{2,4}{1,5}
{1,5}{4,2}{2,4}
{1,5}{4,2}{5,1}{2,4}
Attempted to clarify, tweaked some rules, fixed examples
Source Link
DonielF
  • 773
  • 6
  • 11

Integer Division Loops and Stacks

Challenge

  1. DivideTake the input in halfand divide it into two halves. IfFor all divisions in this program, if the input is odd, round one half up and one half down. (Exex: {7}{ -> 3,4}.) You now have two numbers, not (henceforth "stacks"7 -> 3.5,3.5).
  2. Divide either stacknumber in half, then take the larger halfof these two new halves and add it back to the other stacknumber that wasn’t split. Ex: {2}{3,4 -> (1,2),4 -> 1}. You are allowed to completely exhaust a stack, i.e. continue dividing it until you end up with one empty stack and one stack identical to the original one. This would be outputted as {x}, rather than {0,x}6. Ex: or {3,4 -> 3,(2}{1,1}{2}) -> 5,2.
  3. Repeat step 2 until you return toreach a set of stacks that has been used previously.
  4. Output the loopyou have seen before. One output for each loop; whileEx: {5 -> 3,2}{ -> (1,2),2 -> 1,4 -> 1},(2,2) -> 3,2 and. Since we’ve seen {13,1}{2} are both valid outputs for an inputbefore, we may stop repeating. You may completely exhaust a stack in the process of doing this. Ex: 5 -> 3,2 -> (1,2),2 -> 1,4 -> (0,1),4 -> 0,5, since they describe.
  5. Output each pair in the same loop, only one of them should be outputted (i. This also excludes two sequences that aree. the same loop going inabove without the reverse orderintermediate steps, likefrom the first appearance of a pair until the second, but not including the second). Ex: {3}{1,2}{ -> 1,4. If the input is included, don’t output a 0 with it - 5 -> 3,2 -> 1,4, not 0,5 -> 3,2 -> 1},4. Output may be in any convenient type.
  6. Start back from stepRepeat steps 1 and divide-4 by splitting the stackspairs differently to get different loops. Reiterate until all possible loops have been exhausted.

I/O

Input is a positive integer larger than 1 and smaller than either the maximum integer supported by your language or the maximum integer that won’t crash the computer, whichever is smaller.

Output is the loops from above in any format you want (string, list, array, etc.). Trailing white space is allowed.

Do not output the same loop twice, nor different versions of the same loop. Ex: 2 -> 1,1 and 1,1 -> 2 are both valid loops, but they describe the same loop, beginning at different points on the loop. Likewise, two loops that are identical but go in the reverse order should not be outputted. Ex: 3 -> 1,2 -> 2,1 and 3 -> 2,1 -> 1,2 are the same loop, but they go in the opposite direction from each other.

You may use any delimitersdelimiter to dividedifferentiate between the stackspairs, between each number in the pairs, and loopsbetween each loop, provided that they are all different from each otherthree distinct chars or strings. Above, I divided the numbers using commas, the pairs using ->’s, and the loops using boring instructions. In my examples below, I usedwill use brackets to divide pairsaround each pair, commas to divide stacksbetween each number within a pair, and new lines to divide loopsbetween each loop.

Input: 1
Output: {1}

Input: 2
Output: {2}{1,1}

{42,35}{3,4,3}
{2,5}{45,32}
{3,4}{5,2}
{7}{4,3}{6,1}
{7}{3,4}{1,6}
{7}{4,3}{2,5}{1,6}
{7}{3,4}{5,2}{6,1}
{7}{3,4}{5,2}{2,5}{1,6}
{7}{4,3}{2,5}{5,2}{6,1}
{6,1}{3,4}{1,6}{4,3}
{6,1}{3,4}{5,2}
{1,6}{4,3}{2,5}

(I may have missed some loops in the higher numbers, so if you catch any that I missed, feel free to let me know. I drafted these lists by hand, not by code, and I’ve edited some mistakes I’ve caught.)

Division Loops and Stacks

  1. Divide the input in half. If the input is odd, round one half up and one down. (Ex: {7}{3,4}.) You now have two numbers (henceforth "stacks").
  2. Divide either stack in half, then take the larger half and add it to the other stack. Ex: {2}{1,1}. You are allowed to completely exhaust a stack, i.e. continue dividing it until you end up with one empty stack and one stack identical to the original one. This would be outputted as {x}, rather than {0,x}. Ex: {2}{1,1}{2}
  3. Repeat step 2 until you return to a set of stacks that has been used previously.
  4. Output the loop. One output for each loop; while {2}{1,1} and {1,1}{2} are both valid outputs for an input of 2, since they describe the same loop, only one of them should be outputted. This also excludes two sequences that are the same loop going in the reverse order, like {3}{1,2}{2,1}. Output may be in any convenient type.
  5. Start back from step 1 and divide the stacks differently to get different loops. Reiterate until all possible loops have been exhausted.

Trailing white space is allowed. You may use any delimiters to divide the stacks, pairs, and loops, provided that they are all different from each other. In my examples below I used brackets to divide pairs, commas to divide stacks, and new lines to divide loops.

Input: 1
Output: {1}

Input: 2
Output: {2}{1,1}

{4,3}{3,4}
{2,5}{4,3}
{3,4}{5,2}
{7}{4,3}{6,1}
{7}{3,4}{1,6}
{7}{4,3}{2,5}{1,6}
{7}{3,4}{5,2}{6,1}
{6,1}{3,4}{1,6}{4,3}
{6,1}{3,4}{5,2}
{1,6}{4,3}{2,5}

(I may have missed some loops in the higher numbers, so if you catch any that I missed, feel free to let me know. I drafted these lists by hand, not by code.)

Integer Division Loops

Challenge

  1. Take the input and divide it into two halves. For all divisions in this program, if the input is odd, round one half up and one half down (ex: 7 -> 3,4, not 7 -> 3.5,3.5).
  2. Divide either number in half, then take the larger of these two new halves and add it back to the number that wasn’t split. Ex: 3,4 -> (1,2),4 -> 1,6 or 3,4 -> 3,(2,2) -> 5,2.
  3. Repeat step 2 until you reach a set you have seen before. Ex: 5 -> 3,2 -> (1,2),2 -> 1,4 -> 1,(2,2) -> 3,2. Since we’ve seen 3,2 before, we may stop repeating. You may completely exhaust a stack in the process of doing this. Ex: 5 -> 3,2 -> (1,2),2 -> 1,4 -> (0,1),4 -> 0,5.
  4. Output each pair in the loop (i.e. the above without the intermediate steps, from the first appearance of a pair until the second, but not including the second). Ex: 3,2 -> 1,4. If the input is included, don’t output a 0 with it - 5 -> 3,2 -> 1,4, not 0,5 -> 3,2 -> 1,4.
  5. Repeat steps 1-4 by splitting the pairs differently.

I/O

Input is a positive integer larger than 1 and smaller than either the maximum integer supported by your language or the maximum integer that won’t crash the computer, whichever is smaller.

Output is the loops from above in any format you want (string, list, array, etc.). Trailing white space is allowed.

Do not output the same loop twice, nor different versions of the same loop. Ex: 2 -> 1,1 and 1,1 -> 2 are both valid loops, but they describe the same loop, beginning at different points on the loop. Likewise, two loops that are identical but go in the reverse order should not be outputted. Ex: 3 -> 1,2 -> 2,1 and 3 -> 2,1 -> 1,2 are the same loop, but they go in the opposite direction from each other.

You may use any delimiter to differentiate between the pairs, between each number in the pairs, and between each loop, provided that they are three distinct chars or strings. Above, I divided the numbers using commas, the pairs using ->’s, and the loops using boring instructions. In my examples below, I will use brackets around each pair, commas between each number within a pair, and new lines between each loop.

Input: 2
Output: {2}{1,1}

{2,5}{4,3}
{2,5}{5,2}
{3,4}{5,2}
{7}{4,3}{6,1}
{7}{3,4}{1,6}
{7}{4,3}{2,5}{1,6}
{7}{3,4}{5,2}{6,1}
{7}{3,4}{5,2}{2,5}{1,6}
{7}{4,3}{2,5}{5,2}{6,1}
{6,1}{3,4}{1,6}{4,3}
{6,1}{3,4}{5,2}
{1,6}{4,3}{2,5}

(I may have missed some loops in the higher numbers, so if you catch any that I missed, feel free to let me know. I drafted these lists by hand, not by code, and I’ve edited some mistakes I’ve caught.)

added 69 characters in body
Source Link
DonielF
  • 773
  • 6
  • 11
Loading
deleted 16 characters in body
Source Link
DonielF
  • 773
  • 6
  • 11
Loading
added 41 characters in body
Source Link
DonielF
  • 773
  • 6
  • 11
Loading
Post Closed as "Needs details or clarity" by Martin Ender
fixed formatting
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
DonielF
  • 773
  • 6
  • 11
Loading

AltStyle によって変換されたページ (->オリジナル) /