原文:

wythoff 数组是从序列中导出的无限整数矩阵。矩阵中的每个正整数只出现一次。 t3【怀瑟夫阵:t5】

  1    2    3    5    8    13    ...
  4    7    11   18   29   47    ...
  6    10   16   26   42   68    ...
  9    15   24   39   63   102   ...
  12   20   32   52   84   136   ...
  14   23   37   60   97   157   ...
  .    .    .    .    .    .
  .    .    .    .    .    . 

如果 a m,n 表示第 mth 行和第n列中的元素,则

  • a m,1 = [[mφ]φ]
  • a m,2=[【mφ】φ2
  • a m,n = a m,n-2 a m,n-1 代表 n > 2
  • φ = (1 √5) / 2

如果我们从左上角元素开始以反对角线方式遍历矩阵,那么 wythoff 序列:

1, 2, 4, 3, 7, 6, 5, 11, 10, 9….

对于给定的 n ,任务首先打印序列的 n 号。 例:

输入: n = 10 输出: 1、2、4、3、7、6、5、11、10、9 输入: n = 15 输出: 1、2、4、3、7、6、5、11、10、9、8、18、16、15、12

进场: 以上循环可修改为

  • t(n,-1) = n-1,如果 k = -1
  • t(n,0) = [n*φ],如果 k = 0
  • t(n,k) = t(n,k-1) t(n,k-2),如果 k > 0
  • φ = (1 √5) / 2

所以我们可以递归地找到 t(n,k) 的值,对于 t = 0 和对于t =–1 有两种基本情况。我们将这些值存储在中,并在需要时使用它来减少计算。在我们得到数组后,我们必须以反对角线的方式遍历它,所以我们设置 i=0j= 0 并减少 j 并增加 ij < 0 时,我们初始化 j = ii = 0 。 我们还保留一个计数,当显示一个数字时,该计数会增加。当计数达到要求的值时,我们中断数组。 以下是上述方法的实施:

卡片打印处理机(card print processor 的缩写)

// c   program to find wythoff array
#include 
using namespace std;
// function to find the n, k term of wythoff array
int wythoff(map >& mp, int n, int k)
{
    // tau = (sqrt(5) 1)/2
    double tau = (sqrt(5)   1) / 2.0, t_n_k;
    // already_stored
    if (mp[n][k] != 0)
        return mp[n][k];
    // t(n, -1) = n-1.
    if (k == -1) {
        return n - 1;
    }
    // t(n, 0) = floor(n*tau).
    else if (k == 0) {
        t_n_k = floor(n * tau);
    }
    // t(n, k) = t(n, k-1)   t(n, k-2) for k>=1.
    else
    {
        t_n_k = wythoff(mp, n, k - 1)  
                             wythoff(mp, n, k - 2);
    }
    // store
    mp[n][k] = t_n_k;
    // return the ans
    return (int)t_n_k;
}
// function to find  first n terms of wythoff
// array by traversing in anti-diagonal
void wythoff_array(int n)
{
    int i = 0, j = 0, count = 0;
    // map to store the wythoff array
    map > mp;
    while (count < n) {
        cout << wythoff(mp, i   1, j   1);
        count  ;
        if(count != n)
            cout << ", ";
        // anti diagonal
        i  ;
        j--;
        if (j < 0) {
            j = i;
            i = 0;
        }
    }
}
// driver code
int main()
{
    int n = 15;
    // function call
    wythoff_array(n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find wythoff array
import java.util.*;
public class gfg
{
  // function to find the n, k term of wythoff array
  static int wythoff(hashmap> mp,
                     int n, int k)
  {
    // tau = (sqrt(5) 1)/2
    double tau = (math.sqrt(5)   1) / 2.0, t_n_k;
    // already_stored
    if (mp.containskey(n) &&
        mp.get(n).containskey(k) &&
        mp.get(n).get(k) != 0)
      return mp.get(n).get(k);
    // t(n, -1) = n-1.
    if (k == -1)
    {
      return n - 1;
    }
    // t(n, 0) = floor(n*tau).
    else if (k == 0)
    {
      t_n_k = math.floor(n * tau);
    }
    // t(n, k) = t(n, k-1)   t(n, k-2) for k>=1.
    else
    {
      t_n_k = wythoff(mp, n, k - 1)  
        wythoff(mp, n, k - 2);
    }
    // store
    mp.put(n, new hashmap(k,(int)t_n_k));
    // return the ans
    return (int)t_n_k;
  }
  // function to find  first n terms of wythoff
  // array by traversing in anti-diagonal
  static void wythoff_array(int n)
  {
    int i = 0, j = 0, count = 0;
    // map to store the wythoff array
    hashmap> mp =
      new hashmap>();
    while (count < n)
    {
      system.out.print(wythoff(mp, i   1, j   1));
      count  ;
      if(count != n)
        system.out.print(", ");
      // anti diagonal
      i  ;
      j--;
      if (j < 0)
      {
        j = i;
        i = 0;
      }
    }
  }
  // driver code
  public static void main(string[] args)
  {
    int n = 15;
    // function call
    wythoff_array(n);
  }
}
// this code is contributed by divyeshrabadiya07.

python 3

# python3 program to find wythoff array
import math
# function to find the n, k term of wythoff array
def wythoff(mp, n, k):
    # tau = (sqrt(5) 1)/2
    tau = (math.sqrt(5)   1) / 2
    t_n_k = 0
    # already_stored
    if ((n in mp) and (k in mp[n])):
        return mp[n][k];
    # t(n, -1) = n-1.
    if (k == -1):
        return n - 1;
    # t(n, 0) = floor(n*tau).
    elif (k == 0):
        t_n_k = math.floor(n * tau);
    # t(n, k) = t(n, k-1)   t(n, k-2) for k>=1.
    else:
        t_n_k = wythoff(mp, n, k - 1)   wythoff(mp, n, k - 2)
    # store
    if n not in mp:
        mp[n] = dict()
    mp[n][k] = t_n_k;
    # return the ans
    return int(t_n_k)
# function to find  first n terms of wythoff
# array by traversing in anti-diagonal
def wythoff_array(n):
    i = 0
    j = 0
    count = 0;
    # map to store the wythoff array
    mp = dict()
    while (count < n):
        print(wythoff(mp, i   1, j   1), end = '')
        count  = 1
        if(count != n):
            print(", ", end = '')
        # anti diagonal
        i  = 1
        j -= 1
        if (j < 0):
            j = i;
            i = 0;
# driver code
if __name__=='__main__':
    n = 15;
    # function call
    wythoff_array(n);
    # this code is contributed by rutvik_56

c

// c# program to find wythoff array
using system;
using system.collections.generic;
class gfg
{
  // function to find the n, k term of wythoff array
  static int wythoff(dictionary> mp, int n, int k)
  {
    // tau = (sqrt(5) 1)/2
    double tau = (math.sqrt(5)   1) / 2.0, t_n_k;
    // already_stored
    if (mp.containskey(n) && mp[n].containskey(k) && mp[n][k] != 0)
      return mp[n][k];
    // t(n, -1) = n-1.
    if (k == -1) {
      return n - 1;
    }
    // t(n, 0) = floor(n*tau).
    else if (k == 0)
    {
      t_n_k = math.floor(n * tau);
    }
    // t(n, k) = t(n, k-1)   t(n, k-2) for k>=1.
    else
    {
      t_n_k = wythoff(mp, n, k - 1)   wythoff(mp, n, k - 2);
    }
    // store
    if(!mp.containskey(n))
    {
      mp[n] = new dictionary();
    }
    mp[n][k] = (int)t_n_k;
    // return the ans
    return (int)t_n_k;
  }
  // function to find  first n terms of wythoff
  // array by traversing in anti-diagonal
  static void wythoff_array(int n)
  {
    int i = 0, j = 0, count = 0;
    // map to store the wythoff array
    dictionary> mp = new dictionary>();
    while (count < n)
    {
      console.write(wythoff(mp, i   1, j   1));
      count  ;
      if(count != n)
        console.write(", ");
      // anti diagonal
      i  ;
      j--;
      if (j < 0) {
        j = i;
        i = 0;
      }
    }
  }
  // driver code
  static void main()
  {
    int n = 15;
    // function call
    wythoff_array(n);
  }
}
// this code is contributed by divyesh072019.

输出:

1, 2, 4, 3, 7, 6, 5, 11, 10, 9, 8, 18, 16, 15, 12, 

参考:t2https://oeis.org/a035513