Recuento de números por debajo de N cuya suma de divisores primos es K

Dados dos enteros K y N , la tarea es encontrar el número de enteros del rango [2, N – 1] cuya suma de divisores primos es K
Ejemplo: 
 

Entrada: N = 20, K = 7 
Salida:
7 y 10 son los únicos números válidos. 
sumPFactors(7) = 7 
sumPFactors(10) = 2 + 5 = 7
Entrada: N = 25, K = 5 
Salida:
 

Enfoque: Cree una array sumPF[] donde sumPF[i] almacene la suma de los divisores primos de i que se pueden calcular fácilmente utilizando el enfoque utilizado en este artículo. Ahora, inicialice una variable conteo = 0 y ejecute un ciclo de 2 a N – 1 y para cada elemento i si sumPF[i] = K luego incremente el conteo .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
#define MAX 1000001
 
// Function to return the count of numbers
// below N whose sum of prime factors is K
int countNum(int N, int K)
{
    // To store the sum of prime factors
    // for all the numbers
    int sumPF[MAX] = { 0 };
 
    for (int i = 2; i < N; i++) {
 
        // If i is prime
        if (sumPF[i] == 0) {
 
            // Add i to all the numbers
            // which are divisible by i
            for (int j = i; j < N; j += i) {
                sumPF[j] += i;
            }
        }
    }
 
    // To store the count of required numbers
    int count = 0;
    for (int i = 2; i < N; i++) {
        if (sumPF[i] == K)
            count++;
    }
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    int N = 20, K = 7;
 
    cout << countNum(N, K);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int MAX = 1000001;
 
// Function to return the count of numbers
// below N whose sum of prime factors is K
static int countNum(int N, int K)
{
    // To store the sum of prime factors
    // for all the numbers
    int []sumPF = new int[MAX];
 
    for (int i = 2; i < N; i++)
    {
 
        // If i is prime
        if (sumPF[i] == 0)
        {
 
            // Add i to all the numbers
            // which are divisible by i
            for (int j = i; j < N; j += i)
            {
                sumPF[j] += i;
            }
        }
    }
 
    // To store the count of required numbers
    int count = 0;
    for (int i = 2; i < N; i++)
    {
        if (sumPF[i] == K)
            count++;
    }
 
    // Return the required count
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 20, K = 7;
 
    System.out.println(countNum(N, K));
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach
MAX = 1000001
 
# Function to return the count of numbers
# below N whose sum of prime factors is K
def countNum(N, K) :
 
    # To store the sum of prime factors
    # for all the numbers
    sumPF = [0] * MAX;
 
    for i in range(2, N) :
         
        # If i is prime
        if (sumPF[i] == 0) :
 
            # Add i to all the numbers
            # which are divisible by i
            for j in range(i, N, i) :
                sumPF[j] += i;
 
    # To store the count of required numbers
    count = 0;
    for i in range(2, N) :
        if (sumPF[i] == K) :
            count += 1;
 
    # Return the required count
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    N = 20; K = 7;
     
    print(countNum(N, K));
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
     
class GFG
{
static int MAX = 1000001;
 
// Function to return the count of numbers
// below N whose sum of prime factors is K
static int countNum(int N, int K)
{
    // To store the sum of prime factors
    // for all the numbers
    int []sumPF = new int[MAX];
 
    for (int i = 2; i < N; i++)
    {
 
        // If i is prime
        if (sumPF[i] == 0)
        {
 
            // Add i to all the numbers
            // which are divisible by i
            for (int j = i; j < N; j += i)
            {
                sumPF[j] += i;
            }
        }
    }
 
    // To store the count of required numbers
    int count = 0;
    for (int i = 2; i < N; i++)
    {
        if (sumPF[i] == K)
            count++;
    }
 
    // Return the required count
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 20, K = 7;
 
    Console.WriteLine(countNum(N, K));
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// Javascript implementation of the approach
 
const MAX = 1000001;
 
// Function to return the count of numbers
// below N whose sum of prime factors is K
function countNum(N, K)
{
    // To store the sum of prime factors
    // for all the numbers
    let sumPF = new Array(MAX).fill(0);
 
    for (let i = 2; i < N; i++) {
 
        // If i is prime
        if (sumPF[i] == 0) {
 
            // Add i to all the numbers
            // which are divisible by i
            for (let j = i; j < N; j += i) {
                sumPF[j] += i;
            }
        }
    }
 
    // To store the count of required numbers
    let count = 0;
    for (let i = 2; i < N; i++) {
        if (sumPF[i] == K)
            count++;
    }
 
    // Return the required count
    return count;
}
 
// Driver code
    let N = 20, K = 7;
 
    document.write(countNum(N, K));
 
</script>
Producción: 

2

 

Complejidad del tiempo: O(N 2 )

Espacio Auxiliar: O(MAX)

Publicación traducida automáticamente

Artículo escrito por Samdare B y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *