How to resolve the algorithm Population count step by step in the Oforth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Population count step by step in the Oforth 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 Oforth programming language
Source code in the oforth programming language
: popcount(n)
0 while ( n ) [ n isOdd + n bitRight(1) ->n ] ;
: test
| i count |
30 seq map(#[ 3 swap 1- pow ]) map(#popcount) println
0 ->count
0 while( count 30 <> ) [ dup popcount isEven ifTrue: [ dup . count 1+ ->count ] 1+ ] drop printcr
0 ->count
0 while( count 30 <> ) [ dup popcount isOdd ifTrue: [ dup . count 1+ ->count ] 1+ ] drop ;
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the Tcl programming language
You may also check:How to resolve the algorithm Animation step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the 8th programming language
You may also check:How to resolve the algorithm Stable marriage problem step by step in the Groovy programming language
You may also check:How to resolve the algorithm Unprimeable numbers step by step in the FreeBASIC programming language