Entero más pequeño que tiene al menos K divisores primos con diferencia entre cada factor al menos D

Dados dos enteros D y K . La tarea es encontrar el número N más pequeño que tenga al menos K divisores primos y la diferencia entre cada par de divisores sea al menos D

Ejemplos

Entrada: D = 3, K = 2
Salida: 55
Explicación: Es el número más pequeño que tiene 4 divisores 1 y 2 divisores primos 5, 11 y su diferencia entre cualquiera del par es D.

Entrada: D = 1, K = 4
Salida: 210
Explicación: Es el número más pequeño que tiene 5 divisores 1 y 4 divisores primos 2, 3, 5, 7, y su diferencia entre cualquiera del par es D.

 

Enfoque: este problema se puede resolver usando la criba de Eratóstenes Siga los pasos a continuación para resolver el problema dado.

  • Haz un colador de Eratóstenes.
  • Inicializa una variable firstDivisor y almacena D + 1 .
  • Iterar firstDivisor por 1 hasta que se convierta en primo.
  • Inicialice SmallestNumber = FirstDivisor + D .
  • Ahora itere el ciclo e incremente SmallestNumber hasta que obtengamos K -1 primos.
  • Y, el producto con todos los divisores y devolver el producto.

A continuación se muestra la implementación del enfoque anterior.

C++

// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
 
// Function of Sieve of Eratosthenes
void SieveOfEratosthenes(vector<bool>& prime)
{
    for (int p = 2; p * p <= N; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= N; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find smallest
// number with given conditions
int SmallestNumber(vector<bool> prime,
                   int D, int K)
{
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           and !prime[FirstDivisor]) {
        ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 and SmallestNumber < N) {
        if (prime[Divisor]
            and Divisor - D >= prevDivisor) {
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K--;
        }
        Divisor++;
    }
 
    // Return the final answer
    return SmallestNumber;
}
 
// Driver Code
int main()
{
    vector<bool> prime(N, true);
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    cout << SmallestNumber(prime, D, K);
 
    return 0;
}

Java

// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
static int N = 1000000;
 
// Function of Sieve of Eratosthenes
static void SieveOfEratosthenes(boolean[] prime)
{
    for (int p = 2; p * p < N; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i < N; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find smallest
// number with given conditions
static int SmallestNumber(boolean[] prime,
                   int D, int K)
{
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           && !prime[FirstDivisor]) {
        ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 && SmallestNumber < N) {
        if (prime[Divisor]
            && Divisor - D >= prevDivisor) {
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K--;
        }
        Divisor++;
    }
 
    // Return the final answer
    return SmallestNumber;
}
 
// Driver Code
public static void main(String args[])
{
    boolean[] prime = new boolean[N];
    Arrays.fill(prime, true);
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    System.out.println(SmallestNumber(prime, D, K));
}
}
 
// This code is contributed by sanjoy_62.

Python3

# Python program to implement
# the above approach
N = 1000000;
 
# Function of Sieve of Eratosthenes
def SieveOfEratosthenes(prime):
    for p in range(2, N//2):
        if (prime[p] == True):
            for i in range(p*p,N,p):
                prime[i] = False;
         
# Function to find smallest
# number with given conditions
def SmallestNumber(prime, D, K):
 
    # Initialize first with D + 1
    # because 1 is also a divisor
    FirstDivisor = D + 1;
 
    while (FirstDivisor < N and prime[FirstDivisor]!=True):
        FirstDivisor += 1;
     
    # Now value of K is decrement by 1
    K -= 1;
 
    # Initialize Divisor with First + D
    # to maintain a difference D
    # We get Remaining divisor
    SmallestNumber = FirstDivisor;
    Divisor = FirstDivisor + D;
 
    # Maintain previous divisor
    # to maintain difference
    prevDivisor = FirstDivisor;
    while (K > 0 and SmallestNumber < N):
        if (prime[Divisor] and Divisor - D >= prevDivisor):
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K -= 1;
         
        Divisor += 1;
 
    # Return the final answer
    return SmallestNumber;
 
# Driver Code
if __name__ == '__main__':
    prime = [True for i in range(N)];
     
    SieveOfEratosthenes(prime);
 
    D = 1;
    K = 4;
 
    # Function Call
    print(SmallestNumber(prime, D, K));
 
# This code is contributed by Rajput-Ji

C#

// C# program to implement
// the above approach
using System;
 
public class GFG
{
 
  static int N = 1000000;
 
  // Function of Sieve of Eratosthenes
  static void SieveOfEratosthenes(bool[] prime)
  {
    for (int p = 2; p * p < N; p++) {
      if (prime[p] == true) {
        for (int i = p * p; i < N; i += p)
          prime[i] = false;
      }
    }
  }
 
  // Function to find smallest
  // number with given conditions
  static int SmallestNumber(bool[] prime,
                            int D, int K)
  {
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           && !prime[FirstDivisor]) {
      ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 && SmallestNumber < N) {
      if (prime[Divisor]
          && Divisor - D >= prevDivisor) {
        SmallestNumber *= Divisor;
        prevDivisor = Divisor;
        K--;
      }
      Divisor++;
    }
 
    // Return the readonly answer
    return SmallestNumber;
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    bool[] prime = new bool[N];
    for(int i = 0;i<N;i++)
      prime[i] = true;
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    Console.WriteLine(SmallestNumber(prime, D, K));
  }
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
        // JavaScript code for the above approach
 
        let N = 1000000;
 
        // Function of Sieve of Eratosthenes
        function SieveOfEratosthenes(prime) {
            for (let p = 2; p * p <= N; p++) {
                if (prime[p] == true) {
                    for (let i = p * p; i <= N; i += p)
                        prime[i] = false;
                }
            }
        }
 
        // Function to find smallest
        // number with given conditions
        function SmallestNumber(prime,
            D, K) {
 
            // Initialize first with D + 1
            // because 1 is also a divisor
            let FirstDivisor = D + 1;
 
            while (FirstDivisor < N
                && !prime[FirstDivisor]) {
                ++FirstDivisor;
            }
 
            // Now value of K is decrement by 1
            K--;
 
            // Initialize Divisor with First + D
            // to maintain a difference D
            // We get Remaining divisor
            let SmallestNumber = FirstDivisor;
            let Divisor = FirstDivisor + D;
 
            // Maintain previous divisor
            // to maintain difference
            let prevDivisor = FirstDivisor;
            while (K > 0 && SmallestNumber < N) {
                if (prime[Divisor]
                    && Divisor - D >= prevDivisor) {
                    SmallestNumber *= Divisor;
                    prevDivisor = Divisor;
                    K--;
                }
                Divisor++;
            }
 
            // Return the final answer
            return SmallestNumber;
        }
 
        // Driver Code
        let prime = new Array(N).fill(true);
 
        SieveOfEratosthenes(prime);
 
        let D = 1;
        let K = 4;
 
        // Function Call
        document.write(SmallestNumber(prime, D, K))
 
       // This code is contributed by Potta Lokesh
    </script>
Producción

210

Complejidad temporal: O(N) 
Espacio auxiliar: O(N)

Publicación traducida automáticamente

Artículo escrito por hrithikgarg03188 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 *