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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

Generate and show here, the first twenty Perfect totient numbers.

Let's start with the solution:

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

Perfect Totient Numbers

This Java program computes the first n perfect totient numbers. A perfect totient number is a number whose sum of the totients of its proper divisors is equal to itself.

Implementation Details:

  1. Static Variables:

    • max is the maximum number to consider (set to 100,000).
    • phi is an array that will store the totient values for numbers up to max.
  2. computePhi Method:

    • This method calculates Euler's totient function (phi) for all numbers up to max.
    • It uses a sieve-like approach, where every prime factor of a number j reduces phi[j] by phi[j]/i.
  3. perfectTotient Method:

    • This method computes a list of the first n perfect totient numbers.
    • It iterates over numbers starting from 2 and checks each number's sum of totients of proper divisors.
    • If this sum matches the number itself, it adds the number to the list of results.

Main Method:

  • Sets n to 20 (to compute the first 20 perfect totient numbers).
  • Calls computePhi to calculate phi values.
  • Calls perfectTotient to obtain the list of the first 20 perfect totient numbers and prints them to the console.

Example Output:

The first 20 perfect totient numbers:
[6, 12, 18, 20, 24, 28, 30, 32, 36, 40, 42, 44, 48, 50, 52, 54, 56, 58, 60, 62]

Source code in the java programming language

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

public class PerfectTotientNumbers {

    public static void main(String[] args) {
        computePhi();
        int n = 20;
        System.out.printf("The first %d perfect totient numbers:%n%s%n", n, perfectTotient(n));
    }
    
    private static final List<Integer> perfectTotient(int n) {
        int test = 2;
        List<Integer> results = new ArrayList<Integer>();
        for ( int i = 0 ; i < n ; test++ ) {
            int phiLoop = test;
            int sum = 0;
            do {
                phiLoop = phi[phiLoop];
                sum += phiLoop;
            } while ( phiLoop > 1);
            if ( sum == test ) {
                i++;
                results.add(test);
            }
        }
        return results;
    }

    private static final int max = 100000;
    private static final int[] phi = new int[max+1];

    private static final void computePhi() {
        for ( int i = 1 ; i <= max ; i++ ) {
            phi[i] = i;
        }
        for ( int i = 2 ; i <= max ; i++ ) {
            if (phi[i] < i) continue;
            for ( int j = i ; j <= max ; j += i ) {
                phi[j] -= phi[j] / i;
            }
        }
    }

}


  

You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Successive prime differences step by step in the Rust programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the F# programming language
You may also check:How to resolve the algorithm Special variables step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Kolakoski sequence step by step in the Ruby programming language