任务是找到由吉尔布雷斯猜想产生的数字三角形。
吉尔布雷斯的猜想:
可以观察到,给定素数的序列,序列可通过第i个和第(i + 1)的给定序列的第术语和给定过程之间的绝对差,形成可重复以形成数字的三角形。这个数字形成吉尔布雷斯猜想三角形的元素。
吉尔布雷斯三角形的形成如下:
- 让我们取质数:2, 3, 5, 7。
- 现在相邻素数的差是:1,2,2。
- 现在相邻元素之间的差异是:1, 0。
- 现在相邻元素之间的区别是:1。
- 这样,吉尔布雷斯三角形就形成为:
2 3 5 7
1 2 2
1 0
1
- 这个三角形将反对角向上读为
2, 1, 3, 1, 2, 5, 1, 0, 2, 7,
例子:
Input: n = 10
Output: 2, 1, 3, 1, 2,
5, 1, 0, 2, 7,
Input: n = 15
Output: 2, 1, 3, 1, 2,
5, 1, 0, 2, 7,
1, 2, 2, 4, 11
方法:
- Gilbreath 序列的第(n, k) 项由下式给出
- 其中n>0 ,
- F(0, k)是第 k 个素数,其中n = 0 。
F(n, k) = |F(n – 1, k + 1) – F(n – 1, k)|
- 定义一个递归函数,我们可以在图中(N,K)个项映射,并将其储存,以减少计算量。我们将填补素数的0个行。
- 逆对角向上遍历吉尔布雷斯三角形,因此我们将从 n = 0,k = 0 开始,并且在每一步中增加 k 并减少 n 如果 n<0 那么我们将分配 n=k 和 k = 0,在此我们可以逆对角向上遍历三角形的方式。
- 我们用 100 个素数填充了第0 行。如果我们需要找到更大的级数,我们可以增加素数。
下面是上述方法的实现:
CPP14
// C++ code for printing the Triangle of numbers
// arising from Gilbreath's conjecture
#include
using namespace std;
// Check whether the number
// is prime or not
bool is_Prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
// Set the 0th row of the matrix
// with c primes from 0, 0 to 0, c-1
void set_primes(map >& mp,
map >& hash,
int c)
{
int count = 0;
for (int i = 2; count < c; i++) {
if (is_Prime(i)) {
mp[0][count++] = i;
hash[0][count - 1] = 1;
}
}
}
// Find the n, k term of matrix of
// Gilbreath's conjecture
int Gilbreath(map >& mp,
map >& hash,
int n, int k)
{
if (hash[n][k] != 0)
return mp[n][k];
// recursively find
int ans
= abs(Gilbreath(mp, hash, n - 1, k + 1)
- Gilbreath(mp, hash, n - 1, k));
// store the ans
mp[n][k] = ans;
return ans;
}
// Print first n terms of Gilbreath sequence
// successive absolute differences of primes
// read by antidiagonals upwards.
void solve(int n)
{
int i = 0, j = 0, count = 0;
// map to store the matrix
// and hash to check if the
// element is present or not
map > mp, hash;
// set the primes of first row
set_primes(mp, hash, 100);
while (count < n) {
// print the Gilbreath number
cout << Gilbreath(mp, hash, i, j)
<< ", ";
// increase the count
count++;
// anti diagonal upwards
i--;
j++;
if (i < 0) {
i = j;
j = 0;
}
}
}
// Driver code
int main()
{
int n = 15;
solve(n);
return 0;
}
Java
// Java code for printing the Triangle of numbers
// arising from Gilbreath's conjecture
import java.util.*;
public class GFG
{
// Check whether the number
// is prime or not
static boolean is_Prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
// Set the 0th row of the matrix
// with c primes from 0, 0 to 0, c-1
static void set_primes(HashMap> mp,
HashMap> hash,
int c)
{
int count = 0;
for (int i = 2; count < c; i++)
{
if (is_Prime(i))
{
if(!mp.containsKey(0))
{
mp.put(0, new HashMap());
}
if(!mp.get(0).containsKey(count))
{
mp.get(0).put(count, i);
}
else
{
mp.get(0).put(count, i);
}
count++;
if(!hash.containsKey(0))
{
hash.put(0, new HashMap());
}
if(!hash.get(0).containsKey(count - 1))
{
hash.get(0).put(count - 1, 1);
}
else
{
hash.get(0).put(count - 1, 1);
}
}
}
}
// Find the n, k term of matrix of
// Gilbreath's conjecture
static int Gilbreath(HashMap> mp,
HashMap> hash,
int n, int k)
{
if (hash.containsKey(n) && hash.get(n).containsKey(k) && hash.get(n).get(k) != 0)
return mp.get(n).get(k);
// recursively find
int ans
= Math.abs(Gilbreath(mp, hash, n - 1, k + 1)
- Gilbreath(mp, hash, n - 1, k));
// store the ans
if(!mp.containsKey(n))
{
mp.put(n, new HashMap());
}
mp.get(n).put(k, ans);
return ans;
}
// Print first n terms of Gilbreath sequence
// successive absolute differences of primes
// read by antidiagonals upwards.
static void solve(int n)
{
int i = 0, j = 0, count = 0;
// map to store the matrix
// and hash to check if the
// element is present or not
HashMap> mp =
new HashMap>();
HashMap> hash =
new HashMap>();
// set the primes of first row
set_primes(mp, hash, 100);
while (count < n) {
// print the Gilbreath number
System.out.print(Gilbreath(mp, hash, i, j) + ", ");
// increase the count
count++;
// anti diagonal upwards
i--;
j++;
if (i < 0)
{
i = j;
j = 0;
}
}
}
// Driver code
public static void main(String[] args) {
int n = 15;
solve(n);
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 code for printing the Triangle of numbers
# arising from Gilbreath's conjecture
import math
# Check whether the number
# is prime or not
def is_Prime(n):
if (n < 2):
return False;
for i in range(2, int(math.sqrt(n)) + 1):
if (n % i == 0):
return False;
return True;
# Set the 0th row of the matrix
# with c primes from 0, 0 to 0, c-1
def set_primes(mp, hash, c):
count = 0;
i = 2
while(count < c):
if (is_Prime(i)):
if 0 not in mp:
mp[0] = dict()
mp[0][count] = i;
count += 1
if 0 not in hash:
hash[0] = dict()
hash[0][count - 1] = 1;
i += 1
# Find the n, k term of matrix of
# Gilbreath's conjecture
def Gilbreath(mp, hash, n, k):
if (n in hash and k in hash[n] and hash[n][k] != 0):
return mp[n][k];
# recursively find
ans = abs(Gilbreath(mp, hash, n - 1, k + 1)
- Gilbreath(mp, hash, n - 1, k));
if n not in mp:
mp[n] = dict()
# store the ans
mp[n][k] = ans;
return ans;
# Print first n terms of Gilbreath sequence
# successive absolute differences of primes
# read by antidiagonals upwards.
def solve(n):
i = 0
j = 0
count = 0;
# map to store the matrix
# and hash to check if the
# element is present or not
mp = dict()
hash = dict()
# set the primes of first row
set_primes(mp, hash, 100);
while (count < n):
# print the Gilbreath number
print(Gilbreath(mp, hash, i, j), end = ', ')
# increase the count
count += 1
# anti diagonal upwards
i -= 1
j += 1
if (i < 0):
i = j;
j = 0;
# Driver code
if __name__=='__main__':
n = 15;
solve(n);
# This code is contributed by rutvik_56.
C#
// C# code for printing the Triangle of numbers
// arising from Gilbreath's conjecture
using System;
using System.Collections.Generic;
class GFG
{
// Check whether the number
// is prime or not
static bool is_Prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i <= Math.Sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
// Set the 0th row of the matrix
// with c primes from 0, 0 to 0, c-1
static void set_primes(Dictionary> mp,
Dictionary> hash,
int c)
{
int count = 0;
for (int i = 2; count < c; i++)
{
if (is_Prime(i))
{
if(!mp.ContainsKey(0))
{
mp[0] = new Dictionary();
}
if(!mp[0].ContainsKey(count))
{
mp[0].Add(count, i);
}
else
{
mp[0][count] = i;
}
count++;
if(!hash.ContainsKey(0))
{
hash[0] = new Dictionary();
}
if(!hash[0].ContainsKey(count - 1))
{
hash[0].Add(count - 1, 1);
}
else
{
hash[0][count - 1] = 1;
}
}
}
}
// Find the n, k term of matrix of
// Gilbreath's conjecture
static int Gilbreath(Dictionary> mp,
Dictionary> hash,
int n, int k)
{
if (hash.ContainsKey(n) && hash[n].ContainsKey(k) && hash[n][k] != 0)
return mp[n][k];
// recursively find
int ans
= Math.Abs(Gilbreath(mp, hash, n - 1, k + 1)
- Gilbreath(mp, hash, n - 1, k));
// store the ans
if(!mp.ContainsKey(n))
{
mp[n] = new Dictionary();
}
mp[n][k] = ans;
return ans;
}
// Print first n terms of Gilbreath sequence
// successive absolute differences of primes
// read by antidiagonals upwards.
static void solve(int n)
{
int i = 0, j = 0, count = 0;
// map to store the matrix
// and hash to check if the
// element is present or not
Dictionary> mp =
new Dictionary>();
Dictionary> hash =
new Dictionary>();
// set the primes of first row
set_primes(mp, hash, 100);
while (count < n) {
// print the Gilbreath number
Console.Write(Gilbreath(mp, hash, i, j) + ", ");
// increase the count
count++;
// anti diagonal upwards
i--;
j++;
if (i < 0)
{
i = j;
j = 0;
}
}
}
// Driver code
static void Main()
{
int n = 15;
solve(n);
}
}
// This code is contributed by divyeshrabadiya07.
输出:
2, 1, 3, 1, 2, 5, 1, 0, 2, 7, 1, 2, 2, 4, 11,
参考:http://oeis.org/A036262