Population count: Difference between revisions

m
typo
m (typo)
 
(12 intermediate revisions by 8 users not shown)
Line 39:
{{trans|Python}}
 
<syntaxhighlight 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 368 ⟶ 365:
00 03 05 06 09 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
01 02 04 07 08 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|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN popcount n:
IF n=0: RETURN 0
RETURN (n mod 2) + popcount (floor (n/2))
 
HOW TO REPORT evil n:
REPORT (popcount n) mod 2 = 0
 
HOW TO REPORT odious n:
REPORT (popcount n) mod 2 = 1
 
FOR i IN {0..29}: WRITE popcount (3 ** i)
WRITE /
 
PUT {} IN evilnums
PUT {} IN odiousnums
 
FOR n IN {0..59}:
SELECT:
evil n: INSERT n IN evilnums
odious n: INSERT n IN odiousnums
 
FOR i IN {1..30}: WRITE evilnums item i
WRITE /
FOR i IN {1..30}: WRITE odiousnums item i
WRITE /</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|Ada}}==
Line 1,017 ⟶ 1,045:
return popul
end function</syntaxhighlight>
 
{{out}}
==={{header|Run BASIC}}===
<pre>Same as Yabasic entry.</pre>
<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}}==
Line 1,643 ⟶ 1,739:
 
</syntaxhighlight>
 
=={{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}}==
Line 1,939 ⟶ 2,070:
=={{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}}==
Line 3,049 ⟶ 3,206:
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>
 
Line 3,793 ⟶ 3,995:
$c;
}</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Gen 30 All Pow3 (1)>>
<Prout <Gen 30 Evil Iota (0)>>
<Prout <Gen 30 Odious Iota (0)>>;
};
 
Gen {
0 s.Fil s.Gen (s.State) = ;
s.N s.Fil s.Gen (s.State),
<Mu s.Gen s.State>: (s.Next) s.Item,
<Mu s.Fil s.Item>: {
T = s.Item <Gen <- s.N 1> s.Fil s.Gen (s.Next)>;
F = <Gen s.N s.Fil s.Gen (s.Next)>;
};
};
 
Popcount {
0 = 0;
s.N, <Divmod s.N 2>: (s.R) s.B = <+ s.B <Popcount s.R>>;
};
 
Pow3 {
s.N = (<* 3 s.N>) <Popcount s.N>;
};
 
Evil {
s.N, <Mod <Popcount s.N> 2>: {
0 = T;
1 = F;
};
};
 
Odious {
s.N, <Mod <Popcount s.N> 2>: {
0 = F;
1 = T;
};
};
 
All { s.X = T; }
Iota { s.N = (<+ 1 s.N>) s.N; }</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|REXX}}==
Line 4,045 ⟶ 4,294:
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}}==
<syntaxhighlight lang="ruby">func population_count(n) { n.as_bin.count('1') }
Line 4,380 ⟶ 4,651:
{{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.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var popCount = Fn.new { |n|
Line 4,427 ⟶ 4,698:
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>
 
2,094

edits