Encuentre dos factores propios de N tales que su suma sea coprima con N

Dado un entero N , debe encontrar dos factores propios de N tales que su suma sea coprima con el entero N dado . Si no existen tales factores, imprima -1.

Ejemplos:

Entrada: N = 15
Salida: 3, 5
Explicación: 3 y 5 son los factores propios de 15 y 3+5 -> 8 es coprimo con 15.

Entrada: N = 4
Salida: -1
Explicación: no hay factores propios que satisfagan las condiciones requeridas

Enfoque ingenuo: genere una lista de todos los factores propios de N y, para cada par posible, compruebe si su suma es coprima con N, es decir , MCD (suma de un par de enteros, N) = 1. Aquí, MCD significa el máximo común divisor.

Enfoque eficiente:  si dos números A y B son coprimos, entonces su suma es coprima con su producto. Teniendo eso en cuenta, encuentre todos los factores de N y para cada factor d1 , calcule el factor más grande de N, d2 que es coprimo con d1 . Para calcular d2, simplemente divida N con d1 hasta que N%d1 != 0 . Finalmente, compruebe si d1 y d2 son factores propios de N o no (es decir, d1>1 y d2>1).

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

C++

// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N
void printFactors(int n)
{
 
    // Find factors in sorted order
    for (int i = 2; i <= sqrt(n); i++) {
 
        if (n % i == 0) {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0) {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1) {
                // Print answer
                cout << d1 << ", " << d2;
                return;
            }
        }
    }
 
    // No such factors exist
    cout << -1;
}
 
// Driver code
int main()
{
    int N = 10;
 
    // Function Call
    printFactors(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N   
static void printFactors(int n)
{
     
    // Find factors in sorted order
    for(int i = 2; i <= (int)Math.sqrt(n); i++)
    {
        if (n % i == 0)
        {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0)
            {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1)
            {
                 
                // Print answer
                System.out.print(d1 + ", " + d2);
                return;
            }
        }
    }
 
    // No such factors exist
    System.out.print(-1);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10;
     
    // Function Call
    printFactors(N);
}
}
 
// This code is contributed by Potta Lokesh

Python3

# Python Program for the above approach
import math
 
# Function to find two proper
# factors of N such that their
# sum is coprime with N
def printFactors(n):
    # Find factors in sorted order
    for i in range(2, int(math.sqrt(n))+1):
        if (n % i == 0):
            d1 = i
            d2 = n
             
            # Find largest value of d2 such
            # that d1 and d2 are co-prime
            while (d2 % d1 == 0):
                d2 = d2 // d1
             
            # Check if d1 and d2 are proper
            # factors of N
            if (d1 > 1 and d2 > 1):
                 
                # Print answer
                print(d1, d2, sep=", ")
                return
    # No such factors exist
    print(-1)
# Driver code
N = 10
 
# Function Call
printFactors(N)
     
# This code is contributed by Shivani

C#

// C# Program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N
static void printFactors(int n)
{
 
    // Find factors in sorted order
    for (int i = 2; i <= (int)Math.Sqrt(n); i++) {
 
        if (n % i == 0) {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0) {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1)
            {
               
                // Print answer
                Console.Write(d1 + ", "+d2);
                return;
            }
        }
    }
 
    // No such factors exist
    Console.Write(-1);
}
 
// Driver code
public static void Main()
{
    int N = 10;
   
    // Function Call
    printFactors(N);
}
}
 
// This code is contributed by ipg2016107.

Javascript

<script>
// Javascript Program for the above approach
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N
function printFactors(n) {
  // Find factors in sorted order
  for (let i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
      let d1 = i,
        d2 = n;
 
      // Find largest value of d2 such
      // that d1 and d2 are co-prime
      while (d2 % d1 == 0) {
        d2 = Math.floor(d2 / d1);
      }
 
      // Check if d1 and d2 are proper
      // factors of N
      if (d1 > 1 && d2 > 1) {
        // Print answer
        document.write(d1 + ", " + d2);
        return;
      }
    }
  }
 
  // No such factors exist
  document.write(-1);
}
 
// Driver code
 
let N = 10;
 
// Function Call
printFactors(N);
 
// This code is contributed by _saurabh_jaiswal.
</script>
Producción: 

2, 5

 

Complejidad de Tiempo: O(√N)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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