Dado un número, comprueba si está libre de cuadrados o no. Se dice que un número es libre de cuadrados si ningún factor primo lo divide más de una vez, es decir, la mayor potencia de un factor primo que divide a n es uno. Los primeros números cuadrados libres son 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, …
Ejemplos:
Input : n = 10 Output : Yes 10 can be factorized as 2*5. Since no prime factor appears more than once, it is a square free number. Input : n = 20 Output : No 20 can be factorized as 2 * 2 * 5. Since prime factor appears more than once, it is not a square free number.
La idea es simple, uno por uno encontramos todos los factores primos . Para cada factor primo, comprobamos si su cuadrado también divide a n. Si es así, devolvemos falso. Finalmente, si no encontramos un factor primo que sea divisible más de una vez, devolvemos falso.
C++
// C++ Program to print // all prime factors # include <bits/stdc++.h> using namespace std; // Returns true if n is a square free // number, else returns false. bool isSquareFree(int n) { if (n % 2 == 0) n = n/2; // If 2 again divides n, then n is // not a square free number. if (n % 2 == 0) return false; // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i+2) { // Check if i is a prime factor if (n % i == 0) { n = n/i; // If i again divides, then // n is not square free if (n % i == 0) return false; } } return true; } /* Driver program to test above function */ int main() { int n = 10; if (isSquareFree(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java Program to print // all prime factors class GFG { // Returns true if n is a square free // number, else returns false. static boolean isSquareFree(int n) { if (n % 2 == 0) n = n / 2; // If 2 again divides n, then n is // not a square free number. if (n % 2 == 0) return false; // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i = i + 2) { // Check if i is a prime factor if (n % i == 0) { n = n / i; // If i again divides, then // n is not square free if (n % i == 0) return false; } } return true; } /* Driver program to test above function */ public static void main(String[] args) { int n = 10; if (isSquareFree(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by prerna saini.
Python3
# Python3 Program to print # all prime factors from math import sqrt # Returns true if n is # a square free number, # else returns false. def isSquareFree(n): if n % 2 == 0: n = n / 2 # If 2 again divides n, # then n is not a square # free number. if n % 2 == 0: return False # n must be odd at this # point. So we can skip # one element # (Note i = i + 2) for i in range(3, int(sqrt(n) + 1)): # Check if i is a prime # factor if n % i == 0: n = n / i # If i again divides, then # n is not square free if n % i == 0: return False return True # Driver program n = 10 if isSquareFree(n): print ("Yes") else: print ("No") # This code is contributed by Shreyanshi Arun.
C#
// C# Program to print // all prime factors using System; class GFG { // Returns true if n is a square free // number, else returns false. static bool isSquareFree(int n) { if (n % 2 == 0) n = n / 2; // If 2 again divides n, then n is // not a square free number. if (n % 2 == 0) return false; // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.Sqrt(n); i = i + 2) { // Check if i is a prime factor if (n % i == 0) { n = n / i; // If i again divides, then // n is not square free if (n % i == 0) return false; } } return true; } // Driver program public static void Main() { int n = 10; if (isSquareFree(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to print // all prime factors // Returns true if n is a square free // number, else returns false. function isSquareFree($n) { if ($n % 2 == 0) $n = $n / 2; // If 2 again divides n, then n is // not a square free number. if ($n % 2 == 0) return false; // n must be odd at this // point. So we can skip // one element (Note i = i +2) for ($i = 3; $i <= sqrt($n); $i = $i + 2) { // Check if i is a prime factor if ($n % $i == 0) { $n = $n / $i; // If i again divides, then // n is not square free if ($n % $i == 0) return false; } } return true; } // Driver Code $n = 10; if (isSquareFree($n)) echo("Yes"); else echo("No"); // This code is contributed by Ajit. ?>
Javascript
<script> // JavaScript Program to print // all prime factors // Returns true if n is a square free // number, else returns false. function isSquareFree(n) { if (n % 2 == 0) n = n / 2; // If 2 again divides n, then n is // not a square free number. if (n % 2 == 0) return false; // n must be odd at this point. So we can // skip one element (Note i = i +2) for (let i = 3; i <= Math.sqrt(n); i = i + 2) { // Check if i is a prime factor if (n % i == 0) { n = n / i; // If i again divides, then // n is not square free if (n % i == 0) return false; } } return true; } // Driver code let n = 10; if (isSquareFree(n)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad de tiempo: O(sqrt(N))
En el peor de los casos, cuando el número es un cuadrado perfecto, habrá sqrt(n)/2 iteraciones.
Publicación traducida automáticamente
Artículo escrito por Shashank_Pathak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA