📌  相关文章
📜  给定范围[L,R]中不同Primonacci数的计数

📅  最后修改于: 2021-04-21 22:02:08             🧑  作者: Mango

给定两个整数[L,R] ,任务是计算范围[L,R]中Primonacci Numbers数

例子:

方法:
可以使用动态编程和Eratosthenes筛子解决该问题。请按照以下步骤解决问题:

  • 使用Eratosthenes筛生成所有素数。
  • 初始化一个HashSet来存储不同的Primonacci数字。
  • 初始化数组dp [] ,使dp [i]存储第iPrimonacci Number
  • 设置dp [1] = dp [2] = 1
  • 对于每个i ,迭代小于i的所有素数p并通过添加dp [i – p]来更新dp [i ]
  • 如果dp [i][L,R]范围内,则插入HashSet中
  • 最后,如果dp [i]超过R ,则打印HashSet的大小。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Stores list of all primes
static vector primes;
int M = 100005;
 
// Function to find all primes
void sieve()
{
     
    // To mark the prime ones
    bool mark[M];
    for(int i = 0; i < M; i++)
       mark[i] = false;
 
    // Initially all indices as prime
    for(int i = 2; i < M; i++)
        mark[i] = true;
 
    for(int i = 2; i * i < M; i++)
    {
 
        // If i is prime
        if (mark[i])
        {
 
            // Set all multiples
            // of i as non-prime
            for(int j = i * i;
                    j < M; j += i)
                mark[j] = false;
        }
    }
 
    // Adding all primes to a list
    for(int i = 2; i < M; i++)
        if (mark[i])
            primes.push_back(i);
}
 
// Function to return the count of
// Primonacci Numbers in the range [l, r]
void countPrimonacci(int l, int r)
{
 
    // dp[i] contains ith Primonacci Number
    vector dp;
    dp.push_back(1);
    dp.push_back(1);
    int i = 2;
 
    // Stores the Primonacci Numbers
    set s;
    while (true)
    {
        int x = 0;
 
        // Iterate over all smaller primes
        for(int j = 0; j < primes.size(); j++)
        {
            int p = primes[j];
 
            if (p >= i)
                break;
 
            x += dp[i - p];
        }
 
        // If Primonacci number lies
        // within the range [L, R]
        if (x >= l && x <= r)
            s.insert(x);
 
        if (x > r)
            break;
 
        dp.push_back(x);
        i++;
    }
 
    // Count of Primonacci Numbers
    cout << s.size();
}
 
// Driver Code
int main()
{
    sieve();
 
    int L = 1, R = 10;
 
    countPrimonacci(L, R);
}
 
// This code is contributed by bgangwar59


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG {
 
    // Stores list of all primes
    static ArrayList primes;
    static int M = 100005;
 
    // Function to find all primes
    static void sieve()
    {
        primes = new ArrayList<>();
 
        // To mark the prime ones
        boolean mark[] = new boolean[M];
 
        // Initially all indices as prime
        for (int i = 2; i < M; i++)
            mark[i] = true;
 
        for (int i = 2; i * i < M; i++) {
 
            // If i is prime
            if (mark[i]) {
 
                // Set all multiples
                // of i as non-prime
                for (int j = i * i;
                     j < M; j += i)
 
                    mark[j] = false;
            }
        }
 
        // Adding all primes to a list
        for (int i = 2; i < M; i++)
            if (mark[i])
                primes.add(i);
    }
 
    // Function to return the count of
    // Primonacci Numbers in the range [l, r]
    static void countPrimonacci(int l, int r)
    {
 
        // dp[i] contains ith Primonacci Number
        ArrayList dp = new ArrayList<>();
        dp.add(1);
        dp.add(1);
        int i = 2;
 
        // Stores the Primonacci Numbers
        HashSet s = new HashSet<>();
        while (true) {
 
            int x = 0;
 
            // Iterate over all smaller primes
            for (int j = 0; j < primes.size();
                 j++) {
 
                int p = primes.get(j);
 
                if (p >= i)
                    break;
 
                x += dp.get(i - p);
            }
 
            // If Primonacci number lies
            // within the range [L, R]
            if (x >= l && x <= r)
                s.add(x);
 
            if (x > r)
                break;
 
            dp.add(x);
            i++;
        }
 
        // Count of Primonacci Numbers
        System.out.println(s.size());
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        sieve();
 
        int L = 1, R = 10;
 
        countPrimonacci(L, R);
    }
}


Python3
# Python3 implementation of
# the above approach
M = 100005
 
# Stores list of all primes
primes = []
 
# Function to find all primes
def sieve():
 
    # To mark the prime ones
    mark = [False] * M
 
    # Initially all indices as prime
    for i in range(2, M):
        mark[i] = True
 
    i = 2
    while i * i < M:
 
        # If i is prime
        if(mark[i]):
 
            # Set all multiples
            # of i as non-prime
            j = i * i
            while j < M:
                mark[j] = False
                j += i
                 
        i += 1
 
    # Adding all primes to a list
    for i in range(2, M):
        if(mark[i]):
            primes.append(i)
 
# Function to return the count of
# Primonacci Numbers in the range [l, r]
def countPrimonacci(l, r):
 
    # dp[i] contains ith Primonacci Number
    dp = []
    dp.append(1)
    dp.append(1)
    i = 2
 
    # Stores the Primonacci Numbers
    s = set()
 
    while(True):
        x = 0
 
        # Iterate over all smaller primes
        for j in range(len(primes)):
            p = primes[j]
 
            if(p >= i):
                break
 
            x += dp[i - p]
 
        # If Primonacci number lies
        # within the range [L, R]
        if(x >= l and x <= r):
            s.add(x)
 
        if(x > r):
            break
 
        dp.append(x)
        i += 1
 
    # Count of Primonacci Numbers
    print(len(s))
 
# Driver Code
if __name__ == '__main__':
 
    sieve()
    L, R = 1, 10
 
    countPrimonacci(L, R)
 
# This code is contributed by Shivam Singh


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
    // Stores list of all primes
    static List primes;
    static int M = 100005;
 
    // Function to find all primes
    static void sieve()
    {
        primes = new List();
 
        // To mark the prime ones
        bool[] mark = new bool[M];
 
        // Initially all indices as prime
        for (int i = 2; i < M; i++)
            mark[i] = true;
 
        for (int i = 2; i * i < M; i++)
        {
 
            // If i is prime
            if (mark[i])
            {
 
                // Set all multiples
                // of i as non-prime
                for (int j = i * i; j < M; j += i)
                    mark[j] = false;
            }
        }
 
        // Adding all primes to a list
        for (int i = 2; i < M; i++)
            if (mark[i])
                primes.Add(i);
    }
 
    // Function to return the count of
    // Primonacci Numbers in the range [l, r]
    static void countPrimonacci(int l, int r)
    {
 
        // dp[i] contains ith Primonacci Number
        List dp = new List();
        dp.Add(1);
        dp.Add(1);
        int i = 2;
 
        // Stores the Primonacci Numbers
        HashSet s = new HashSet();
        while (true)
        {
            int x = 0;
 
            // Iterate over all smaller primes
            for (int j = 0; j < primes.Count; j++)
            {
                int p = primes[j];
                if (p >= i)
                    break;
                x += dp[i - p];
            }
 
            // If Primonacci number lies
            // within the range [L, R]
            if (x >= l && x <= r)
                s.Add(x);
 
            if (x > r)
                break;
            dp.Add(x);
            i++;
        }
 
        // Count of Primonacci Numbers
        Console.WriteLine(s.Count);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        sieve();
        int L = 1, R = 10;
        countPrimonacci(L, R);
    }
}
 
// This code is contributed by shikhasingrajput


输出:
5






时间复杂度: O(N * P),其中P是直到R的素数的数量
辅助空间: O(N)