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 |
|||
⚫ | |||
load "stdlib.ring" |
|||
evens = [] |
|||
pows = [] |
|||
nodd = 0 |
|||
⚫ | |||
bineven = [] |
|||
binpow = [] |
|||
while true |
|||
⚫ | |||
⚫ | |||
bin = binarydigits(n) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
ok |
|||
next |
|||
if numb % 2 = 0 |
|||
⚫ | |||
if neven < 31 |
|||
add(bineven, n) |
|||
ok |
|||
else |
|||
nodd = nodd + 1 |
|||
if nodd < 31 |
|||
add(binodd, n) |
|||
ok |
|||
⚫ | |||
if neven > 30 and nodd > 30 |
|||
exit |
|||
ok |
|||
end |
|||
⚫ | |||
see "3^x:" + nl |
|||
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 |
|||
for nr = 1 to len(bin) |
|||
if bin[nr] = "1" |
|||
numb = numb + 1 |
|||
ok |
|||
⚫ | |||
add(binpow, numb) |
|||
next |
next |
||
showarray(binpow) |
|||
see nl |
|||
showOne("3^x:", pows) |
|||
⚫ | |||
showOne("Evil numbers:", evens) |
|||
showarray(bineven) |
|||
⚫ | |||
see nl |
|||
⚫ | |||
showarray(binodd) |
|||
see nl |
|||
func |
func onesCount(b) |
||
c = 0 m = 50 |
|||
while b > 0 |
|||
p = pow(2, m) |
|||
if b >= p b -= p c++ ok |
|||
m-- |
|||
⚫ | |||
svect = left(svect, len(svect) - 2) |
|||
see svect |
|||
see "]" + nl |
|||
</lang> |
|||
Output: |
|||
⚫ | |||
3^x: |
|||
⚫ | |||
func arrayToStr(ary) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
func showOne(title, ary) |
|||
⚫ | |||
? arrayToStr(ary) + nl</lang> |
|||
{{out}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 |
[ 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}}== |