Number of 1's in binary expansion of n: Difference between revisions

From Rosetta Code
Content added Content deleted
(Blanked the page)
Line 1: Line 1:
{{Draft task}}

;Task:
Show the number of 1's in binary expansion of n, where '''0 <= n < 50'''
<br><br>

=={{header|Ring}}==
<lang ring>
load "stdlib.ring"
decimals(0)
see "working..." + nl
see "Number of 1's in binary expansion of n are:" + nl

decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]

row = 0
limit = 49
for n = 0 to limit
num = 0
nbin = decimaltobase(n,2)
for m = 1 to len(nbin)
if nbin[m] = "1"
num = num + 1
ok
next
row = row + 1
see "" + num + " "
if row%10 = 0
see nl
ok
next

see "done..." + nl

func decimaltobase(nr,base)
binList = []
binary = 0
remainder = 1
while(nr != 0)
remainder = nr % base
ind = find(decList,remainder)
rem = baseList[ind]
add(binList,rem)
nr = floor(nr/base)
end
binlist = reverse(binList)
binList = list2str(binList)
binList = substr(binList,nl,"")
return binList
</lang>
{{out}}
<pre>
working...
Number of 1's in binary expansion of n are:
0 1 1 2 1 2 2 3 1 2
2 3 2 3 3 4 1 2 2 3
2 3 3 4 2 3 3 4 3 4
4 5 1 2 2 3 2 3 3 4
2 3 3 4 3 4 4 5 2 3
done...
</pre>

Revision as of 06:14, 11 April 2021