Population count: Difference between revisions

no edit summary
(→‎{{header|Perl}}: Add Math::BigInt method)
No edit summary
Line 721:
%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]
%3 = [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|Pascal}}==
{{works with|freepascal}}
Like Ada a unit is used.
<lang pascal>unit popcount;
{$IFDEF FPC}
{$MODE DELPHI}
{$OPTIMIZATION ON,ASMCSE,CSE,PEEPHOLE}
{$Smartlink OFF}
{$ENDIF}
 
interface
function popcnt(n:Uint64):integer;overload;
function popcnt(n:Uint32):integer;overload;
function popcnt(n:Uint16):integer;overload;
function popcnt(n:Uint8):integer;overload;
 
implementation
const
//K1 = $0101010101010101;
K33 = $3333333333333333;
K55 = $5555555555555555;
KF1 = $0F0F0F0F0F0F0F0F;
KF2 = $00FF00FF00FF00FF;
KF4 = $0000FFFF0000FFFF;
KF8 = $00000000FFFFFFFF;
{
function popcnt64(n:Uint64):integer;
begin
n := n- (n shr 1) AND K55;
n := (n AND K33)+ ((n shr 2) AND K33);
n := (n + (n shr 4)) AND KF1;
n := (n*k1) SHR 56;
result := n;
end;
}
function popcnt(n:Uint64):integer;overload;
// on Intel Haswell 2x faster for fpc 32-Bit
begin
n := (n AND K55)+((n shr 1) AND K55);
n := (n AND K33)+((n shr 2) AND K33);
n := (n AND KF1)+((n shr 4) AND KF1);
n := (n AND KF2)+((n shr 8) AND KF2);
n := (n AND KF4)+((n shr 16) AND KF4);
n := (n AND KF8)+ (n shr 32);
result := n;
end;
 
function popcnt(n:Uint32):integer;overload;
var
c,b : NativeUint;
begin
b := n;
c := (b shr 1) AND NativeUint(K55); b := (b AND NativeUint(K55))+C;
c := ((b shr 2) AND NativeUint(K33));b := (b AND NativeUint(K33))+C;
c:= ((b shr 4) AND NativeUint(KF1)); b := (b AND NativeUint(KF1))+c;
c := ((b shr 8) AND NativeUint(KF2));b := (b AND NativeUint(KF2))+c;
c := b shr 16; b := (b AND NativeUint(KF4))+ C;
result := b;
end;
 
function popcnt(n:Uint16):integer;overload;
var
c,b : NativeUint;
begin
b := n;
c := (b shr 1) AND NativeUint(K55); b := (b AND NativeUint(K55))+C;
c :=((b shr 2) AND NativeUint(K33)); b := (b AND NativeUint(K33))+C;
c:= ((b shr 4) AND NativeUint(KF1)); b := (b AND NativeUint(KF1))+c;
c := b shr 8; b := (b AND NativeUint(KF2))+c;
result := b;
end;
 
function popcnt(n:Uint8):integer;overload;
var
c,b : NativeUint;
begin
b := n;
c := (b shr 1) AND NativeUint(K55); b := (b AND NativeUint(K55))+C;
c :=((b shr 2) AND NativeUint(K33));b := (b AND NativeUint(K33))+C;
c:= b shr 4;
result := (b AND NativeUint(KF1))+c;
end;
 
Begin
End.</lang>
The program
<lang pascal>program pcntTest;
uses
sysutils,popCount;
 
function Odious(n:Uint32):boolean;inline;
Begin
Odious := boolean(PopCnt(n) AND 1)
end;
 
function EvilNumber(n:Uint32):boolean;inline;
begin
EvilNumber := boolean(NOT(PopCnt(n)) AND 1);
end;
 
var
s : String;
i : Uint64;
k : LongWord;
Begin
s :='PopCnt 3^i :';
i:= 1;
For k := 1 to 30 do
Begin
s := s+InttoStr(PopCnt(i)) +' ';
i := 3*i;
end;
writeln(s);writeln;
 
s:='Evil numbers :';i := 0;k := 0;
repeat
IF EvilNumber(i) then
Begin
inc(k);s := s+InttoStr(i) +' ';
end;
inc(i);
until k = 30;
writeln(s);writeln;s:='';
 
 
s:='Odious numbers :';i := 0;k := 0;
repeat
IF Odious(i) then
Begin
inc(k);s := s+InttoStr(i) +' ';
end;
inc(i);
until k = 30;
writeln(s);
end.</lang>
;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
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|Perl}}==
Line 781 ⟶ 921:
use Bit::Vector;
say Bit::Vector->new_Dec(64,1234567)->Norm;</lang>
 
=={{header|Perl 6}}==
<lang perl6>sub population-count(Int $n where * >= 0) { [+] $n.base(2).comb }
Anonymous user