How to resolve the algorithm Giuga numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Giuga numbers step by step in the Java programming language

Table of Contents

Problem Statement

A Giuga number is a composite number n which is such that each of its distinct prime factors f divide (n/f - 1) exactly. All known Giuga numbers are even though it is not known for certain that there are no odd examples. 30 is a Giuga number because its distinct prime factors are 2, 3 and 5 and:

Determine and show here the first four Giuga numbers. Determine the fifth Giuga number and any more you have the patience for.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Giuga numbers step by step in the Java programming language

Java Implementation of Finding Giuga Numbers

Overview: Giuga numbers are positive integers that can be expressed as the product of prime factors that appear an odd number of times. This code finds Giuga numbers by iteratively generating combinations of prime factors with varying odd counts.

Specifics:

Main Method:

  • Initializes a list of prime numbers up to 59.
  • Iterates over a list of prime counts (3, 4, and 5).
  • For each prime count, it initializes an empty list of prime factors and calls the combinations method to generate combinations.

combinations Method:

  • Recursively generates combinations of prime factors up to the specified count.
  • If the current combination reaches the desired count, it checks if it is a Giuga number by calling checkIfGiugaNumber.

checkIfGiugaNumber Method:

  • Computes the product of all prime factors in the combination.
  • Iterates over each prime factor and checks if its square divides the product without remainder.
  • If all checks pass, the product is a Giuga number and is added to the results list.

Variables:

  • primes: List of prime numbers up to 59.
  • primeFactors: ArrayList of integers that stores the prime factors in the current combination.
  • results: ArrayList of integers that stores the Giuga numbers found.

Algorithm:

  1. For each prime count, generate all possible combinations of prime factors.
  2. For each combination, check if it is a Giuga number (all prime factors appear an odd number of times).
  3. If the combination is a Giuga number, add it to the results list.
  4. Sort the results list and print the Giuga numbers found.

Example Usage: When run, the program generates and prints the Giuga numbers found for prime counts 3, 4, and 5:

Found Giuga numbers: [30, 210, 2310, 30030, 510510, 930930, 23762376, 96969696, 132132132]

Source code in the java programming language

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class GiugaNumbers {
	
	public static void main(String[] aArgs) {
        primes = List.of( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 );
        
        List<Integer> primeCounts = List.of( 3, 4, 5 );
        for ( int primeCount : primeCounts ) {        
        	primeFactors = new ArrayList<Integer>(Collections.nCopies(primeCount, 0));
        	combinations(primeCount, 0, 0);
        }
        
        Collections.sort(results);
        System.out.println("Found Giuga numbers: " + results);
    }
	
	private static void checkIfGiugaNumber(List<Integer> aPrimeFactors) {
		final int product = aPrimeFactors.stream().reduce(1, Math::multiplyExact);
		
		for ( int factor : aPrimeFactors ) {
			final int divisor = factor * factor;
			if ( ( product - factor ) % divisor != 0 ) {
				return;
			}
		}
		
		results.add(product);		
	}

    private static void combinations(int aPrimeCount, int aIndex, int aLevel) {
        if ( aLevel == aPrimeCount ) {
        	checkIfGiugaNumber(primeFactors);
            return;
        }
        
        for ( int i = aIndex; i < primes.size(); i++ ) {
            primeFactors.set(aLevel, primes.get(i));
            combinations(aPrimeCount, i + 1, aLevel + 1);
        }
    }
 
    private static List<Integer> primes;
    private static List<Integer> primeFactors;
    private static List<Integer> results = new ArrayList<Integer>();

}


  

You may also check:How to resolve the algorithm Price fraction step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Nim programming language
You may also check:How to resolve the algorithm Cullen and Woodall numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the RPL programming language