How to resolve the algorithm Fusc sequence step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fusc sequence step by step in the Java programming language

Table of Contents

Problem Statement

The   fusc   integer sequence is defined as:

Note that MathWorld's definition starts with unity, not zero.   This task will be using the OEIS' version   (above).

where   A   is some non-negative integer expressed in binary,   and where   B   is the binary value of   A   reversed.

Fusc numbers are also known as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fusc sequence step by step in the Java programming language

This program generates the first 61 Fusc numbers and prints them horizontally. It also locates the first Fusc number whose number of digits exceeds the number of digits of any previous Fusc number and prints that number and its index. Here's how the program works:

  1. fusc Sequence: The Fusc sequence is a sequence of numbers that begins with 0 and 1. Each subsequent number in the sequence is obtained by adding the previous two numbers if the previous number is even, or by adding the previous number and the number after it if the previous number is odd.

  2. Main Method: The main method is the entry point of the program. It calls two methods:

    • showFirst61FuscNumbers: This method prints the first 61 Fusc numbers in a horizontal format.
    • findLongestFuscNumber: This method finds the first Fusc number whose number of digits exceeds the number of digits of any previous Fusc number and prints that number and its index.
  3. Calculation of Fusc Sequence: The Fusc sequence is calculated in a static block. It initializes the first two Fusc numbers to 0 and 1 and then uses a loop to calculate subsequent Fusc numbers based on the following rule:

    • If the previous Fusc number (n-1) is even, the next Fusc number is fusc[n] = fusc[n/2].
    • If n-1 is odd, the next Fusc number is fusc[n] = fusc[(n-1)/2] + fusc[(n+1)/2].
  4. Printing Fusc Sequence:

    • showFirst61FuscNumbers: This method prints the first 61 Fusc numbers in a horizontal format using a simple loop.
    • findLongestFuscNumber: This method starts by setting the start variable to 0. It then iterates over the Fusc numbers, looking for the first number whose number of digits is greater than the number of digits of any previous Fusc number. When it finds such a number, it prints the number and its index and updates the start variable to the index of the next Fusc number. It continues this process until it has found and printed the first 6 such Fusc numbers.
  5. Constants and Variables: The program uses the following constants and variables:

    • FUSC_MAX: This is the maximum number of Fusc numbers to calculate and store in the fusc array.
    • fusc: This is an array that stores the Fusc sequence up to FUSC_MAX.

Overall, this program provides two ways to explore the Fusc sequence: by printing the first 61 numbers and by finding the first number with a number of digits that exceeds any previous Fusc number.

Source code in the java programming language

public class FuscSequence {

    public static void main(String[] args) {
        System.out.println("Show the first 61 fusc numbers (starting at zero) in a horizontal format");
        for ( int n = 0 ; n < 61 ; n++ ) {
            System.out.printf("%,d ", fusc[n]);
        }
        
        System.out.printf("%n%nShow the fusc number (and its index) whose length is greater than any previous fusc number length.%n");
        int start = 0;
        for (int i = 0 ; i <= 5 ; i++ ) {
            int val = i != 0 ? (int) Math.pow(10, i) : -1;
            for ( int j = start ; j < FUSC_MAX ; j++ ) {
                if ( fusc[j] > val ) {
                    System.out.printf("fusc[%,d] = %,d%n", j, fusc[j] );
                    start = j;
                    break;
                }
            }
        }
    }
    
    private static final int FUSC_MAX = 30000000;
    private static int[] fusc = new int[FUSC_MAX];

    static {
        fusc[0] = 0;
        fusc[1] = 1;
        for ( int n = 2 ; n < FUSC_MAX ; n++ ) {
            fusc[n] = (n % 2 == 0 ? fusc[n/2] : fusc[(n-1)/2] + fusc[(n+1)/2]);
        }
    }
}


  

You may also check:How to resolve the algorithm Greatest element of a list step by step in the V programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Scala programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the HicEst programming language