How to resolve the algorithm Population count step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Population count step by step in the Java programming language

Table of Contents

Problem Statement

The   population count   is the number of   1s   (ones)   in the binary representation of a non-negative integer. Population count   is also known as:

For example,   5   (which is   101   in binary)   has a population count of   2.

Evil numbers   are non-negative integers that have an   even   population count. Odious numbers     are  positive integers that have an    odd   population count.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Population count step by step in the Java programming language

This Java program explores the concept of population count, which refers to the number of set bits (1s) in a binary representation of an integer. It calculates and displays population counts for different types of integer representations: 32-bit integer (int), 64-bit integer (long), and BigInteger. Additionally, it identifies and lists "evil" and "odious" integers based on the parity of their population count.

Program Structure:

  • Main Method:

    • The program's entry point is the main method.
    • It performs three sets of operations:
      • Calculating and printing population counts for 32-bit integers, 64-bit integers, and BigInteger.
      • Identifying and printing "evil" integers (even population count).
      • Identifying and printing "odious" integers (odd population count).
  • Population Count Calculations:

    • The Integer.bitCount(), Long.bitCount(), and BigInteger.bitCount() methods are used to calculate population counts for different integer types.
    • The program iterates through a range of integer values and prints their population counts.
  • Identification of Evil and Odious Integers:

    • An integer is considered "evil" if its population count is even.
    • An integer is considered "odious" if its population count is odd.
    • The program maintains two arrays to store evil and odious integers.
    • It iterates through a range of integer values and adds each evil and odious integer to the corresponding array.

Output:

  • The program prints the population counts for 32-bit integers, 64-bit integers, and BigInteger.
  • It lists both evil and odious integers up to a predetermined limit.

Explanation of Terminology:

  • Population Count: The number of set bits (1s) in a binary representation of an integer.
  • Evil Integer: An integer with an even population count.
  • Odious Integer: An integer with an odd population count.

Additional Notes:

  • The program generates and prints a sequence of evil and odious integers. The exact values in these sequences may vary depending on the implementation and the range of integers considered.
  • The program only considers positive integers in its operations.

Source code in the java programming language

import java.math.BigInteger;

public class PopCount {
    public static void main(String[] args) {
	{ // with int
	    System.out.print("32-bit integer: ");
	    int n = 1;
	    for (int i = 0; i < 20; i++) {
		System.out.printf("%d ", Integer.bitCount(n));
		n *= 3;
	    }
	    System.out.println();
	}
	{ // with long
	    System.out.print("64-bit integer: ");
	    long n = 1;
	    for (int i = 0; i < 30; i++) {
		System.out.printf("%d ", Long.bitCount(n));
		n *= 3;
	    }
	    System.out.println();
	}
	{ // with BigInteger
	    System.out.print("big integer   : ");
	    BigInteger n = BigInteger.ONE;
	    BigInteger three = BigInteger.valueOf(3);
	    for (int i = 0; i < 30; i++) {
		System.out.printf("%d ", n.bitCount());
		n = n.multiply(three);
	    }
	    System.out.println();
	}

	int[] od = new int[30];
	int ne = 0, no = 0;
	System.out.print("evil   : ");
	for (int n = 0; ne+no < 60; n++) {
	    if ((Integer.bitCount(n) & 1) == 0) {
		if (ne < 30) {
		    System.out.printf("%d ", n);
		    ne++;
		}
	    } else {
		if (no < 30) {
		    od[no++] = n;
		}
	    }
	}
	System.out.println();
	System.out.print("odious : ");
	for (int n : od) {
	    System.out.printf("%d ", n);
	}
	System.out.println();
    }
}


  

You may also check:How to resolve the algorithm Department numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Permutations/Rank of a permutation step by step in the Julia programming language
You may also check:How to resolve the algorithm A+B step by step in the Erlang programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Ring programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the OCaml programming language