Mathematica 中的 Riffling 卡片

发布于 2024-12-25 20:20:06 字数 932 浏览 3 评论 0 原文

我的朋友向我提出了这个问题;感觉很想在这里分享。

给定一副牌,我们将其分成两组,并“将它们交错排列”;让我们将此操作称为“拆分连接”。并在生成的牌组上重复相同的操作。

例如{ 1, 2, 3, 4 } 变为 { 1, 2 } & { 3, 4 } (拆分) 我们得到 { 1, 3, 2, 4 } (合并) >

此外,如果我们有奇数张牌,即 { 1, 2, 3 },我们可以像 { 1, 2 } & 那样将其拆分。 { 3 }(较大的一半在前)通向 { 1, 3, 2 } (即,n 分为 Ceil[n/2]n-Ceil[n/2]

我朋友的问题问我的是:

需要多少这样的拆分连接才能恢复原始牌组?

这让我想知道:

,则需要多少分割连接

  • 如果这副牌有 n 张牌,如果n 是偶数
  • n 是奇数吗?
  • n 是 '2' 的幂吗? [我发现我们需要 log (n) (base 2) 分割连接数...]
  • (随意探索这样的不同场景。)

是否有一个简单的模式/公式/ n 与所需拆分连接数量相关的概念?

我相信,这是在 Mathematica 中探索的一件好事,特别是因为它提供了 Riffle[] 方法。

My friend posed this question to me; felt like sharing it here.

Given a deck of cards, we split it into 2 groups, and "interleave them"; let us call this operation a 'split-join'. And repeat the same operation on the resulting deck.

E.g., { 1, 2, 3, 4 } becomes { 1, 2 } & { 3, 4 } (split) and we get { 1, 3, 2, 4 } (join)

Also, if we have an odd number of cards i.e., { 1, 2, 3 } we can split it like { 1, 2 } & { 3 } (bigger-half first) leading to { 1, 3, 2 }
(i.e., n is split up as Ceil[n/2] & n-Ceil[n/2])

The question my friend asked me was:

HOW many such split-joins are needed to get the original deck back?

And that got me wondering:

If the deck has n cards, what is the number of split-joins needed if:

  • n is even ?
  • n is odd ?
  • n is a power of '2' ? [I found that we then need log (n) (base 2) number of split-joins...]
  • (Feel free to explore different scenarios like that.)

Is there a simple pattern/formula/concept correlating n and the number of split-joins required?

I believe, this is a good thing to explore in Mathematica, especially, since it provides the Riffle[] method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲娇萝莉攻 2025-01-01 20:20:06

引用MathWorld

将一副 n=2, 4, ... 的牌恢复到原来的顺序所需的洗牌次数为 1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6 , 11, ...(Sloane 的 A002326),这只是 2 (mod n-1) 的乘法阶。例如,一副 52 张牌因此在八次洗牌后返回到其原始状态,因为 2**8=1 (mod 51) (Golomb 1961)。需要 1, 2, 3, ... 洗牌才能恢复到牌组原始状态的最小数量的牌 2n 是 1, 2, 4, 3, 16, 5, 64, 9, 37, 6, ... .(斯隆的 A114894)。

未解决 n 为奇数的情况。

请注意,本文还包括一个具有探索 out-shuffle 功能的 Mathematica 笔记本

To quote MathWorld:

The numbers of out-shuffles needed to return a deck of n=2, 4, ... to its original order are 1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, ... (Sloane's A002326), which is simply the multiplicative order of 2 (mod n-1). For example, a deck of 52 cards therefore is returned to its original state after eight out-shuffles, since 2**8=1 (mod 51) (Golomb 1961). The smallest numbers of cards 2n that require 1, 2, 3, ... out-shuffles to return to the deck's original state are 1, 2, 4, 3, 16, 5, 64, 9, 37, 6, ... (Sloane's A114894).

The case when n is odd isn't addressed.

Note that the article also includes a Mathematica notebook with functions to explore out-shuffles.

や莫失莫忘 2025-01-01 20:20:06

如果我们有奇数张牌n==2m-1,并且如果我们将牌分开,使得在每次洗牌期间第一组包含m张牌,第二组包含m张牌m-1 张牌,并且将组连接起来,使得同一组中没有两张牌彼此相邻,则所需的洗牌次数等于 MultiplicativeOrder[2, n ]

为了说明这一点,我们注意到,在洗牌一次后,位于位置 k 的卡片已移动到位置 2k,其中 0<=k并为 2k-2m+1 表示 m<=k<2m-1,其中 k 是这样的0<=k<2m-1。写为模n==2m-1,这意味着对于所有0<=k,新位置为Mod[2k, n] 。因此,为了让每张牌返回到其原始位置,我们需要 N 次洗牌,其中 N 满足 Mod[2^N k, n]==Mod[ k, n] 对于所有 0<=k ,其中 NMultiplicativeOrder[2, n]< 的任意倍数/代码>。

请注意,由于对称性,如果我们以相反的方式拆分牌组,结果将完全相同,即第一组始终包含 m-1 卡,第二组始终包含 m< /code> 卡。我不知道如果你交替的话会发生什么,即对于奇数洗牌,第一组包含 m 卡,对于偶数洗牌 m-1 卡。

If we have an odd number of cards n==2m-1, and if we split the cards such that during each shuffle the first group contains m cards, the second group m-1 cards, and the groups are joined such that no two cards of the same group end up next to each other, then the number of shuffles needed is equal to MultiplicativeOrder[2, n].

To show this, we note that after one shuffle the card which was at position k has moved to position 2k for 0<=k<m and to 2k-2m+1 for m<=k<2m-1, where k is such that 0<=k<2m-1. Written modulo n==2m-1 this means that the new position is Mod[2k, n] for all 0<=k<n. Therefore, for each card to return to its original position we need N shuffles where N is such that Mod[2^N k, n]==Mod[k, n] for all 0<=k<n from which is follows that N is any multiple of MultiplicativeOrder[2, n].

Note that due to symmetry the result would have been exactly the same if we had split the deck the other way around, i.e. the first group always contains m-1 cards and the second group m cards. I don't know what would happen if you alternate, i.e. for odd shuffles the first group contains m cards, and for even shuffles m-1 cards.

季末如歌 2025-01-01 20:20:06

魔术师/数学家 Persi Diaconnis 的旧作讲述了如何通过完美的 riffle shuffle 来恢复顺序。 Ian Stewart 在他 1998 年《科学美国人》数学休闲专栏中写到了这项工作 - 请参阅,例如: http://www.whydomath.org/Reading_Room_Material/ian_stewart/shuffle/shuffle.html

There's old work by magician/mathematician Persi Diaconnis about restoring the order with perfect riffle shuffles. Ian Stewart wrote about that work in one of his 1998 Scientific American Mathematical Recreation columns -- see, e.g.: http://www.whydomath.org/Reading_Room_Material/ian_stewart/shuffle/shuffle.html

脸赞 2025-01-01 20:20:06

我知道老问题,但奇怪的是没有人提出实际的数学解决方案。

 countrifflecards[deck_] := Module[{n = Length@deck, ct, rifdeck},
      ct = 0;
      rifdeck = 
        Riffle @@ 
          Partition[ # , Ceiling[ n/2], Ceiling[ n/2], {1, 1}, {} ] &;
      NestWhile[(++ct; rifdeck[#]) &, deck, #2 != deck &,2 ]; ct]

这处理偶数和奇数情况:

 countrifflecards[RandomSample[ Range[#], #]] & /@ Range[2, 52, 2]

{1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36,
12, 20, 14, 12, 23, 21, 8}

 countrifflecards[RandomSample[ Range[#], #]] & /@ Range[3, 53, 2]

{2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36, 12,
20、14、12、23、21、8、52}

您可以很容易地证明,如果您将一张牌添加到奇数情况下,多余的牌将保留在底部并且不会改变顺序,因此奇数情况结果只是n+1 偶数结果..

 ListPlot[{#, countrifflecards[RandomSample[ Range[#], #]]} & /@ 
      Range[2, 1000]]

在此处输入图像描述

old question I know, but strange no one put up an actual mathematica solution..

 countrifflecards[deck_] := Module[{n = Length@deck, ct, rifdeck},
      ct = 0;
      rifdeck = 
        Riffle @@ 
          Partition[ # , Ceiling[ n/2], Ceiling[ n/2], {1, 1}, {} ] &;
      NestWhile[(++ct; rifdeck[#]) &, deck, #2 != deck &,2 ]; ct]

This handles even and odd cases:

 countrifflecards[RandomSample[ Range[#], #]] & /@ Range[2, 52, 2]

{1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36,
12, 20, 14, 12, 23, 21, 8}

 countrifflecards[RandomSample[ Range[#], #]] & /@ Range[3, 53, 2]

{2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36, 12,
20, 14, 12, 23, 21, 8, 52}

You can readily show if you add a card to the odd-case the extra card will stay on the bottom and not change the sequence, hence the odd case result is just the n+1 even result..

 ListPlot[{#, countrifflecards[RandomSample[ Range[#], #]]} & /@ 
      Range[2, 1000]]

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文