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

Published on 12 May 2024 09:40 PM

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

Source code in the seed7 programming language

$ include "seed7_05.s7i";
 
const func integer: popcount (in integer: n) is
    return card(bitset(n));
 
const proc: main is func
  local
    var integer: count is 0;
    var integer: num is 0;
  begin
    for num range 0 to 29 do
      write(popcount(3 ** num) <& " ");
    end for;
    writeln;
    write("evil:   ");
    for num range 0 to integer.last until count >= 30 do
      if not odd(popcount(num)) then
        write(num <& " ");
	incr(count);
      end if;
    end for;
    writeln;
    write("odious: ");
    count := 0;
    for num range 0 to integer.last until count >= 30 do
      if odd(popcount(num)) then
        write(num <& " ");
        incr(count);
      end if;
    end for;
    writeln;
  end func;

  

You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Lang programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Delphi programming language
You may also check:How to resolve the algorithm Repeat step by step in the Perl programming language
You may also check:How to resolve the algorithm Create a file step by step in the VBA programming language
You may also check:How to resolve the algorithm Abstract type step by step in the Logtalk programming language