Javatpoint Logo
Javatpoint Logo

Square Free Number in Java

In this section, we will discuss what is square free number and also create Java programs to check if the given number is a square free number or not. The square free number program frequently asked in Java coding interviews and academics.

Square Free Number

A positive integer n is a square free integer if it is product of different primes. In other words, n is square free integer if it is not divisible by p2 (perfect square) for any prime number p other than 1. It can be also defined as numbers that are not divisible by a square greater than 1 is called square free numbers. Its prime factorization has exactly one factor for each prime that appears in it. It is an OEIS sequence A005117.

Square Free Number in Java

Let's understand it through examples.

Square Free Number Example

Example 1: Suppose, we have to check 72 is square free number or not.

Factors (the numbers that divides 72) of 72 are: 1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, and 72

Prime factors are: 2 and 3

In the above factors, perfect square factors are 4, 9, and 36. The factor 8, 12, 24, and 72 are divisible by 4 and 18 is divisible by 9. So, the factor 4, 9, and 36 cannot be square free numbers. In the above factors, there are only three factors left that are not perfect square i.e., 2, 3, and 6 and none of them is divisible by perfect square. Hence, 72 is a square free number.

Let's see another approach to find the same. This approach is a bit easy than the above.

Example 2: Suppose, we have to check 111 is square free number or not.

Factors of 72 are: 1, 3, 37, and 111

Prime factors are: 3, 37

Any factor is not divisible by perfect square and no prime factor appears more than once. Hence, 111 is a square free number.

Example 3: Check 108 is square free number or not.

First, we find the prime factorization of 108: 2 × 2 × 3 × 3 × 3 = 22 × 33

We observe that prime factors appear more than once. Therefore, 108 is not a square free number.

Similarly, we can check for other numbers, also.

Some other 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, 41, 42, 43, 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, 111, 113.

Steps to Check Square Free Number

  1. Read or initialize a number N.
  2. Find the prime factors of N.
  3. Check if prime factor appears more than once or not.
    • If any factor appears more than once, the given number is not a square free number.
    • Else, the given number is square free.

Let's implement the above logic in a Java program.

Square Free Number Java Program

SquareFreeNumberExample1.java

Output 1:

Enter the number: 113
113 is a square free number.

Output 2:

Enter the number: 12
12 is not a square free number.

Find n-th Square Free Number

SquareFreeNumberExample2.java

Output 1:

Enter the number: 10
The 10th square free number is: 14

Output 2:

Enter the number: 20
The 20th square free number is: 31






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA