📜  吉尔布雷斯猜想产生的数字三角形

📅  最后修改于: 2021-05-05 02:19:10             🧑  作者: Mango

任务是找到由吉尔布雷斯猜想产生的数字三角形。
吉尔布雷斯的猜想:
可以看出,给定一个质数序列,可以通过给定序列的第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

方法:

  1. Gilbreath序列的(n,k)项由下给出
    • 其中n> 0
    • F(0,k)是第k个素数,其中n = 0

  1. 定义一个递归函数,我们可以在映射中映射第(n,k)项并将其存储以减少计算量。我们将在0行中填入素数。
  2. 沿反对角向上移动吉尔布雷斯三角形,因此我们将从n = 0,k = 0开始,并在每个步骤中增加k并减小n(如果n <0),则我们将分配n = k和k = 0,在此步骤中我们可以对角向上对角线穿过三角形。
  3. 我们已经在0行填充了100个素数。如果需要查找级数更大的项,则可以增加素数。

下面是上述方法的实现:

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