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

Published on 12 May 2024 09:40 PM

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

Source code in the mad programming language

            NORMAL MODE IS INTEGER
            
            INTERNAL FUNCTION LOWBIT.(K) = K-K/2*2
            
          R FUNCTION TO CALC POP COUNT  
            INTERNAL FUNCTION(N)
            ENTRY TO POPCNT.
            RSLT = 0
            PCTNUM = N
LOOP        PCTNXT = PCTNUM/2
            RSLT = RSLT + PCTNUM-PCTNXT*2
            PCTNUM = PCTNXT
            WHENEVER PCTNUM.NE.0, TRANSFER TO LOOP
            FUNCTION RETURN RSLT
            END OF FUNCTION
            
          R POP COUNT OF 3^0 TO 3^29
            POW3 = 1
            THROUGH P3CNT, FOR I=0, 1, I.GE.30
            PRINT FORMAT P3FMT, I, POPCNT.(POW3)
P3CNT       POW3 = POW3 * 3
            VECTOR VALUES P3FMT = $15HPOP COUNT OF 3^,I2,2H: ,I3*$
          
          R EVIL AND ODIOUS NUMBERS   
            PRINT COMMENT$ $
            PRINT COMMENT$ FIRST 30 EVIL NUMBERS ARE$
            SEEN = 1
            THROUGH EVIL, FOR I=0, 1, SEEN.GE.30
            WHENEVER LOWBIT.(POPCNT.(I)).E.0
                PRINT FORMAT NUMFMT,I
                SEEN = SEEN + 1
EVIL        END OF CONDITIONAL

            PRINT COMMENT$ $
            PRINT COMMENT$ FIRST 30 ODIOUS NUMBERS ARE$
            SEEN = 1
            THROUGH ODIOUS, FOR I=0, 1, SEEN.GE.30
            WHENEVER LOWBIT.(POPCNT.(I)).E.1
                PRINT FORMAT NUMFMT,I
                SEEN = SEEN + 1
ODIOUS      END OF CONDITIONAL
           
            VECTOR VALUES NUMFMT = $I2*$
            END OF PROGRAM

  

You may also check:How to resolve the algorithm Middle three digits step by step in the D programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Delphi programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Raku programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Java programming language
You may also check:How to resolve the algorithm Range extraction step by step in the MiniScript programming language