
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Giuga Number in Java
In this article, we will learn about Giuga Numbers and how to write a code to check if a number is a Giuga Number or not in Java. A Giuga number is a composite number N such that for each prime factor p of N, (N/p)-1 is divisible by p. The first Giuga numbers are 30, 858, 1722, 66198, 2214408306, 24423128562, and 432749205173838.
Example
Lets take N=30. 30 is a composite number, and its prime factors are 2, 3, and 5. ? For p=2: (N/p)-1 to be divisible by p. N is 30, and p here is 2. (30/2)-1=14, which is divisible by 2. ? For p=3: (30/3) - 1 = 9, which is divisible by 3. ? For p=5: (30/5)-1=5, which is divisible by 5. Since the condition is true for all its prime factors, 30 is a Giuga number.
Algorithm
This step-by-step process helps check if a number is a Giuga number in a simple way.
1.Input a number N.
2.If N is prime, it is not a Giuga number, so return false. If N is composite, proceed with the next steps.
3.Find all the prime factors of N.
4.For every prime factor, do these two steps:
- Calculate (N/p) - 1
- Check the result is divisible by p.
5. If all prime factors pass these two steps, then N is a Giuga number, so return True.
Implementation code
The following Java program checks whether a given number is a Giuga number.
public class GiugaChecker { // Method to check if a number is prime public static boolean isPrimeNumber(int number) { if (number <= 1) return false; // Numbers <= 1 are not prime for (int i = 2; i * i <= number; i++) { if (number % i == 0) return false; // If divisible, not prime } return true; // Otherwise, it's prime } // Method to check if a number is a Giuga number public static boolean isGiuga(int n) { if (isPrimeNumber(n)) return false; // Prime numbers can't be Giuga numbers // Check each prime factor of n for (int i = 2; i <= n / 2; i++) { if (isPrimeNumber(i) && n % i == 0) { // If i is a prime factor of n if ((n / i - 1) % i != 0) return false; // Condition fails, not Giuga } } return true; // Passes all checks, it's a Giuga number } // Main method to test the Giuga number checker public static void main(String[] args) { int number = 30; // Change this number to test different inputs // Check and display if the number is a Giuga number if (isGiuga(number)) { System.out.println(number + " is a Giuga number."); } else { System.out.println(number + " is not a Giuga number."); } } }
Output
30 is a Giuga number.
Conclusion
In this article, we learned what Giuga numbers are and how to check if a number is a Giuga number or not with examples. We also learned about how to write Java code to implement the concept.