C++ 中的维思序列

2025 年 5 月 23 日 | 阅读 9 分钟

引言

遵循黄金比例,Wythoff 序列是组合博弈论中用于玩家移动的数学组合。它以 Willem Abraham Wythoff 的名字命名,他根据斐波那契数创建了一个与黄金分割有着奇特关系的序列。在本文中,我们将讨论 Wythoff 序列并提供一篇关于生成它的 C++ 代码的文章。

问题陈述

Wythoff 序列由两个交错序列组成,分别称为下 Wythoff 序列和上 Wythoff 序列,表示为

A(n) = floor(n * φ)

B(n) = A(n) + n

其中 φ (phi) = (1 + sqrt(5)) / 2 是黄金比例。

我们的目标是使用 C++ 生成并显示 Wythoff 序列的前 N 项。

数学背景

Wythoff 序列与黄金比例密切相关,这确保了数字分布良好而不会聚集。序列定义为

  • 下 Wythoff 序列: A(n) = floor(n * φ)
  • 上 Wythoff 序列: B(n) = A(n) + n

这些序列在 Wythoff 游戏和 Nim 等组合游戏中扮演着关键角色。

示例 1

让我们看一个 C++ 程序来生成并显示 Wythoff 序列的前 N 项。

输出

 
Enter the number of terms: 10
Wythoff sequence (first 10 terms):
Lower Sequence: 1 3 4 6 8 9 11 12 14 16
Upper Sequence: 2 5 7 10 13 15 18 20 23 26   

代码解释

  • 设置 PHI 的值: 我们事先通过 (1 + sqrt(5)) / 2 计算 φ。
  • 在 for 循环中生成序列: 使用 floor(n * PHI) 计算下序列,使用 floor(n * PHI)+n 计算上序列。
  • 用户输入 N 的值,程序依次返回两个序列的前 N 个元素。

示例 2

让我们再举一个例子来说明 C++ 中的Wythoff 序列

输出

 
Enter the number of terms: 20

Wythoff sequence (first 20 terms):
Lower Sequence: 1 3 4 6 8 9 11 12 14 16 17 19 21 22 24 25 27 29 30 32 
Upper Sequence: 2 5 7 10 13 15 18 20 23 26 28 31 34 36 39 41 44 47 49 52 
Enter a number to search in the sequence: 23
23 is present in the sequence.
Sequence saved to wythoff_sequence.txt   

Wythoff 序列的应用

  • 在组合博弈论中,它用于 Wythoff 游戏和其他类似的两人游戏。
  • 在数学中,它出现在与斐波那契和黄金比例相关的研究中。
  • 在优化中,它有助于在两个或多个方之间分配资源。

示例 3

让我们再举一个例子来说明 C++ 中的Wythoff 序列

输出

 
Enter the number of terms for Wythoff sequence: 30

Sequence generated in 6 ms.

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 1

Wythoff sequence (first 30 terms):
Lower Sequence: 4 19 1 40 3 45 6 8 9 11 12 46 48 14 16 17 21 22 24 25 27 29 30 32 33 35 37 38 42 43 
Upper Sequence: 7 31 2 65 5 73 10 13 15 18 20 75 78 23 26 28 34 36 39 41 44 47 49 52 54 57 60 62 68 70 

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 2
Enter number to search: 39
39 is in the sequence.

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 2
Enter number to search: 43
43 is in the sequence.

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 2
Enter number to search: 1000
1000 is NOT in the sequence.

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 3
Sequence saved to wythoff_sequence.txt

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 4

ASCII Representation of Wythoff Sequence:
  1: **** (4)
  2: ******************* (19)
  3: * (1)
  4: **************************************** (40)
  5: *** (3)
  6: ********************************************* (45)
  7: ****** (6)
  8: ******** (8)
  9: ********* (9)
 10: *********** (11)
 11: ************ (12)
 12: ********************************************** (46)
 13: ************************************************ (48)
 14: ************** (14)
 15: **************** (16)
 16: ***************** (17)
 17: ********************* (21)
 18: ********************** (22)
 19: ************************ (24)
 20: ************************* (25)
 21: *************************** (27)
 22: ***************************** (29)
 23: ****************************** (30)
 24: ******************************** (32)
 25: ********************************* (33)
 26: *********************************** (35)
 27: ************************************* (37)
 28: ************************************** (38)
 29: ****************************************** (42)
 30: ******************************************* (43)

===== Wythoff Sequence Generator =====
1. Generate & Display Sequence
2. Search for a Number in the Sequence
3. Save Sequence to a File
4. Visualize Sequence
5. Exit
======================================
Enter your choice: 5
Exiting program...   

结论

总之,Wythoff 序列优雅地阐释了数论和组合数学之间的相互作用。通过利用黄金比例,可以创建具有实际应用的特殊序列。此处提供的 C++ 实现有效地计算了序列,使得试验不同的 N 值变得容易。