Population count: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
(27 intermediate revisions by 18 users not shown)
Line 31:
 
;See also
* The On-Line Encyclopedia of Integer Sequences:   [[oeis:A000120|A000120 population count]].
* The On-Line Encyclopedia of Integer Sequences:   [[oeis:A000069|A000069 odious numbers]].
* The On-Line Encyclopedia of Integer Sequences:   [[oeis:A001969|A001969 evil numbers]].
Line 38 ⟶ 39:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">Fprint((0.<30).map(i -> bits:popcount(nInt64(3) ^ i)))
R bin(n).count(‘1’)
 
print((0.<30).map(i -> popcount(Int64(3) ^ i)))
 
[Int] evil, odious
V i = 0
L evil.len < 30 | odious.len < 30
V p = bits:popcount(i)
I (p % 2) != 0
odious.append(i)
Line 54 ⟶ 52:
 
print(evil[0.<30])
print(odious[0.<30])</langsyntaxhighlight>
 
{{out}}
Line 69 ⟶ 67:
* in Unnormalized Double Floating Point, one is implemented X'4E00000000000001'
<br>
<langsyntaxhighlight lang="360asm">* Population count 09/05/2019
POPCNT CSECT
USING POPCNT,R13 base register
Line 150 ⟶ 148:
CC DS C
REGEQU
END POPCNT </langsyntaxhighlight>
{{out}}
<pre>
Line 164 ⟶ 162:
It will only run correctly on a real 8080.
 
<langsyntaxhighlight lang="8080asm"> org 100h
mvi e,30 ; 3^0 to 3^29 inclusive
powers: push d ; Keep counter
Line 261 ⟶ 259:
nl: db 13,10,'$'
pow3: db 1,0,0,0,0,0 ; pow3, starts at 1
pow3c: equ $ ; room for copy</langsyntaxhighlight>
 
{{out}}
Line 271 ⟶ 269:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> cpu 8086
bits 16
org 100h
Line 360 ⟶ 358:
mov dl,' '
int 21h
ret</langsyntaxhighlight>
 
{{out}}
Line 372 ⟶ 370:
Specification and implementation of an auxiliary package "Population_Count". The same package is used for [[Pernicious numbers#Ada]]
 
<langsyntaxhighlight lang="ada">with Interfaces;
 
package Population_Count is
subtype Num is Interfaces.Unsigned_64;
function Pop_Count(N: Num) return Natural;
end Population_Count;</langsyntaxhighlight>
 
<langsyntaxhighlight Adalang="ada">package body Population_Count is
function Pop_Count(N: Num) return Natural is
Line 396 ⟶ 394:
end Pop_Count;
end Population_Count;</langsyntaxhighlight>
 
The main program:
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Population_Count; use Ada.Text_IO; use Population_Count;
 
procedure Test_Pop_Count is
Line 436 ⟶ 434:
end loop;
New_Line;
end Test_Pop_Count;</langsyntaxhighlight>
 
{{out}}
Line 445 ⟶ 443:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># returns the population count (number of bits on) of the non-negative #
# integer n #
PROC population count = ( LONG INT n )INT:
Line 486 ⟶ 484:
OD;
print( ( newline ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 495 ⟶ 493:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% returns the population count (number of bits on) of the non-negative integer n %
integer procedure populationCount( integer value n ) ;
Line 563 ⟶ 561:
end odious_numbers_loop
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 572 ⟶ 570:
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
APL (DYALOG APL)
popDemo←{⎕IO←0
Line 600 ⟶ 598:
'***Passes' '***Fails'⊃⍨(ans3≢act3)∨(actEvil≢ansEvil)∨(actOdious≢ansOdious)
}
</syntaxhighlight>
</lang>
{{out}}
popDemo 30
Line 612 ⟶ 610:
 
{{Trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">--------------------- POPULATION COUNT ---------------------
 
-- populationCount :: Int -> Int
Line 834 ⟶ 832:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>Population counts of the first 30 powers of three:
Line 848 ⟶ 846:
===Straightforward===
 
<langsyntaxhighlight lang="applescript">on popCount(n)
set counter to 0
repeat until (n is 0)
Line 880 ⟶ 878:
as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Popcounts of 1st thirty powers of 3:
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
1st thirty 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
1st thirty 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"</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">popCount: function [num][
size select split to :string as.binary num 'x -> x="1"
]
Line 902 ⟶ 900:
 
print "first thirty odious numbers"
print take select 0..100 => [odd? popCount &] 30</langsyntaxhighlight>
 
{{out}}
Line 914 ⟶ 912:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Loop, 30
Out1 .= PopCount(3 ** (A_Index - 1)) " "
Loop, 60
Line 926 ⟶ 924:
, x := (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
return (x * 0x0101010101010101) >> 56
}</langsyntaxhighlight>
{{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
Line 933 ⟶ 931:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f POPULATION_COUNT.AWK
# converted from VBSCRIPT
Line 970 ⟶ 968:
return(y)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 977 ⟶ 975:
odious: 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>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Yabasic}}
<syntaxhighlight lang="basic256">print "Pop cont (3^x): ";
for i = 0 to 29
print population(3^i); " "; #los últimos números no los muestra correctamente
next i
 
print : print
print "Evil numbers: ";
call EvilOdious(30, 0)
 
print : print
print "Odious numbers: ";
call EvilOdious(30, 1)
end
 
subroutine EvilOdious(limit, type)
i = 0 : cont = 0
 
do
eo = (population(i) mod 2)
if (type and eo) or (not type and not eo) then
cont += 1 : print i; " ";
end if
i += 1
until (cont = limit)
end subroutine
 
function population(number)
popul = 0
 
binary$ = tobinary(number)
for i = 1 to length(binary$)
popul += int(mid(binary$, i, 1))
next i
return popul
end function</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="vb">function tobin$(num)
bin$ = ""
if num = 0 then bin$ = "0"
while num >= 1
num = num / 2
X$ = str$(num)
D$ = "": F$ = ""
for i = 1 to len(X$)
L$ = mid$(X$, i, 1)
if L$ <> "." then
D$ = D$ + L$
else
F$ = F$ + right$(X$, len(X$) - i)
exit for
end if
next i
if F$ = "" then B$ = "0" else B$ = "1"
bin$ = bin$ + B$
num = val(D$)
wend
B$ = ""
for i = len(bin$) to 1 step -1
B$ = B$ + mid$(bin$, i, 1)
next i
tobin$ = B$
end function
 
function population(number)
popul = 0
'digito$ = tobin$(number)
'print tobin$(number)
for i = 1 to len(tobin$(number))
popul = popul + val(mid$(tobin$(number), i, 1))
next i
population = popul
end function
 
sub evilodious limit, tipo
i = 0
cont = 0
while 1
eo = (population(i) mod 2)
if (tipo and eo = 1) or ((not(tipo) and not(eo)) = 1) then
cont = cont + 1: print i; " ";
end if
i = i + 1
if cont = limit then exit while
wend
end sub
 
print "Pop cont (3^x): ";
for i = 0 to 14
print population(3 ^ i); " ";
next i
 
print
print "Evil numbers: ";
call evilodious 15, 0
 
print
print "Odious numbers: ";
call evilodious 15, 1
end</syntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Definitions
Line 1,048 ⟶ 1,155:
printFirst(30, evil)
printFirst(30, odious)
$)</langsyntaxhighlight>
{{out}}
<pre> 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
Line 1,054 ⟶ 1,161:
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>
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">PopCount ← {(2|𝕩)+𝕊⍟×⌊𝕩÷2}
Odious ← 2|PopCount
Evil ← ¬Odious
Line 1,061 ⟶ 1,168:
>⟨PopCount¨ 3⋆↕30,
Evil _List 30,
Odious _List 30⟩</langsyntaxhighlight>
{{out}}
<pre>┌─
Line 1,071 ⟶ 1,178:
=={{header|C}}==
{{works with|GCC}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main() {
Line 1,109 ⟶ 1,216:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,119 ⟶ 1,226:
 
GCC's builtin doesn't exist prior to 3.4, and the LL version is broken in 3.4 to 4.1. In 4.2+, if the platform doesn't have a good popcount instruction or isn't enabled (e.g. not compiled with <code>-march=native</code>), it typically emits unoptimized code which is over 2x slower than the C below. Alternative:
<langsyntaxhighlight lang="c">#if defined(__POPCNT__) && defined(__GNUC__) && (__GNUC__> 4 || (__GNUC__== 4 && __GNUC_MINOR__> 1))
#define HAVE_BUILTIN_POPCOUNTLL
#endif
Line 1,134 ⟶ 1,241:
b = (b + (b >> 4)) & 0x0f0f0f0f;
return (b * 0x01010101) >> 24;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.Linq;
Line 1,208 ⟶ 1,315:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,219 ⟶ 1,326:
=={{header|C++}}==
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <bitset>
#include <climits>
Line 1,260 ⟶ 1,367:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,270 ⟶ 1,377:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(defn population-count [n]
(Long/bitCount n)) ; use Java inter-op
Line 1,298 ⟶ 1,405:
 
(defn odious-numbers []
(filter odious? (integers)))</langsyntaxhighlight>
 
{{out}}
Line 1,318 ⟶ 1,425:
; ==> (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>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">pop_count = proc (n: int) returns (int)
p: int := 0
while n>0 do
p := p + n//2
n := n/2
end
return(p)
end pop_count
 
evil = proc (n: int) returns (bool)
return(pop_count(n)//2 = 0)
end evil
 
odious = proc (n: int) returns (bool)
return(~evil(n))
end odious
 
first = iter (n: int, p: proctype (int) returns (bool)) yields (int)
i: int := 0
while n>0 do
if p(i) then
yield(i)
n := n-1
end
i := i+1
end
end first
 
start_up = proc ()
po: stream := stream$primary_output()
for i: int in int$from_to(0,29) do
stream$putright(po, int$unparse(pop_count(3**i)), 3)
end
stream$putl(po, "")
for i: int in first(30, evil) do
stream$putright(po, int$unparse(i), 3)
end
stream$putl(po, "")
for i: int in first(30, odious) do
stream$putright(po, int$unparse(i), 3)
end
stream$putl(po, "")
end start_up</syntaxhighlight>
{{out}}
<pre> 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
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
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>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. HAMMING.
 
Line 1,392 ⟶ 1,551:
DIVIDE 2 INTO POPCOUNT-IN.
IF BIT-IS-SET, ADD 1 TO POPCOUNT-OUT.
MOVE POPCOUNT-REST TO POPCOUNT-IN.</langsyntaxhighlight>
{{out}}
<pre> 3^ EVIL ODD
Line 1,427 ⟶ 1,586:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(format T "3^x: ~{~a ~}~%"
(loop for i below 30
collect (logcount (expt 3 i))))
Line 1,438 ⟶ 1,597:
finally (return (values evil odious)))
(format T "evil: ~{~a ~}~%" evil)
(format T "odious: ~{~a ~}~%" odious))</langsyntaxhighlight>
{{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
Line 1,447 ⟶ 1,606:
{{trans|Ruby}}
For Crystal select|reject are inherently lazy enumerators, and has popcount for upto unsigned 64-bit integers.
<langsyntaxhighlight lang="ruby">struct Int
def evil?
self >= 0 && popcount.even?
Line 1,455 ⟶ 1,614:
puts "Powers of 3:", (0...30).map{|n| (3u64 ** n).popcount}.join(' ') # can also use &** (to prevent arithmetic overflow)
puts "Evil:" , 0.step.select(&.evil?).first(30).join(' ')
puts "Odious:", 0.step.reject(&.evil?).first(30).join(' ')</langsyntaxhighlight>
{{Out}}<pre>Powers of 3:
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
Line 1,465 ⟶ 1,624:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, core.bitop;
 
Line 1,473 ⟶ 1,632:
uint.max.iota.filter!(i => pCount(i) % 2 == 0).take(30),
uint.max.iota.filter!(i => pCount(i) % 2).take(30));
}</langsyntaxhighlight>
{{out}}
<pre>[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]
Line 1,482 ⟶ 1,641:
{{libheader| System.SysUtils, Math}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Population_count;
 
Line 1,548 ⟶ 1,707:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func popcnt x .
while x > 0
r += x mod 2
x = x div 2
.
return r
.
proc show3 . .
write "3^n:"
bb = 1
for i = 1 to 30
write " " & popcnt bb
bb *= 3
.
print ""
.
proc show s$ x . .
write s$
while n < 30
if popcnt i mod 2 = x
n += 1
write " " & i
.
i += 1
.
print ""
.
show3
show "evil:" 0
show "odious:" 1
</syntaxhighlight>
 
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Population do
 
def count(n), do: count(<<n :: integer>>, 0)
Line 1,570 ⟶ 1,764:
IO.inspect Stream.iterate(0, &(&1+1)) |> Stream.filter(&Population.evil?(&1)) |> Enum.take(30)
IO.puts "first thirty odious numbers:"
IO.inspect Stream.iterate(0, &(&1+1)) |> Stream.filter(&Population.odious?(&1)) |> Enum.take(30)</langsyntaxhighlight>
 
{{out}}
Line 1,583 ⟶ 1,777:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">-module(population_count).
-export([popcount/1]).
 
Line 1,632 ⟶ 1,826:
io:format("Powers of 3: ~p~n",[threes(30)]),
io:format("Evil:~p~n",[evil(30)]),
io:format("Odious:~p~n",[odious(30)]).</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="erlang">61> population_count:task().
Powers of 3: [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]
Line 1,641 ⟶ 1,835:
Odious:[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]
ok</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Population count. Nigel Galloway: February 18th., 2021
let pC n=Seq.unfold(fun n->match n/2L,n%2L with (0L,0L)->None |(n,g)->Some(g,n))n|>Seq.sum
Line 1,650 ⟶ 1,844:
printf "evil :"; Seq.initInfinite(int64)|>Seq.filter(fun n->(pC n) &&& 1L=0L)|>Seq.take 30|>Seq.iter(printf "%3d"); printfn ""
printf "odious:"; Seq.initInfinite(int64)|>Seq.filter(fun n->(pC n) &&& 1L=1L)|>Seq.take 30|>Seq.iter(printf "%3d"); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,658 ⟶ 1,852:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting kernel lists lists.lazy math math.bitwise
math.functions namespaces prettyprint.config sequences ;
 
Line 1,667 ⟶ 1,861:
100 margin set 0 lfrom [ 3^n ] [ evil ] [ odious ] tri
[ 30 swap ltake list>array ] tri@
"3^n: %u\nEvil: %u\nOdious: %u\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 1,676 ⟶ 1,870:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Popcount(n) = if n = 0 then 0 else if 2*(n\2)=n then Popcount(n\2) else Popcount((n-1)\2)+1 fi fi.
Func Odiousness(n) = p:=Popcount(n);if 2*(p\2) = p then 0 else 1 fi.
 
Line 1,685 ⟶ 1,879:
e:=0
n:=0
while e<30 do if Odiousness(n)=1 then !n;!' ';e:=e+1 fi; n:=n+1 od</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|Gforth|0.7.3}}
<langsyntaxhighlight Forthlang="forth">: popcnt ( n -- u) 0 swap
BEGIN dup WHILE tuck 1 AND + swap 1 rshift REPEAT
DROP ;
Line 1,708 ⟶ 1,902:
over odious? IF over . 1+ THEN swap 1+ swap
REPEAT DROP DROP CR ;
task1 task2 task3 BYE</langsyntaxhighlight>
{{out}}
<pre>3**i popcnt: 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
Line 1,716 ⟶ 1,910:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program population_count
implicit none
 
Line 1,766 ⟶ 1,960:
 
end function
end program</langsyntaxhighlight>
{{out}}
<pre> 3**i : 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
Line 1,776 ⟶ 1,970:
It accepts one integer parameter and is defined for all unsigned integer types.
Therefore its implementation is skipped.
<langsyntaxhighlight lang="pascal">program populationCount(input, output, stdErr);
var
// general advice: iterator variables are _signed_
Line 1,837 ⟶ 2,031:
end;
writeLn();
end.</langsyntaxhighlight>
{{out}}
<pre> 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
Line 1,845 ⟶ 2,039:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Population_count}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
Fōrmulæ has an integrated expression BitCount that counts the number of 1's of the binary representation of the number.
In '''[https://formulae.org/?example=Population_count this]''' page you can see the program(s) related to this task and their results.
 
However, a function can also be written, as follows:
 
[[File:Fōrmulæ - Population count 01.png]]
 
'''Case 1. Display the pop count of the 1st thirty powers of 3'''
 
[[File:Fōrmulæ - Population count 02.png]]
 
[[File:Fōrmulæ - Population count 03.png]]
 
'''Case 2. Display the 1st thirty evil numbers'''
 
We need first a function to calculate the first numbers whose population count satisfies a given condition, passed as a lambda expression:
 
[[File:Fōrmulæ - Population count 04.png]]
 
[[File:Fōrmulæ - Population count 05.png]]
 
[[File:Fōrmulæ - Population count 06.png]]
 
'''Case 3. Display the 1st thirty odious numbers'''
 
[[File:Fōrmulæ - Population count 07.png]]
 
[[File:Fōrmulæ - Population count 08.png]]
 
=={{Header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#define NTERMS 30
 
Line 1,889 ⟶ 2,109:
print s_tp
print s_evil
print s_odious</langsyntaxhighlight>
{{out}}
<pre>
Line 1,896 ⟶ 2,116:
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>
 
 
=={{Header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_limit = 30
 
local fn Population( x as long ) as long
long q, r, y = 0
while ( x > 0 )
q = int( x / 2 )
r = x - q * 2
if r == 1 then y++
x = q
wend
end fn = y
 
void local fn EvilOdious
long i = 0, k, ee = 0, eo = 0
long type(_limit - 1), evil(_limit - 1), odious(_limit - 1)
Str255 typeStr, evilStr, odiousStr
while ( ( ee < _limit ) or ( eo < _limit ) )
if i < _limit then type(i) = fn Population(3^i)
k = fn Population(i)
if k mod 2 == 0 and ee < _limit then evil(ee) = i : ee++
if k mod 2 == 1 and eo < _limit then odious(eo) = i : eo++
i++
wend
typeStr = "" : evilStr = "" : odiousStr = ""
for i = 0 to _limit - 1
typeStr = typeStr + str$( type(i) ) + " "
evilStr = evilStr + str$( evil(i) ) + " "
odiousStr = odiousStr + str$( odious(i) ) + " "
next
print typeStr : print evilStr : print odiousStr
end fn
 
fn EvilOdious
 
HandleEvent
</syntaxhighlight>
{{output}}
<pre>
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
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
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>
 
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=538335b7b71f5ea7b59c0c82fbb0ea3e Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sEvil, sOdious As String 'To store the output for printing Evil and Odious
Dim iCount, iEvil, iOdious As Integer 'Counters
Line 1,926 ⟶ 2,196:
Print "1st 30 Odious numbers =\t" & sOdious 'Print Odious
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,937 ⟶ 2,207:
===Standard Library===
As of Go 1.9, this function is in the standard Library.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,974 ⟶ 2,244:
}
fmt.Println()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,986 ⟶ 2,256:
===Implementation===
Method of WP example '''popcount_3''':
<langsyntaxhighlight lang="go">func pop64(w uint64) int {
const (
ff = 1<<64 - 1
Line 1,998 ⟶ 2,268:
w = (w + w>>4) & maskf
return int(w * maskp >> 56)
}</langsyntaxhighlight>
Method of WP example '''popcount_4''':
<langsyntaxhighlight lang="go">func pop64(w uint64) (c int) {
for w != 0 {
w &= w - 1
Line 2,006 ⟶ 2,276:
}
return
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
{{works with|GHC|7.4+}}
<langsyntaxhighlight lang="haskell">import Data.Bits (popCount)
 
printPops :: (Show a, Integral a) => String -> [a] -> IO ()
Line 2,019 ⟶ 2,289:
printPops "popcount " $ map popCount $ iterate (*3) (1 :: Integer)
printPops "evil " $ filter (even . popCount) ([0..] :: [Integer])
printPops "odious " $ filter ( odd . popCount) ([0..] :: [Integer])</langsyntaxhighlight>
{{out}}
<pre>
Line 2,029 ⟶ 2,299:
Or, if we want to write our own popCount, perhaps something like:
 
<langsyntaxhighlight lang="haskell">import Data.Bifoldable (biList)
import Data.List (partition, unfoldr)
import Data.Tuple (swap)
Line 2,050 ⟶ 2,320:
( (popCount . (3 ^) <$> [0 .. 29]) :
biList (partition (even . popCount) [0 .. 59])
)</langsyntaxhighlight>
{{Out}}
<pre>Population count of powers of 3:
Line 2,063 ⟶ 2,333:
=={{header|Idris}}==
 
<langsyntaxhighlight Idrislang="idris">module Main
import Data.Vect
 
Line 2,102 ⟶ 2,372:
printCompact (filterUnfoldN 30 isEvil id (1 +) 0)
putStr "Odious: "
printCompact (filterUnfoldN 30 isOdious id (1 +) 0)</langsyntaxhighlight>
{{Out}}
<pre>popcnt(3**i): 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
Line 2,112 ⟶ 2,382:
Implementation:
 
<langsyntaxhighlight Jlang="j">countPopln=: +/"1@#:
isOdd=: 1 = 2&|
isEven=: 0 = 2&|</langsyntaxhighlight>
 
 
Task:
 
<langsyntaxhighlight Jlang="j"> countPopln 3^i.30x
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
30{.(#~ isOdd@countPopln) i. 100 NB. odd population count (aka "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
30{.(#~ isEven@countPopln) i. 100 NB. even population count (aka "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</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public class PopCount {
Line 2,182 ⟶ 2,452:
System.out.println();
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,195 ⟶ 2,465:
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 2,324 ⟶ 2,594:
// ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Population counts of the first 30 powers of three:
Line 2,337 ⟶ 2,607:
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def popcount:
def bin: recurse( if . == 0 then empty else ./2 | floor end ) % 2;
[bin] | add;
Line 2,362 ⟶ 2,632:
;
 
task</langsyntaxhighlight>
{{Out}}
$ jq -n -r -c -f Population_count.jq
Line 2,373 ⟶ 2,643:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">println("First 3 ^ i, up to 29 pop. counts: ", join((count_ones(3 ^ n) for n in 0:29), ", "))
println("Evil numbers: ", join(filter(x -> iseven(count_ones(x)), 0:59), ", "))
println("Odious numbers: ", join(filter(x -> isodd(count_ones(x)), 0:59), ", "))</langsyntaxhighlight>
 
{{out}}
Line 2,383 ⟶ 2,653:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun popCount(n: Long) = when {
Line 2,422 ⟶ 2,692:
}
println()
}</langsyntaxhighlight>
 
{{out}}
Line 2,437 ⟶ 2,707:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Take decimal number, return binary string
function dec2bin (n)
local bin, bit = ""
Line 2,479 ⟶ 2,749:
firstThirty("3^x")
firstThirty("Evil")
firstThirty("Odious")</langsyntaxhighlight>
{{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: 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: 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>
 
=={{header|M2000 Interpreter}}==
 
=====Using just Count() and loops=====
<syntaxhighlight lang="m2000 interpreter">
Module Population_count{
Function Count(x as long long) {
integer Count
long long m=x
m|div 0x1_0000_0000&&
x|mod 0x1_0000_0000&&
While x<>0&
x=Binary.And(X, X-1&&)
Count++
End While
x=m
While x<>0&
x=Binary.And(X, X-1&&)
Count++
End While
=Count
}
long long i, b=3
stack new {
for i=0 to 29
Data count(b^i)
next
print "3^x population:", array([])#str$()
i=0: b=0
while i<30
if Count(b) mod 2=0 then data b:i++
b++
end while
print "evil numbers:", array([])#str$()
i=0: b=0
while i<30
if Count(b) mod 2=1 then data b:i++
b++
end while
print "odious numbers:", array([])#str$()
}
}
Population_count
</syntaxhighlight>
 
=====Using Generators=====
 
<syntaxhighlight lang="m2000 interpreter">
Module Population_count{
Count=lambda (x as long long)->{
integer Count
long long m=x
m|div 0x1_0000_0000&&
x|mod 0x1_0000_0000&&
While x<>0&
x=Binary.And(X, X-1&&)
Count++
End While
x=m
While x<>0&
x=Binary.And(X, X-1&&)
Count++
End While
=Count
}
series3pow=lambda Count, i=0&& -> {
=count(3&&^i):i++
}
seriesEvil=lambda Count, i=0&&-> {
while Count(i) mod 2=1{i++}
=i:i++
}
seriesOdious=lambda Count, i=0&&-> {
while Count(i) mod 2=0{i++}
=i:i++
}
Dim a(30)<<series3pow()
print "3^x population:", a()#str$()
Dim a(30)<<seriesEvil()
print "evil numbers:", a()#str$()
Dim a(30)<<seriesOdious()
print "odious numbers:", a()#str$()
}
Population_count
</syntaxhighlight>
{{out}}
<pre>
3^x population: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: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>
 
=={{header|MAD}}==
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION LOWBIT.(K) = K-K/2*2
Line 2,530 ⟶ 2,891:
VECTOR VALUES NUMFMT = $I2*$
END OF PROGRAM</langsyntaxhighlight>
 
{{out}}
Line 2,629 ⟶ 2,990:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">popcount[n_Integer] := IntegerDigits[n, 2] // Total
Print["population count of powers of 3"]
popcount[#] & /@ (3^Range[0, 30])
Line 2,653 ⟶ 3,014:
]
Print["first thirty odious numbers"]
odiouslist</langsyntaxhighlight>
{{out}}
<pre>population count of powers of 3
Line 2,664 ⟶ 3,025:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(2 over over mod 'div dip) :divmod2
 
(
Line 2,676 ⟶ 3,037:
"3^n: " print! 30 iota (3 swap pow int pop-count) map puts!
60 iota (pop-count odd?) partition
"Evil: " print! puts! "Odious: " print! puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 2,683 ⟶ 3,044:
Odious: (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>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map (show . take 30) [powers_of_3, evil, odious]))]
 
powers_of_3 :: [num]
powers_of_3 = map (popcount . (3^)) [0..]
 
evil :: [num]
evil = filter f [0..] where f n = popcount n mod 2 = 0
 
odious :: [num]
odious = filter f [0..] where f n = popcount n mod 2 = 1
 
popcount :: num -> num
popcount 0 = 0
popcount n = n mod 2 + popcount (n div 2)</syntaxhighlight>
{{out}}
<pre>[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]
[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]
[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>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import bitops
import strformat
 
Line 2,712 ⟶ 3,094:
for i in 0..<30:
write(stdout, fmt"{od[i]:2} ")
write(stdout, '\n')</langsyntaxhighlight>
 
{{out}}
Line 2,722 ⟶ 3,104:
 
Another version, in functional style, with most computations done at compile time:
<langsyntaxhighlight Nimlang="nim">import bitops, math, sequtils, strutils
 
const
Line 2,733 ⟶ 3,115:
echo "3^n: ", popcounts.mapIt(($it).align(2)).join(" ")
echo "evil: ", evil.mapIt(($it).align(2)).join(" ")
echo "odious:", odious.mapIt(($it).align(2)).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 2,739 ⟶ 3,121:
evil: 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: 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>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let popcount n =
let rec aux acc = function
| 0 -> acc
| x -> aux (succ acc) (x land pred x)
in
aux 0 n
 
let is_parity p x =
p = 1 land popcount x
 
(* test code *)
 
let powers3_seq () =
Seq.unfold (fun x -> Some (popcount x, x * 3)) 1
 
let parity_seq p =
Seq.(filter (is_parity p) (ints 0))
 
let print_seq_30 s =
Seq.(s |> take 30 |> map string_of_int)
|> List.of_seq |> String.concat " " |> print_endline
 
let () = print_seq_30 (powers3_seq ())
let () = print_seq_30 (parity_seq 0)
let () = print_seq_30 (parity_seq 1)</syntaxhighlight>
{{out}}
<pre>
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
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
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>
 
=={{header|Oforth}}==
<langsyntaxhighlight lang="oforth">: popcount(n)
0 while ( n ) [ n isOdd + n bitRight(1) ->n ] ;
 
Line 2,752 ⟶ 3,167:
 
0 ->count
0 while( count 30 <> ) [ dup popcount isOdd ifTrue: [ dup . count 1+ ->count ] 1+ ] drop ;</langsyntaxhighlight>
 
{{out}}
Line 2,760 ⟶ 3,175:
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
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 ok
</pre>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(define (popcount n)
(let loop ((n n) (c 0))
(if (= n 0)
c
(loop (>> n 1)
(if (eq? (band n 1) 0) c (+ c 1))))))
(print (popcount 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253))
 
 
(define thirty 30)
 
(display "popcount:")
(for-each (lambda (i)
(display " ")
(display (popcount (expt 3 i))))
(iota thirty 0))
(print)
 
(define (evenodd name test)
(display name) (display ":")
(for-each (lambda (i)
(display " ")
(display i))
(reverse
(let loop ((n 0) (i 0) (out '()))
(if (= i thirty)
out
(if (test (popcount n))
(loop (+ n 1) (+ i 1) (cons n out))
(loop (+ n 1) i out))))))
(print))
 
(evenodd "evil" even?)
(evenodd "odius" odd?)
</syntaxhighlight>
{{out}}
<pre>
159
popcount: 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: 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
odius: 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>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">vector(30,n,hammingweight(3^(n-1)))
od=select(n->hammingweight(n)%2,[0..100]); ev=setminus([0..100],od);
ev[1..30]
od[1..30]</langsyntaxhighlight>
{{out}}
<pre>%1 = [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]
Line 2,775 ⟶ 3,235:
{{works with|freepascal}}
Like Ada a unit is used.
<langsyntaxhighlight lang="pascal">unit popcount;
{$IFDEF FPC}
{$MODE DELPHI}
Line 2,856 ⟶ 3,316:
 
Begin
End.</langsyntaxhighlight>
The program
<langsyntaxhighlight lang="pascal">program pcntTest;
uses
sysutils,popCount;
Line 2,906 ⟶ 3,366:
until k = 30;
writeln(s);
end.</langsyntaxhighlight>
;Output:
<pre>PopCnt 3^i :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
Line 2,914 ⟶ 3,374:
 
Some processors define the <code>card</code> function, which can be used in conjunction with sets:
<langsyntaxhighlight lang="pascal">var
i: integer;
f: set of 0..(bitSizeOf(i)-1) absolute i; // same address as i, but different interpretation
begin
writeLn(card(f));
end;</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 2,926 ⟶ 3,386:
We'll emulate infinite lists with closures.
 
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
sub population_count {
my $n = shift;
die "argument can't be negative" if $n < 0;
my $c;
for ($c = 0; $n; $n >>= 1) {
$c += $n & 1;
}
$c;
}
 
print join ' ', map { population_count(3**$_) } 0 .. 30 - 1;
print "\n";
sub evil {
my $i = 0;
sub { $i++ while population_count($i) % 2; $i++ }
}
 
sub odious {
my $i = 0;
Line 2,950 ⟶ 3,400:
}
 
sub population_count {
my ($evil, $odious) = (evil, odious);
my (@evil,$n = @odious)shift;
my $c;
for (1 .. 30) {
for ($c = 0; $n; $n >>= 1) { $c += $n & 1 }
push @evil, $evil->();
$c
push @odious, $odious->();
}
 
say join ' ', map { population_count 3**$_ } 0 .. 30 - 1;
printf "Evil : %s\n", join ' ', @evil;
 
printf "Odious: %s\n", join ' ', @odious;</lang>
my (@evil, @odious);
my ($evil, $odious) = (evil, odious);
push( @evil, $evil->() ), push @odious, $odious->() for 1 .. 30;
 
say "Evil @evil";
say "Odious @odious";</syntaxhighlight>
{{out}}
<pre>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 : 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: 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>
 
 
A faster population count can be done with pack/unpack:
<syntaxhighlight lang="text">say unpack("%b*",pack "J*", 1234567); # J = UV</langsyntaxhighlight>
 
Various modules can also perform a population count, with the first of these being faster than the pack/unpack builtins. The first three easily support bigints, the last will with some adjustment.
<langsyntaxhighlight lang="perl">use ntheory qw/hammingweight/;
say hammingweight(1234567);
 
Line 2,979 ⟶ 3,434:
 
use Bit::Vector;
say Bit::Vector->new_Dec(64,1234567)->Norm;</langsyntaxhighlight>
 
=={{header|Phix}}==
As of 1.0.2 there is a builtin count_bits(), and also mpz_popcount(), both of which match the results from pop_count() below.
<lang Phix>function pop_count(atom n)
<!--<syntaxhighlight lang="phix">(phixonline)-->
if n<0 then ?9/0 end if
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer res = 0
<span style="color: #008080;">function</span> <span style="color: #000000;">pop_count</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
while n!=0 do
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res += and_bits(n,1)
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
n = floor(n/2)
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
end while
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
return res
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
sequence s = {}
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
for i=0 to 29 do
s &= pop_count(power(3,i))
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3^x pop_counts:%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">pop_count</span><span style="color: #0000FF;">)})</span>
end for
puts(1,"3^x pop_counts:") ?s
<span style="color: #008080;">procedure</span> <span style="color: #000000;">eo</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">)</span>
procedure eo(integer b0, string name)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
integer k=0, l=1
<span style="color: #008080;">while</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">30</span> <span style="color: #008080;">do</span>
while l<=30 do
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pop_count</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">b0</span> <span style="color: #008080;">then</span>
if and_bits(pop_count(k),1)=b0 then
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span>
s[l] = k
<span style="color: #000000;">l</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
l += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
k += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end while
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s numbers:%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
puts(1,name&" numbers:") ?s
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end procedure
<span style="color: #000000;">eo</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" evil"</span><span style="color: #0000FF;">)</span>
eo(0," evil")
<span style="color: #000000;">eo</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"odious"</span><span style="color: #0000FF;">)</span>
eo(1,"odious")</lang>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,019 ⟶ 3,475:
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<lang PHP>
function convertToBinary($integer) {
$binary = "";
Line 3,088 ⟶ 3,544:
echo "\nfirst 30 odious numbers:";
printFirst30Odious();
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,095 ⟶ 3,551:
first 30 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
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
println(powers_of_three=[pop_count(3**I) : I in 0..29]),
println('evil_numbers '=take_n($evil_number, 30,0)),
println('odious_numbers '=take_n($odious_number, 30,0)),
nl.
 
% Get the first N numbers that satisfies function F, starting with S
take_n(F,N,S) = L =>
I = S,
C = 0,
L = [],
while(C < N)
if call(F,I) then
L := L ++ [I],
C := C + 1
end,
I := I + 1
end.
 
evil_number(N) => pop_count(N) mod 2 == 0.
odious_number(N) => pop_count(N) mod 2 == 1.
 
pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).</syntaxhighlight>
 
{{out}}
<pre>powers_of_three = [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 = [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>
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de popz (N)
(cnt
'((N) (= "1" N))
Line 3,122 ⟶ 3,609:
(when (bit? 1 (popz (inc 'N)))
(link N)
(inc 'C) ) ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 3,131 ⟶ 3,618:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function pop-count($n) {
(([Convert]::ToString($n, 2)).toCharArray() | where {$_ -eq '1'}).count
Line 3,138 ⟶ 3,625:
"even pop_count: $($m = $n = 0; while($m -lt 30) {if(0 -eq ((pop-count $n)%2)) {$m += 1; $n}; $n += 1} )"
"odd pop_count: $($m = $n = 0; while($m -lt 30) {if(1 -eq ((pop-count $n)%2)) {$m += 1; $n}; $n += 1} )"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 3,147 ⟶ 3,634:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.i PopCount(n.i) : ProcedureReturn CountString(Bin(Pow(3,n)),"1") : EndProcedure
Procedure PutR(v.i) : Print(RSet(Str(v),3)) : EndProcedure
 
Line 3,161 ⟶ 3,648:
Print("Evil numbers ") : ForEach ne() : PutR(ne()) : Next : PrintN("")
Print("Odious numb..") : ForEach no() : PutR(no()) : Next : Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>
Line 3,171 ⟶ 3,658:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">>>> def popcount(n): return bin(n).count("1")
...
>>> [popcount(3**i) for i in range(30)]
Line 3,186 ⟶ 3,673:
>>> odious[:30]
[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]
>>> </langsyntaxhighlight>
 
===Python: Kernighans' algorithm===
The algorithm is explained [https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/ here]. Replace popcount with pop_kernighan in the example above to get the same evil and odious results.
 
<langsyntaxhighlight lang="python">def pop_kernighan(n):
i = 0
while n:
i, n = i + 1, n & (n - 1)
return i
</syntaxhighlight>
</lang>
 
===Composition of pure functions===
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Population count'''
 
from functools import reduce
Line 3,325 ⟶ 3,812:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Population count of first 30 powers of 3:
Line 3,338 ⟶ 3,825:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap
[ dup while
dup 1 &
Line 3,369 ⟶ 3,856:
cr
say "The first thirty odious numbers." cr
30 echopopwith odd cr</langsyntaxhighlight>
 
{{out}}
Line 3,384 ⟶ 3,871:
=={{header|R}}==
By default, R does not support 64-bit integer types. We therefore need the bit64 library and an awkward popCount function in order to make this work. Aside from the ugly one-liner that is the popCount function, the rest is trivial.
<langsyntaxhighlight Rlang="rsplus">library(bit64)
popCount <- function(x) sum(as.numeric(strsplit(as.bitstring(as.integer64(x)), "")[[1]]))
finder <- function()
Line 3,401 ⟶ 3,888:
cat("The first 30 odious numbers are:", odious)
}
finder()</langsyntaxhighlight>
{{out}}
<pre>The pop count of the 1st 30 powers of 3 are: 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
The first 30 evil numbers are: 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
The first 30 odious numbers are: 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>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Positive version from "popcount_4" in:
;; https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation
Line 3,447 ⟶ 3,935:
(check-true (odious? 1) "the least odious number")
(check-true (odious? #b1011011011) "seven (which is odd) bits")
(check-false (odious? #b011011011) "six bits... is evil"))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,460 ⟶ 3,948:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub population-count(Int $n where * >= 0) { [+] $n.base(2).comb }
 
say map &population-count, 3 «**« ^30;
say "Evil: ", (grep { population-count($_) %% 2 }, 0 .. *)[^30];
say "Odious: ", (grep { population-count($_) % 2 }, 0 .. *)[^30];</langsyntaxhighlight>
{{out}}
<pre>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
Line 3,470 ⟶ 3,958:
Odious: 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>
That's the convenient way to write it, but the following avoids string processing and is therefore about twice as fast:
<syntaxhighlight lang="raku" perl6line>sub population-count(Int $n is copy where * >= 0) {
loop (my $c = 0; $n; $n +>= 1) {
$c += $n +& 1;
}
$c;
}</langsyntaxhighlight>
 
=={{header|REXX}}==
The &nbsp; ''pop count'' &nbsp; is used in some encryption/decryption methods; &nbsp; a major mainframe manufacturer was coerced &nbsp; <br>(many years ago) &nbsp; to add a hardware instruction to count the bits in a (binary) integer.
<langsyntaxhighlight lang="rexx">/*REXX program counts the number of "one" bits in the binary version of a decimal number*/
/*─────────────────── and also generates a specific number of EVIL and ODIOUS numbers.*/
parse arg N B . /*get optional arguments from the C.L. */
Line 3,509 ⟶ 3,997:
d2b: return word( strip( x2b( d2x( arg(1) ) ), 'L', 0) 0, 1) /*dec ──► bin.*/
popCount: return length( space( translate( d2b(arg(1) ), , 0), 0) ) /*count ones. */
showList: say; say 'The 1st' N arg(1)":"; say strip($); #= 0; $=; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,523 ⟶ 4,011:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring"># Project : Population count
<lang ring>
# Project : Population count
 
odds = []
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 59
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)
showOne("Odious numbers:", odds)
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 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</syntaxhighlight>
{{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:
[ 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>
 
=={{header|RPL}}==
{{Trans|Forth}}
≪ # 0b SWAP
'''WHILE''' DUP # 0b ≠ '''REPEAT'''
DUP # 1b AND ROT + SWAP SR '''END'''
DROP B→R
≫ ''''POPCT'''' STO
≪ '''POPCT''' 2 MOD
≫ ‘'''ODUS?'''’ STO
≪ '''ODUS?''' NOT
≫ ‘'''EVIL?'''’ STO
 
≪ → n
≪ { } # 1b 1 n START
DUP '''POPCT''' ROT SWAP + SWAP 3 * NEXT DROP
{ } # 0b WHILE OVER SIZE n < REPEAT
IF DUP '''EVIL?''' THEN SWAP OVER B→R + SWAP END 1 + END DROP
{ } # 0b WHILE OVER SIZE n < REPEAT
IF DUP '''ODUS?''' THEN SWAP OVER B→R + SWAP END 1 + END DROP
≫ ≫ ‘TASK’ STO
30 TASK
{{out}}
<pre>
3: { 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 }
2: { 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 }
1: { 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>
 
=={{header|Ruby}}==
Demonstrating lazy enumerators.
<langsyntaxhighlight lang="ruby">class Integer
def popcount
Line 3,617 ⟶ 4,103:
puts "Powers of 3:", (0...30).map{|n| (3**n).popcount}.join(' ')
puts "Evil:" , 0.step.lazy.select(&:evil?).first(30).join(' ')
puts "Odious:", 0.step.lazy.reject(&:evil?).first(30).join(' ')</langsyntaxhighlight>
{{Output}}<pre>
Powers of 3:
Line 3,628 ⟶ 4,114:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main() {
let mut num = 1u64;
let mut vec = Vec::new();
Line 3,649 ⟶ 4,135:
println!("\nFirst 30 even pop count:\n{:?}",even);
println!("\nFirst 30 odd pop count:\n{:?}",odd);
}</langsyntaxhighlight>
{{out}}
<pre>pop count of 3^0, 3^1 ... 3^29:
Line 3,667 ⟶ 4,153:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">import java.lang.Long.bitCount
 
object PopCount extends App {
Line 3,685 ⟶ 4,171:
println(series(0L).filter(bitCount(_) % 2 != 0).take(nNumber).mkString(", "))
 
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 3,693 ⟶ 4,179:
is used to compute the population count of the bitset.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const func integer: popcount (in integer: n) is
Line 3,724 ⟶ 4,210:
end for;
writeln;
end func;</langsyntaxhighlight>
{{out}}
<pre>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
Line 3,730 ⟶ 4,216:
odious: 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>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program population_count;
print([popcount(3**n) : n in [0..29]]);
print([n : n in [0..59] | evil n]);
print([n : n in [0..59] | odious n]);
 
op evil(n);
return even popcount n;
end op;
 
op odious(n);
return odd popcount n;
end op;
 
op popcount(n);
return +/[[n mod 2, n div:=2](1) : until n=0];
end op;
end program;</syntaxhighlight>
{{out}}
<pre>[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]
[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]
[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>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func population_count(n) { n.as_bin.count('1') }
say "#{0..29 «**« 3 «call« population_count -> join(' ')}"
 
Line 3,739 ⟶ 4,247:
 
say "Evil: #{numbers.grep{_[1] %% 2}.map{.first}.join(' ')}"
say "Odious: #{numbers.grep{_[1] & 1}.map{.first}.join(' ')}"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,748 ⟶ 4,256:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func populationCount(n: Int) -> Int {
guard n >= 0 else { fatalError() }
 
Line 3,772 ⟶ 4,280:
print("Powers:", Array(pows))
print("Evils:", Array(evils))
print("Odious:", Array(odious))</langsyntaxhighlight>
 
{{out}}
Line 3,781 ⟶ 4,289:
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
 
| Pop Count 3^i
Line 3,832 ⟶ 4,340:
endif
"' Odious Numbers : ' $o " []
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,842 ⟶ 4,350:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc hammingWeight {n} {
Line 3,855 ⟶ 4,363:
}
puts "evil: [lrange $e 0 29]"
puts "odious: [lrange $o 0 29]"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,865 ⟶ 4,373:
=={{header|UNIX Shell}}==
{{works with|bash}}
<langsyntaxhighlight lang="bash">popcount() {
local -i n=$1
(( n < 0 )) && return 1
Line 3,897 ⟶ 4,405:
done
echo "evil nums: ${evil[*]:0:30}"
echo "odious nums: ${odious[*]:0:30}"</langsyntaxhighlight>
{{output}}
<pre>powers of 3 popcounts: 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
Line 3,907 ⟶ 4,415:
{{works with|VBA|VBA Excel 2013}}
The Decimal subtype of Variant does the job to expand 32-bit integers (Long) to 28-digit integers (Decimal).
<langsyntaxhighlight lang="vb">Sub Population_count()
nmax = 30
b = 3
Line 3,942 ⟶ 4,450:
Wend
popcount = y
End Function 'popcount </langsyntaxhighlight>
{{out}}
<pre>
Line 3,956 ⟶ 4,464:
Use of the variant currency subtype. Currency mode is a gray area where some operators do not work,
for instance: ^ \ Mod
<langsyntaxhighlight lang="vb">' Population count - VBScript - 10/05/2019
nmax=30
b=3
Line 3,988 ⟶ 4,496:
Wend
popcount=y
End Function 'popcount </langsyntaxhighlight>
{{out}}
<pre>
Line 4,001 ⟶ 4,509:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Console, System.Diagnostics
 
Module Module1
Line 4,051 ⟶ 4,559:
End Function
 
End Module</langsyntaxhighlight>
{{out}}
Added a "Pattern" line. The "Pattern" line shows the sequence pattern of integers for the Evil and Odious output. The pattern goes to about 50, whereas only the first 30 Evil and Odious integers are shown.
Line 4,065 ⟶ 4,573:
{{libheader|Wren-fmt}}
The first part is slightly awkward for Wren as 'native' bit-wise operations are limited to unsigned 32-bit integers and 3^21 exceeds this limit. We therefore need to switch to BigInts just before that point to process the remaining powers.
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var popCount = Fn.new { |n|
Line 4,100 ⟶ 4,608:
odious.add(n)
System.print("\n\nThe first 30 odious numbers are:")
Fmt.print("$d", odious)</langsyntaxhighlight>
 
{{out}}
Line 4,112 ⟶ 4,620:
The first 30 odious numbers are:
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>
 
=={{header|XPL0}}==
Double precision floating point numbers are used because XPL0's 32-bit
integers don't have sufficient precision to reach 3^29. Double precision
has a 53-bit mantissa that can represent integers up to 2^53, which is
approximately 9.0e15 or approximately 3^33, which is sufficient.
<syntaxhighlight lang "XPL0">func PopCnt(N); \Return count of 1s in binary representation of N
real N; int C;
[C:= 0;
while N >= 0.5 do
[if fix(Mod(N, 2.)) = 1 then C:= C+1;
N:= Floor(N/2.);
];
return C;
];
 
proc Show30(LSb); \Display 30 numbers with even or odd population count
int LSb, C; real N; \Least Significant bit determines even or odd
[N:= 0.; C:= 0;
repeat if (PopCnt(N)&1) = LSb then
[RlOut(0, N); C:= C+1];
N:= N+1.;
until C >= 30;
CrLf(0);
];
 
real N; int P;
[Format(3, 0);
Text(0, "Pow 3: ");
N:= 1.;
for P:= 0 to 29 do
[RlOut(0, float(PopCnt(N))); N:= N*3.];
CrLf(0);
Text(0, "Evil: "); Show30(0);
Text(0, "Odious:"); Show30(1);
]</syntaxhighlight>
{{out}}
<pre>
Pow 3: 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: 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: 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>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">print "Pop count (3^x): "
 
for i = 0 to 29
Line 4,148 ⟶ 4,698:
next
return popul
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
Ints have the 1s count as a property.
<langsyntaxhighlight lang="zkl">n:=1; do(30){ print(n.num1s,","); n*=3 } println();
println("evil: ",[0..].filter(30,fcn(n){ n.num1s.isEven }).concat(","));
Line 4,158 ⟶ 4,708:
// now, as an iterator aka lazy:
println("odious: ",(0).walker(*).tweak( // 0,1,2,3,4... iterator
fcn(n){ if(n.num1s.isEven) Void.Skip else n }).walk(30).concat(","));</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits