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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Population count step by step in the Scala 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 Scala programming language

Source code in the scala programming language

import java.lang.Long.bitCount

object PopCount extends App {
  val nNumber = 30

  def powersThree(start: Long): LazyList[Long] = start #:: powersThree(start * 3L)

  println("Population count of 3ⁿ :")
  println(powersThree(1L).map(bitCount).take(nNumber).mkString(", "))

  def series(start: Long): LazyList[Long] = start #:: series(start + 1L)

  println("Evil numbers:")
  println(series(0L).filter(bitCount(_) % 2 == 0).take(nNumber).mkString(", "))

  println("Odious numbers:")
  println(series(0L).filter(bitCount(_) % 2 != 0).take(nNumber).mkString(", "))

}


  

You may also check:How to resolve the algorithm Queue/Usage step by step in the Diego programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Call a function step by step in the SmallBASIC programming language
You may also check:How to resolve the algorithm Combinations step by step in the Erlang programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Quackery programming language