Javatpoint Logo
Javatpoint Logo

Giuga numbers in Java

A Giuga Number is a composite number N that has a unique property. The property states that for every prime factor, p of N, N divided by p minus 1 ((N/p) - 1) should also be divisible by p. If a number N satisfies this condition for all its prime factors, it is considered a Giuga Number.

To check if a given number N is a Giuga Number, you need to verify this property for all its prime factors. If it holds true for every prime factor, the number N is classified as a Giuga Number, and the output is "Yes". If the property does not hold for its prime factors, the number is not a Giuga Number, and the result is "No".

Here are a few more examples to illustrate the concept of Giuga Numbers:

Example 1:

Input: N = 24

Output: No


24 is a composite number with prime divisors {2, 3}.

2 does not divide 24 / 2 - 1 = 11, and

3 does not divide 24 / 3 - 1 = 7.

Example 2:

Input: N = 56

Output: No


56 is a composite number with prime divisors {2, 7}.

2 does not divide 56 / 2 - 1 = 27

7 divides 56 / 7 - 1 = 7.

Example 3:

Input: N = 49

Output: No


49 is a composite number with a prime divisor {7}.

7 does not divide 49 / 7 - 1 = 6

Example 4:

Input: N = 30

Output: Yes


30 is a composite number with prime divisors {2, 3, 5}.

2 divides 30 / 2 - 1 = 14,

3 divides 30 / 3 - 1 = 9,

5 divides 30 / 5 - 1 = 5.

Approach: Brute Force Approach

In this approach, the code iterates through all numbers from 2 to (N-1) to check if each i satisfies the Giuga Number property. It checks whether i is prime, N is divisible by i, and (N/i - 1) is divisible by i for each i. If all conditions hold true for at least one i, the number N is not considered a Giuga Number. If no such i is found, N is regarded as a Giuga Number.


Step 1: Create a method called isPrime(number) that determines whether a given number is prime.

Step 2: When the provided number gets less than or equal to 1, return false since numbers within this range are not considered prime.

Step 3: Iterate from 2 to the square root of the given number:

  1. If the value of the number is divisible by any integer in this range, return false immediately, indicating that it is not a prime number.

Step 4: When no divisors have been identified during the loop, return true, indicating that the integer is a prime number.

Step 5: Create a method isGiugaNumber(n) to check if a given number is a Giuga Number.

Step 6: If the input number is prime, return false, as prime numbers cannot be Giuga Numbers.

Step 7: Iterate through integers from 2 to the input number minus one:

  1. For each integer i, check if it is prime using the isPrime(i) method.
  2. Check if the input number n is divisible by i.
  3. Check if (n / i - 1) is divisible by i.
  4. If all conditions are met, return true as it is a Giuga Number.

Step 8: If no such i is found that breaks the Giuga Number property, return false to indicate that the number does not satisfy the Giuga Number property.




30 is a Giuga number.

Time Complexity: O(n2), where n is the input number.

Space Complexity: O(1)

Next TopicHessian Java

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA