Dado un número entero N. La tarea es contar números P menores que N tales que P sea un producto de dos cuadrados perfectos distintos.
Ejemplos :
Input : N = 36 Output : 5 Numbers are 4 = 12 * 22, 9 = 12 * 32, 16 = 12 * 42, 25 = 12 * 52, 36 = 12 * 62
Input : N = 1000000 Output : 999
Enfoque: Consideremos un número P = (a 2 * b 2 ) tal que P <= N. Entonces tenemos (a 2 * b 2 ) <= N. Esto se puede escribir como (a * b) <= sqrt (NORTE).
Entonces tenemos que contar pares (a, b) tales que (a * b) <= sqrt(N) y a <= b.
Tomemos un número Q = (a * b) tal que Q <= sqrt(N).
Tomando a = 1, tenemos b = sqrt(N) – 1 números tales que, ( a * b = Q <= sqrt(N)).
Así podemos tener todos los números sqrt(N) – 1 tales que (a 2 * b 2 ) <= N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to count number less // than N which are product of // any two perfect squares #include <bits/stdc++.h> using namespace std; // Function to count number less // than N which are product of // any two perfect squares int countNumbers(int N) { return int(sqrt(N)) - 1; } // Driver program int main() { int N = 36; cout << countNumbers(N); return 0; }
Java
// Java program to count number less // than N which are product of // any two perfect squares import java.util.*; class solution { // Function to count number less // than N which are product of // any two perfect squares static int countNumbers(int N) { return (int)Math.sqrt(N) - 1; } // Driver program public static void main(String args[]) { int N = 36; System.out.println(countNumbers(N)); } } //This code is contributed by // Surendra_Gangwar
Python 3
# Python 3 program to count number # less than N which are product of # any two perfect squares import math # Function to count number less # than N which are product of # any two perfect squares def countNumbers(N): return int(math.sqrt(N)) - 1 # Driver Code if __name__ == "__main__": N = 36 print(countNumbers(N)) # This code is contributed # by ChitraNayal
C#
// C# program to count number less // than N which are product of // any two perfect squares using System; class GFG { // Function to count number less // than N which are product of // any two perfect squares static int countNumbers(int N) { return (int)(Math.Sqrt(N)) - 1; } // Driver Code public static void Main() { int N = 36; Console.Write(countNumbers(N)); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP program to count number less // than N which are product of // any two perfect squares // Function to count number less // than N which are product of // any two perfect squares function countNumbers($N) { return (int)(sqrt($N)) - 1; } // Driver Code $N = 36; echo countNumbers($N); // This code is contributed by akt_mit ?>
Javascript
<script> // Javascript program to count number less // than N which are product of // any two perfect squares // Function to count number less // than N which are product of // any two perfect squares function countNumbers(N) { return parseInt(Math.sqrt(N), 10) - 1; } let N = 36; document.write(countNumbers(N)); </script>
5
Complejidad de tiempo: O(log(N)), Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Sanjit_Prasad y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA