How to resolve the algorithm Population count step by step in the Ring programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Population count step by step in the Ring 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 Ring programming language
Source code in the ring programming language
# Project : Population count
odds = []
evens = []
pows = []
for n = 0 to 59
if n < 30 add(pows, onesCount(pow(3, n))) ok
num = onesCount(n)
if num & 1 = 0 add(evens, n) else add(odds, n) ok
next
showOne("3^x:", pows)
showOne("Evil numbers:", evens)
showOne("Odious numbers:", odds)
func onesCount(b)
c = 0 m = 50
while b > 0
p = pow(2, m)
if b >= p b -= p c++ ok
m--
end return c
func arrayToStr(ary)
res = "[" s = ", "
for n = 1 to len(ary)
if ary[n] < 10 res += " " ok
if n = len(ary) s = "]" ok
res += "" + ary[n] + s
next return res
func showOne(title, ary)
? title
? arrayToStr(ary) + nl
You may also check:How to resolve the algorithm Long year step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the zkl programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Oz programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the QB64 programming language
You may also check:How to resolve the algorithm Assertions step by step in the Delphi programming language