Wythoff数组是从斐波那契数列派生的整数的无限矩阵。矩阵中的每个正整数仅出现一次。
Wythoff数组:
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表示第m行第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个数字。
例子:
Input : N = 10
Output : 1, 2, 4, 3, 7, 6, 5, 11, 10, 9
Input : N = 15
Output : 1, 2, 4, 3, 7, 6, 5, 11, 10, 9, 8, 18, 16, 15, 12
方法:
上面的提示可以修改为
- 如果k = -1,则T(n,-1)= n-1
- 如果k = 0,则T(n,0)= [n *φ]
- 如果k> 0,则T(n,k)= T(n,k-1)+ T(n,k-2)
- φ=(1 +√5)/ 2
因此,我们可以在t = 0和t = – 1的两个基本情况下递归找到T(n,k)的值。我们将这些值存储在映射中,并在需要减少计算量时使用它。得到数组后,我们必须以反对角线的方式遍历它,因此我们将i = 0和j = 0设置为j,然后在j <0时减小j并增加i ,从而初始化j = i和i = 0 。
当数字显示时,我们还会保留增加的计数。当计数达到所需值时,我们将中断数组。
下面是上述方法的实现:
CPP
// 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.
Python3
# 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,
参考: https : //oeis.org/A035513