Dado un número entero N , la tarea es encontrar dos números a y b tales que a * b = N y a + b = N. Escriba “NO” si no es posible obtener tales números.
Ejemplos:
Entrada: N = 69
Salida: a = 67,9851
b = 1,01493
Entrada: N = 1
Salida: NO
Enfoque: Si se observa cuidadosamente, se nos da la suma y el producto de raíces de una ecuación cuadrática.
Si N 2 – 4*N < 0 , entonces solo son posibles raíces imaginarias para la ecuación, por lo tanto, «NO» será la respuesta. De lo contrario , a y b serán:
a = ( N + sqrt( N 2 – 4*N ) ) / 2
b = ( N – sqrt( N 2 – 4*N ) ) / 2
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find a and b // such that a*b=N and a+b=N #include <bits/stdc++.h> using namespace std; // Function to return the smallest string void findAandB(double N) { double val = N * N - 4.0 * N; // Not possible if (val < 0) { cout << "NO"; return; } // find a and b double a = (N + sqrt(val)) / 2.0; double b = (N - sqrt(val)) / 2.0; cout << "a = " << a << endl; cout << "b = " << b << endl; } // Driver Code int main() { double N = 69.0; findAandB(N); return 0; }
Java
// Java program to find a and b // such that a*b=N and a+b=N class GFG{ // Function to return the smallest string static void findAandB(double N) { double val = N * N - 4.0 * N; // Not possible if (val < 0) { System.out.println("NO"); return; } // find a and b double a = (N + Math.sqrt(val)) / 2.0; double b = (N - Math.sqrt(val)) / 2.0; System.out.println("a = "+a); System.out.println("b = "+b); } // Driver Code public static void main(String[] args) { double N = 69.0; findAandB(N); } } // This Code is contributed by mits
Python3
# Python 3 program to find a and b # such that a*b=N and a+b=N from math import sqrt # Function to return the # smallest string def findAandB(N): val = N * N - 4.0 * N # Not possible if (val < 0): print("NO") return # find a and b a = (N + sqrt(val)) / 2.0 b = (N - sqrt(val)) / 2.0 print("a =", '{0:.6}' . format(a)) print("b =", '{0:.6}' . format(b)) # Driver Code if __name__ == '__main__': N = 69.0 findAandB(N) # This code is contributed # by SURENDRA_GANGWAR
C#
// C# program to find a and b // such that a*b=N and a+b=N using System; class GFG { // Function to return the smallest string static void findAandB(double N) { double val = N * N - 4.0 * N; // Not possible if (val < 0) { Console.WriteLine("NO"); return; } // find a and b double a = (N + Math.Sqrt(val)) / 2.0; double b = (N - Math.Sqrt(val)) / 2.0; Console.WriteLine("a = "+a); Console.WriteLine("b = "+b); } // Driver Code static void Main() { double N = 69.0; findAandB(N); } // This code is contributed by ANKITRAI1 }
PHP
<?php // PHP program to find a and b // such that a*b=N and a+b=N // Function to return the // smallest string function findAandB($N) { $val = $N * $N - 4.0 * $N; // Not possible if ($val < 0) { echo "NO"; return; } // find a and b $a = ($N + sqrt($val)) / 2.0; $b = ($N - sqrt($val)) / 2.0; echo "a = " , $a, "\n"; echo "b = " , $b, "\n"; } // Driver Code $N = 69.0; findAandB($N); // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to find a and b // such that a*b=N and a+b=N // Function to return the smallest string function findAandB(N) { let val = N * N - 4.0 * N; // Not possible if (val < 0) { document.write("NO"); return; } // find a and b let a = (N + Math.sqrt(val)) / 2.0; let b = (N - Math.sqrt(val)) / 2.0; document.write("a = "+a.toFixed(4) + "</br>"); document.write("b = "+b.toFixed(5)); } let N = 69.0; findAandB(N); </script>
a = 67.9851 b = 1.01493
Complejidad de tiempo: O (logn 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por swetankmodi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA