Square Free Number

07 Apr, 2021
Given a number, check if it is square free or not. A number is said to be square free if no prime factor divides it more than once, i.e., largest power of a prime factor that divides n is one. First few square free numbers are 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, ...Examples:  
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.
 
The idea is simple, we one by one find all prime factors. For every prime factor, we check if its square also divides n. If yes, then we return false. Finally if we do not find a prime factor which is divisible more than once, we return false. 
C++
Java
Python3
C#
PHP
Javascript
// 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 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 Program to print# all prime factorsfrom 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 programn = 10 if isSquareFree(n):    print ("Yes")else:    print ("No")     # This code is contributed by Shreyanshi Arun.
// C# Program to print// all prime factorsusing 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 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.?>
<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
 
Time Complexity : O(sqrt(N)) In the worst case when the number is a perfect square, then there will be sqrt(n)/2 iterations . 
jit_t
imdhruvgupta
code_hunt
divisors
prime-factor
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Find minimum number of coins that make a given value
Program for Decimal to Binary Conversion