Population count: Difference between revisions

→‎{{header|Ring}}: wrong output, 3 is not the first Evil number, 4 & 16 were in the wrong bin, removed stdlib.ring requirement.
(→‎{{header|Ring}}: wrong output, 3 is not the first Evil number, 4 & 16 were in the wrong bin, removed stdlib.ring requirement.)
Line 3,523:
 
=={{header|Ring}}==
<lang ring># Project : Population count
# Project : Population count
 
binoddodds = []
load "stdlib.ring"
nevens = 0[]
nevenpows = 0[]
nodd = 0
binodd = []
bineven = []
binpow = []
while true
n = n + 1
numb = 0
bin = binarydigits(n)
for nr = 1 to len(bin)
if bin[nr] = "1"
numb = numb + 1
ok
next
if numb % 2 = 0
neven = neven + 1
if neven < 31
add(bineven, n)
ok
else
nodd = nodd + 1
if nodd < 31
add(binodd, n)
ok
ok
if neven > 30 and nodd > 30
exit
ok
end
 
for n = 0 to 2959
see "3^x:" + nl
if n < 30 add(pows, onesCount(pow(3, n))) ok
for n = 0 to 29
numbnum = 0onesCount(n)
binif num & 1 = binarydigits0 add(powevens, n) else add(3odds, n)) ok
for nr = 1 to len(bin)
if bin[nr] = "1"
numb = numb + 1
ok
next
add(binpow, numb)
next
showarray(binpow)
see nl
 
showOne("3^x:", pows)
see "Evil numbers :" + nl
showOne("Evil numbers:", evens)
showarray(bineven)
see showOne("Odious numbers:", + nlodds)
see nl
see "Odious numbers:" + nl
showarray(binodd)
see nl
 
func showarrayonesCount(vectb)
c = see0 "["m = 50
while b svect> = ""0
for n = 1 top len= pow(vect2, m)
if b svect >= svectp +b vect[n]-= +p ",c++ "ok
next m--
end return okc
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl
</lang>
Output:
<pre>
3^x:
[1, 2, 2, 4, 3, 6, 6, 5, 6, 8, 9, 13, 10, 11, 14, 15, 11, 14, 14, 17, 17, 20, 19, 22, 16, 18, 24, 30, 25, 25]
 
func arrayToStr(ary)
Evil numbers :
res = n"[" =s n= +", 1"
[3, 4, 5, 6, 9, 10, 12, 15, 16, 17, 18, 20, 23, 24, 27, 29, 30, 33, 34, 36, 39, 40, 43, 45, 46, 48, 51, 53, 54, 57]
for nrn = 1 to len(binary)
if binary[nrn] < 10 res += "1 " ok
neven if n = nevenlen(ary) s = +"]" 1ok
res += "" + numb = numbary[n] + 1s
next return res
 
func showOne(title, ary)
? numb = 0title
? arrayToStr(ary) + nl</lang>
{{out}}
<pre>3^x:
[ 1, 2, 2, 4, 3, 6, 6, 5, 6, 8, 9, 13, 10, 11, 14, 15, 11, 14, 14, 17, 17, 20, 19, 22, 16, 18, 24, 30, 25, 25]
 
see "Evil numbers :" + nl
[3 0, 4 3, 5, 6, 9, 10, 12, 15, 16, 17, 18, 20, 23, 24, 27, 29, 30, 33, 34, 36, 39, 40, 43, 45, 46, 48, 51, 53, 54, 57, 58]
 
Odious numbers:
[ 1, 2, 4, 7, 8, 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, 47, 49, 50, 52, 55, 56, 59, 61, 62]</pre>
</pre>
 
=={{header|Ruby}}==