Population count: Difference between revisions

Content added Content deleted
(→‎{{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: Line 3,523:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<lang ring># Project : Population count
# Project : Population count


odds = []
load "stdlib.ring"
n = 0
evens = []
neven = 0
pows = []
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 59
see "3^x:" + nl
if n < 30 add(pows, onesCount(pow(3, n))) ok
for n = 0 to 29
numb = 0
num = onesCount(n)
bin = binarydigits(pow(3,n))
if num & 1 = 0 add(evens, n) else add(odds, n) ok
for nr = 1 to len(bin)
if bin[nr] = "1"
numb = numb + 1
ok
next
add(binpow, numb)
next
next
showarray(binpow)
see nl


showOne("3^x:", pows)
see "Evil numbers :" + nl
showOne("Evil numbers:", evens)
showarray(bineven)
showOne("Odious numbers:", odds)
see nl
see "Odious numbers:" + nl
showarray(binodd)
see nl


func showarray(vect)
func onesCount(b)
see "["
c = 0 m = 50
svect = ""
while b > 0
for n = 1 to len(vect)
p = pow(2, m)
svect = svect + vect[n] + ", "
if b >= p b -= p c++ ok
next
m--
end return c
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 = "[" s = ", "
[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 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</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]

Evil numbers:
[ 0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23, 24, 27, 29, 30, 33, 34, 36, 39, 40, 43, 45, 46, 48, 51, 53, 54, 57, 58]


Odious numbers:
Odious numbers:
[1, 2, 7, 8, 11, 13, 14, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, 47, 49, 50, 52, 55, 56, 59, 61, 62]
[ 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]</pre>
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==