Pernicious numbers: Difference between revisions

m
Added Easylang
m (→‎{{header|Factor}}: show all vocabularies)
m (Added Easylang)
 
(47 intermediate revisions by 28 users not shown)
Line 2:
A   [[wp:Pernicious number|pernicious number]]   is a positive integer whose   [[population count]]   is a prime.
 
The   ''population count''   is the number of   ''ones''   in the binary representation of a non-negative integer.
 
 
;Example
'''22'''   (which is   '''10110'''   in binary)   has a population count of   '''3''',   which is prime, and therefore   '''22'''and   is atherefore pernicious number.
<br>'''22''' &nbsp; is a pernicious number.
 
 
Line 19 ⟶ 20:
* Rosetta Code entry &nbsp; [[Population_count|population count, evil numbers, odious numbers]].
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F is_prime(n)
I n < 2
R 0B
L(i) 2 .. Int(sqrt(n))
I n % i == 0
R 0B
R 1B
 
V i = 0
V cnt = 0
L
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)
cnt++
I cnt == 25
L.break
i++
 
print()
L(i) 888888877..888888888
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)</syntaxhighlight>
 
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|360 Assembly}}==
Line 24 ⟶ 55:
For maximum compatibility, this program uses only the basic instruction set (S/360)
with 2 ASSIST macros (XDECO,XPRNT).
<langsyntaxhighlight lang="360asm">* Pernicious numbers 04/05/2016
PERNIC CSECT
USING PERNIC,R13 base register and savearea pointer
Line 149 ⟶ 180:
XDEC DS CL12 edit zone
YREGS
END PERNIC</langsyntaxhighlight>
{{out}}
<pre>
Line 156 ⟶ 187:
</pre>
 
=={{header|AdaAction!}}==
Action! integers are limited to 16 bits, so this implements 32 bit addition and multiplication by 8-bit values to handle the larger numbers.
<syntaxhighlight lang="action!">
;;; find some pernicious numbers - numbers where the population count is prime
 
;;; As the task requires 32 bit integers, this implements 32-bit unsigend
;;; integer addition and multiplication by an 8-bit integer.
;;; The 32-bit values are stored in 4 separate bytes
 
 
;;; returns the population (number of bits on) of the non-negative integer n
BYTE FUNC population( CARD n )
CARD number
BYTE result
number = n
result = 0;
WHILE number > 0 DO
IF number AND 1 THEN result ==+ 1 FI
number ==/ 2
OD
RETURN( result )
 
;;; returns TRUE if n is a prime; n must be <= 32
BYTE FUNC isSmallPrime( BYTE n )
BYTE result
IF n = 2 THEN result = 1
ELSEIF ( n AND 1 ) = 0 THEN result = 0
ELSEIF n = 1 OR n = 9 OR n = 15
OR n = 21 OR n = 25 OR n = 27
THEN result = 0
ELSE result = 1
FI
RETURN( result )
 
;;; returns TRUE if n is pernicious, FALSE otherwise
BYTE FUNC isPernicious( CARD n ) RETURN( isSmallPrime( population( n ) ) )
 
;;; returns TRUE if the 32 bit integer in i1, i2, i3, i4 is pernicious,
;;; FALSE otherwise
BYTE FUNC isPernicious32( BYTE i1, i2, i3, i4 )
BYTE p
p = population( i1 ) + population( i2 )
+ population( i3 ) + population( i4 )
RETURN( isSmallPrime( p ) )
 
;;; adds b to the 32 bit unsigned integer in i1, i2, i3 and i4
PROC i32add8( BYTE POINTER i1, i2, i3, i4, BYTE b )
CARD c1, c2, c3, c4
 
c1 = i1^ c2 = i2^ c3 = i3^ c4 = i4^
c4 ==+ b
i4 ^= c4 MOD 256
c3 ==+ c4 / 256
i3 ^= c3 MOD 256
c2 ==+ c3 / 256
i2 ^= c2 MOD 256
c1 ==+ c2 / 256
i1 ^= c1 MOD 256
 
RETURN
;;; multiplies the 32 bit unsigned integer in i1, i2, i3 and i4 by b
PROC i32mul8( BYTE POINTER i1, i2, i3, i4, BYTE b )
CARD c1, c2, c3, c4, r
 
c1 = i1^ c2 = i2^ c3 = i3^ c4 = i4^
 
r = c4 * b
i4 ^= r MOD 256
r = ( c3 * b ) + ( r / 256 )
i3 ^= r MOD 256
r = ( c2 * b ) + ( r / 256 )
i2 ^= r MOD 256
r = ( c1 * b ) + ( r / 256 )
i1 ^= r MOD 256
 
RETURN
;;; find the first 25 pernicious numbers
PROC Main()
BYTE perniciousCount, i
BYTE i81, i82, i83, i84
BYTE p81, p82, p83, p84
 
perniciousCount = 0
i = 0
WHILE perniciousCount < 25 DO
IF isPernicious( i ) THEN
; found a pernicious number
PrintB( i )Put(' )
perniciousCount ==+ 1
FI
i ==+ 1
OD
PutE()
 
; find the pernicious numbers between 888 888 877 and 888 888 888
 
; form 888 888 800 in i81, i82, i83 and i84
i81 = 0 i82 = 0 i83 = 0 i84 = 88 ; 88
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 8 800
i32add8( @i81, @i82, @i83, @i84, 88 ) ; 8 888
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 888 800
i32add8( @i81, @i82, @i83, @i84, 88 ) ; 888 888
i32mul8( @i81, @i82, @i83, @i84, 10 ) ; 8 888 880
i32add8( @i81, @i82, @i83, @i84, 8 ) ; 8 888 888
i32mul8( @i81, @i82, @i83, @i84, 100 ) ; 888 888 800
 
FOR i = 77 TO 88 DO
p81 = i81 p82 = i82 p83 = i83 p84 = i84
i32add8( @p81, @p82, @p83, @p84, i )
IF isPernicious32( p81, p82, p83, p84 )
THEN
print( "8888888" )PrintB( i )Put(' )
FI
OD
PutE()
RETURN
</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|Ada}}==
Uses package Population_Count from [[Population count#Ada]].
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Population_Count; use Population_Count;
 
procedure Pernicious is
Line 199 ⟶ 354:
end loop;
Ada.Text_IO.New_Line;
end;</langsyntaxhighlight>
 
 
Line 209 ⟶ 364:
A small modification allows to count all the pernicious numbers between 1 and 2**32 in about 32 seconds:
 
<langsyntaxhighlight Adalang="ada"> Counter: Natural;
begin
-- initialize array Prime; Prime(I) must be true if and only if I is a prime
Line 222 ⟶ 377:
end loop;
Ada.Text_IO.Put_Line(Natural'Image(Counter));
end Count_Pernicious;</langsyntaxhighlight>
 
{{out}}
Line 234 ⟶ 389:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># calculate various pernicious numbers #
 
# returns the population (number of bits on) of the non-negative integer n #
Line 281 ⟶ 436:
OD;
print( ( newline ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 287 ⟶ 442:
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % find some pernicious numbers: numbers with a prime population count %
% returns the population count of n %
integer procedure populationCount( integer value n ) ;
begin
integer v, count;
count := 0;
v := abs n;
while v > 0 do begin
if odd( v ) then count := count + 1;
v := v div 2
end while_v_gt_0 ;
count
end populationCount ;
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
begin
p( 1 ) := false; p( 2 ) := true;
for i := 3 step 2 until n do p( i ) := true;
for i := 4 step 2 until n do p( i ) := false;
for i := 3 step 2 until truncate( sqrt( n ) ) do begin
integer ii; ii := i + i;
if p( i ) then for pr := i * i step ii until n do p( pr ) := false
end for_i ;
end Eratosthenes ;
% returns true if p is pernicious, false otherwise, s must be a sieve %
% of primes upto 32 %
logical procedure isPernicious ( integer value p; logical array s ( * ) ) ; p > 0 and s( populationCount( p ) );
% find the pernicious numbers %
begin
% as we are dealing with 32 bit numbers, the maximum possible %
% population is 32 %
logical array isPrime ( 1 :: 32 );
integer p, pCount;
Eratosthenes( isPrime, 32 );
% show the first 25 pernicious numbers %
pCount := 0;
p := 2; % 0 and 1 aren't pernicious, so start at 2 %
while pCount < 25 do begin
if isPernicious( p, isPrime ) then begin
% have a pernicious number %
pCount := pCount + 1;
writeon( i_w := 1, s_w := 0, " ", p )
end if_pernicious_p ;
p := P + 1
end for_p ;
write();
% find the pernicious numbers between 888 888 877 and 888 888 888 %
for p := 888888877 until 888888888 do begin
if isPernicious( p, isPrime ) then writeon( i_w := 1, s_w := 0, " ", p )
end for_p ;
write();
end
end.</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on isPrime(n)
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
repeat with i from 5 to (n ^ 0.5) div 1 by 6
if ((n mod i is 0) or (n mod (i + 2) is 0)) then return false
end repeat
return true
end isPrime
 
on isPernicious(n)
-- 8 bits at a time is statistically slightly more efficient than 1 bit at a time.
set popCount to (n mod 4 + 1) div 2 + (n mod 16 + 4) div 8
set n to n div 16
repeat until (n = 0)
set popCount to popCount + (n mod 4 + 1) div 2 + (n mod 16 + 4) div 8
set n to n div 16
end repeat
return isPrime(popCount)
end isPernicious
 
-- Task code:
on intToText(n)
set output to ""
repeat until (n < 100000000)
set output to text 2 thru 9 of ((100000000 + (n mod 100000000 as integer)) as text) & output
set n to n div 100000000
end repeat
set output to (n as integer as text) & output
return output
end intToText
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set output to lst as text
set AppleScript's text item delimiters to astid
return output
end join
 
on task()
set l1 to {}
set n to 0
set counter to 0
repeat until (counter = 25)
if (isPernicious(n)) then
set end of l1 to n
set counter to counter + 1
end if
set n to n + 1
end repeat
set l2 to {}
-- One solution to 8,888,877 and up being too large to be AppleScript repeat indices.
repeat with i from 88888877 to 88888888
set n to 8.0E+8 + i
if (isPernicious(n)) then set end of l2 to intToText(n)
end repeat
return join(l1, " ") & (linefeed & join(l2, " "))
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886"</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pernicious?: function [n][
prime? size filter split as.binary n 'x -> x="0"
]
 
i: 1
found: 0
while [found<25][
if pernicious? i [
prints i
prints " "
found: found + 1
]
i: i + 1
]
print ""
print select 888888877..888888888 => pernicious?</syntaxhighlight>
 
{{out}}
 
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">c := 0
while c < 25
if IsPern(A_Index)
Line 305 ⟶ 615:
, x := (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
return p[(x * 0x0101010101010101) >> 56]
}</langsyntaxhighlight>
{{Out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886 </pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PERNICIOUS_NUMBERS.AWK
BEGIN {
Line 365 ⟶ 676:
return gsub(/1/,"&",n)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 371 ⟶ 682:
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">n = 1
cont = 0
print "The following are the first 25 pernicious numbers:"
print
 
do
if isPernicious(n) then
print rjust(string(n), 3);
cont += 1
end if
n += 1
until cont = 25
 
print : print
print "The pernicious numbers between 888,888,877 and 888,888,888 inclusive are:"
print
for n = 888888877 to 888888888
if isPernicious(n) then print rjust(string(n), 10);
next n
end
 
function SumBinaryDigits(number)
if number < 0 then number = -number # convert negative numbers to positive
sum = 0
while number > 0
sum += number mod 2
number /= 2
end while
return sum
end function
 
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
 
function isPernicious(number)
popcont = SumBinaryDigits(number)
return isPrime(popcont)
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim n As Integer = 1, count As Integer = 0
 
Print "The following are the first 25 pernicious numbers:\n"
Do
If isPernicious(n) Then
Print Format$(n, "###");
count += 1
End If
n += 1
Loop Until count = 25
Print "\n\nThe pernicious numbers between 888,888,877 and 888,888,888 inclusive are:\n"
For n = 888888877 To 888888888
If isPernicious(n) Then Print Format$(n, "##########");
Next
Print
 
End
 
Public Sub isPrime(ValorEval As Long) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Long = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function
 
Public Function SumBinaryDigits(number As Integer) As Integer
If number < 0 Then number = -number ' convert negative numbers to positive
Dim sum As Integer = 0
While number > 0
sum += number Mod 2
number \= 2
Wend
Return sum
End Function
 
Public Function isPernicious(number As Integer) As Boolean
Dim popCount As Integer = SumBinaryDigits(number)
 
Return isPrime(popCount)
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">n = 1
cont = 0
print "The following are the first 25 pernicious numbers:\n"
repeat
if isPernicious(n) then
print n using ("###");
cont = cont + 1
fi
n = n + 1
until cont = 25
 
print "\n\nThe pernicious numbers between 888,888,877 and 888,888,888 inclusive are:\n"
for n = 888888877 to 888888888
if isPernicious(n) print n using("##########");
next n
print
end
sub SumBinaryDigits(number)
if number < 0 number = -number // convert negative numbers to positive
sum = 0
while number > 0
sum = sum + mod(number, 2)
number = int(number / 2)
wend
return sum
end sub
 
sub isPrime(v)
if v < 2 return False
if mod(v, 2) = 0 return v = 2
if mod(v, 3) = 0 return v = 3
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
 
sub isPernicious(number)
popcont = SumBinaryDigits(number)
return isPrime(popcont)
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Befunge}}==
Line 377 ⟶ 844:
Also note that the extra spaces in the output are just to ensure it's readable on buggy interpreters that don't include a space after numeric output. They can easily be removed by replacing the comma on line 3 with a dollar.
 
<langsyntaxhighlight lang="befunge">55*00p1>:"ZOA>/"***7-*>\:2>/\v
>8**`!#^_$@\<(^v^)>/#2^#\<2 2
^+**"X^yYo":+1<_:.48*,00v|: <%
v".D}Tx"$,+55_^#!p00:-1g<v |<
> * + : * * + ^^ ! % 2 $ <^ <^</langsyntaxhighlight>
 
{{out}}
Line 388 ⟶ 855:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
typedef unsigned uint;
Line 412 ⟶ 879:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|C++}}==
<lang cpp>
#include <iostream>
#include <algorithm>
#include <bitset>
 
using namespace std;
 
class pernNumber
{
public:
void displayFirst( unsigned cnt )
{
unsigned pn = 3;
while( cnt )
{
if( isPernNumber( pn ) )
{
cout << pn << " "; cnt--;
}
pn++;
}
}
void displayFromTo( unsigned a, unsigned b )
{
for( unsigned p = a; p <= b; p++ )
if( isPernNumber( p ) )
cout << p << " ";
}
 
private:
bool isPernNumber( unsigned p )
{
string bin = bitset<64>( p ).to_string();
unsigned c = count( bin.begin(), bin.end(), '1' );
return isPrime( c );
}
bool isPrime( unsigned p )
{
if( p == 2 ) return true;
if( p < 2 || !( p % 2 ) ) return false;
for( unsigned x = 3; ( x * x ) <= p; x += 2 )
if( !( p % x ) ) return false;
return true;
}
};
int main( int argc, char* argv[] )
{
pernNumber p;
p.displayFirst( 25 ); cout << endl;
p.displayFromTo( 888888877, 888888888 ); cout << endl;
return 0;
}
</lang>
{{out}}
<pre>
Line 480 ⟶ 887:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 542 ⟶ 949:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <iostream>
using namespace std;
 
int main() {
int cnt = 0, cnt2, cnt3, tmp, binary[8];
for (int i = 3; cnt < 25; i++) {
tmp = i;
cnt2 = 0;
cnt3 = 0;
for (int j = 7; j > 0; j--) {
binary[j] = tmp % 2;
tmp /= 2;
}
binary[0] = tmp;
for (int j = 0; j < 8; j++) {
if (binary[j] == 1) {
cnt2++;
}
}
for (int j = 2; j <= (cnt2 / 2); j++) {
if (cnt2 % j == 0) {
cnt3++;
break;
}
}
if (cnt3 == 0 && cnt2 != 1) {
cout << i << endl;
cnt++;
}
}
 
cout << endl;
int binary2[31];
 
for (int i = 888888877; i <= 888888888; i++) {
tmp = i;
cnt2 = 0;
cnt3 = 0;
for (int j = 30; j > 0; j--) {
binary2[j] = tmp % 2;
tmp /= 2;
}
binary2[0] = tmp;
for (int j = 0; j < 31; j++) {
if (binary2[j] == 1) {
cnt2++;
}
}
for (int j = 2; j <= (cnt2 / 2); j++) {
if (cnt2 % j == 0) {
cnt3++;
break;
}
}
if (cnt3 == 0 && cnt2 != 1) {
cout << i << endl;
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 550 ⟶ 1,025:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn counting-numbers
([] (counting-numbers 1))
([n] (lazy-seq (cons n (counting-numbers (inc n))))))
Line 558 ⟶ 1,033:
(prime? (count (filter #(= % \1) (Integer/toString n 2)))))
(println (take 25 (filter pernicious? (counting-numbers))))
(println (filter pernicious? (range 888888877 888888889)))</langsyntaxhighlight>
{{Output}}
<pre>(3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36)
(888888877 888888878 888888880 888888883 888888885 888888886)</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% The population count of an integer is never going to be
% higher than the amount of bits in it (max 64)
% so we can get away with a very simple primality test.
is_prime = proc (n: int) returns (bool)
if n<=2 then return(n=2) end
if n//2=0 then return(false) end
for i: int in int$from_to_by(n+2, n/2, 2) do
if n//i=0 then return(false) end
end
return(true)
end is_prime
 
% Find the population count of a number
pop_count = proc (n: int) returns (int)
c: int := 0
while n > 0 do
c := c + n // 2
n := n / 2
end
return(c)
end pop_count
 
% Is N pernicious?
pernicious = proc (n: int) returns (bool)
return(is_prime(pop_count(n)))
end pernicious
 
start_up = proc ()
po: stream := stream$primary_output()
stream$putl(po, "First 25 pernicious numbers: ")
n: int := 1
seen: int := 0
while seen<25 do
if pernicious(n) then
stream$puts(po, int$unparse(n) || " ")
seen := seen + 1
end
n := n + 1
end
stream$putl(po, "\nPernicious numbers between 888,888,877 and 888,888,888:")
for i: int in int$from_to(888888877,888888888) do
if pernicious(i) then
stream$puts(po, int$unparse(i) || " ")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre>First 25 pernicious numbers:
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Pernicious numbers between 888,888,877 and 888,888,888:
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PERNICIOUS-NUMBERS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 AMOUNT PIC 99.
03 CAND PIC 9(9).
03 POPCOUNT PIC 99.
03 POP-N PIC 9(9).
03 FILLER REDEFINES POP-N.
05 FILLER PIC 9(8).
05 FILLER PIC 9.
88 ODD VALUES 1, 3, 5, 7, 9.
03 DSOR PIC 99.
03 DIV-RSLT PIC 99V99.
03 FILLER REDEFINES DIV-RSLT.
05 FILLER PIC 99.
05 FILLER PIC 99.
88 DIVISIBLE VALUE ZERO.
03 PRIME-FLAG PIC X.
88 PRIME VALUE '*'.
01 FORMAT.
03 SIZE-FLAG PIC X.
88 SMALL VALUE 'S'.
88 LARGE VALUE 'L'.
03 SMALL-NUM PIC ZZ9.
03 LARGE-NUM PIC Z(9)9.
03 OUT-STR PIC X(80).
03 OUT-PTR PIC 99.
PROCEDURE DIVISION.
BEGIN.
PERFORM SMALL-PERNICIOUS.
PERFORM LARGE-PERNICIOUS.
STOP RUN.
INIT-OUTPUT-VARS.
MOVE ZERO TO AMOUNT.
MOVE 1 TO OUT-PTR.
MOVE SPACES TO OUT-STR.
SMALL-PERNICIOUS.
PERFORM INIT-OUTPUT-VARS.
MOVE 'S' TO SIZE-FLAG.
PERFORM ADD-PERNICIOUS
VARYING CAND FROM 1 BY 1 UNTIL AMOUNT IS EQUAL TO 25.
DISPLAY OUT-STR.
 
LARGE-PERNICIOUS.
PERFORM INIT-OUTPUT-VARS.
MOVE 'L' TO SIZE-FLAG.
PERFORM ADD-PERNICIOUS
VARYING CAND FROM 888888877 BY 1
UNTIL CAND IS GREATER THAN 888888888.
DISPLAY OUT-STR.
ADD-NUM.
ADD 1 TO AMOUNT.
IF SMALL,
MOVE CAND TO SMALL-NUM,
STRING SMALL-NUM DELIMITED BY SIZE INTO OUT-STR
WITH POINTER OUT-PTR.
IF LARGE,
MOVE CAND TO LARGE-NUM,
STRING LARGE-NUM DELIMITED BY SIZE INTO OUT-STR
WITH POINTER OUT-PTR.
ADD-PERNICIOUS.
PERFORM FIND-POPCOUNT.
PERFORM CHECK-PRIME.
IF PRIME, PERFORM ADD-NUM.
FIND-POPCOUNT.
MOVE ZERO TO POPCOUNT.
MOVE CAND TO POP-N.
PERFORM COUNT-BIT UNTIL POP-N IS EQUAL TO ZERO.
COUNT-BIT.
IF ODD, ADD 1 TO POPCOUNT.
DIVIDE 2 INTO POP-N.
CHECK-PRIME.
IF POPCOUNT IS LESS THAN 2,
MOVE SPACE TO PRIME-FLAG
ELSE
MOVE '*' TO PRIME-FLAG.
PERFORM CHECK-DSOR VARYING DSOR FROM 2 BY 1
UNTIL NOT PRIME OR DSOR IS NOT LESS THAN POPCOUNT.
 
CHECK-DSOR.
DIVIDE POPCOUNT BY DSOR GIVING DIV-RSLT.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</syntaxhighlight>
{{out}}
<pre> 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|Common Lisp}}==
Using <code>primep</code> from [[Primality_by_trial_division#Common_Lisp|Primality by trial division]] task.
 
<langsyntaxhighlight lang="lisp">(format T "~{~a ~}~%"
(loop for n = 1 then (1+ n)
when (primep (logcount n))
Line 576 ⟶ 1,205:
(loop for n from 888888877 to 888888888
when (primep (logcount n))
collect n))</langsyntaxhighlight>
 
{{Out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub prime(n: uint8): (r: uint8) is
if n<2 then
r := 0;
return;
end if;
r := 1;
var d: uint8 := 2;
while d*d <= n loop
if n%d == 0 then
r := 0;
return;
end if;
d := d + 1;
end loop;
end sub;
 
sub popcount(n: uint32): (count: uint8) is
count := 0;
while n > 0 loop
count := count + (n as uint8 & 1);
n := n >> 1;
end loop;
end sub;
 
sub pernicious(n: uint32): (r: uint8) is
r := prime(popcount(n));
end sub;
 
var candidate: uint32 := 0;
var seen: uint8 := 0;
 
while seen < 25 loop
candidate := candidate + 1;
if pernicious(candidate) != 0 then
print_i32(candidate);
print_char(' ');
seen := seen + 1;
end if;
end loop;
print_nl();
 
candidate := 888888877;
while candidate < 888888888 loop
if pernicious(candidate) != 0 then
print_i32(candidate);
print_char(' ');
end if;
candidate := candidate + 1;
end loop;
print_nl();</syntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, core.bitop;
 
Line 589 ⟶ 1,276:
uint.max.iota.filter!pernicious.take(25).writeln;
iota(888_888_877, 888_888_889).filter!pernicious.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
Line 597 ⟶ 1,284:
This high-level code is fast enough to allow to count all the
1_421_120_880 Pernicious numbers in the unsigned 32 bit range in less than 48 seconds with this line:
<langsyntaxhighlight lang="d">uint.max.iota.filter!pernicious.walkLength.writeln;</langsyntaxhighlight>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func popc n .
while n > 0
r += n mod 2
n = n div 2
.
return r
.
n = 1
while cnt < 25
if isprim popc n = 1
write n & " "
cnt += 1
.
n += 1
.
print ""
n = 1
for n = 888888877 to 888888888
if isprim popc n = 1
write n & " "
.
.
</syntaxhighlight>
 
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'sequences)
 
Line 611 ⟶ 1,344:
(take (filter pernicious? [888888877 .. 888888889]) #:all)
→ (888888877 888888878 888888880 888888883 888888885 888888886)
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 715 ⟶ 1,448:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 723 ⟶ 1,456:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
<lang Elixir>
defmodule SieveofEratosthenes do
def init(lim) do
Line 765 ⟶ 1,498:
def pernicious?(n,primes), do: Enum.member?(primes,ones(n))
end
</syntaxhighlight>
</lang>
 
<syntaxhighlight lang="elixir">
<lang Elixir>
PerniciousNumbers.take(25)
PerniciousNumbers.between(888_888_877..888_888_888)
</syntaxhighlight>
</lang>
 
{{out}}
Line 777 ⟶ 1,510:
 
=={{header|F#|F sharp}}==
<langsyntaxhighlight lang="fsharp">open System
 
//Taken from https://gist.github.com/rmunn/bc49d32a586cdfa5bcab1c3e7b45d7ac
Line 798 ⟶ 1,531:
[1 .. 100] |> Seq.filter (bitcount >> isPrime) |> Seq.take 25 |> Seq.toList |> printfn "%A"
[888888877 .. 888888888] |> Seq.filter (bitcount >> isPrime) |> Seq.toList |> printfn "%A"
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>[3; 5; 6; 7; 9; 10; 11; 12; 13; 14; 17; 18; 19; 20; 21; 22; 24; 25; 26; 28; 31; 33; 34; 35; 36]
Line 804 ⟶ 1,537:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: lists lists.lazy math.bitwise math.primes math.ranges
prettyprint sequences ;
 
Line 810 ⟶ 1,543:
 
25 0 lfrom [ pernicious? ] lfilter ltake list>array . ! print first 25 pernicious numbers
888,888,877 888,888,888 [a,b] [ pernicious? ] filter . ! print pernicious numbers in range</langsyntaxhighlight>
{{out}}
<pre>
{ 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 }
{ 888888877 888888878 888888880 888888883 888888885 888888886 }
</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">: popcount { n -- u }
0
begin
n 0<>
while
n n 1- and to n
1+
repeat ;
 
\ primality test for 0 <= n <= 63
: prime? ( n -- ? )
1 swap lshift 0x28208a20a08a28ac and 0<> ;
 
: pernicious? ( n -- ? )
popcount prime? ;
 
: first_n_pernicious_numbers { n -- }
." First " n . ." pernicious numbers:" cr
1
begin
n 0 >
while
dup pernicious? if
dup .
n 1- to n
then
1+
repeat
drop cr ;
 
: pernicious_numbers_between { m n -- }
." Pernicious numbers between " m . ." and " n 1 .r ." :" cr
n 1+ m do
i pernicious? if i . then
loop
cr ;
 
25 first_n_pernicious_numbers
888888877 888888888 pernicious_numbers_between
 
bye</syntaxhighlight>
 
{{out}}
<pre>
First 25 pernicious numbers:
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Pernicious numbers between 888888877 and 888888888:
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program pernicious
implicit none
 
Line 873 ⟶ 1,658:
end if
end function
end program</langsyntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Line 880 ⟶ 1,665:
=={{header|FreeBASIC}}==
{{trans|PureBasic}}
<langsyntaxhighlight lang="freebasic">
' FreeBASIC v1.05.0 win64
 
Line 938 ⟶ 1,723:
Sleep
End
</syntaxhighlight>
</lang>
 
{{out}}
Line 950 ⟶ 1,735:
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">isPernicious = {|x|
bits = countToDict[integerDigits[x,2]].get[1,0]
return bits > 1 and isPrime[bits]
}
 
println["First 25: " + first[select[count[1], isPernicious], 25]]
println[select[888_888_877 to 888_888_888, isPernicious]]</syntaxhighlight>
{{out}}
<pre>
First 25: [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Pernicious_numbers}}
 
'''Solution'''
 
[[File:Fōrmulæ - Pernicious numbers 01.png]]
 
'''Case 1. Display the first 25 pernicious numbers (in decimal)'''
 
[[File:Fōrmulæ - Pernicious numbers 02.png]]
 
[[File:Fōrmulæ - Pernicious numbers 03.png]]
 
'''Case 2. display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive).'''
 
[[File:Fōrmulæ - Pernicious numbers 04.png]]
 
[[File:Fōrmulæ - Pernicious numbers 05.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 984 ⟶ 1,803:
}
fmt.Println()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 992 ⟶ 1,811:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
class example{
static void main(String[] args){
Line 1,017 ⟶ 1,836:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,025 ⟶ 1,844:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">module Pernicious
where
 
Line 1,041 ⟶ 1,860:
solution1 = take 25 $ filter isPernicious [1 ..]
solution2 = filter isPernicious [888888877 .. 888888888]</langsyntaxhighlight>
{{output}}
<pre>[3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36]
Line 1,048 ⟶ 1,867:
Or, in a point-free and applicative style, using unfoldr for the population count:
 
<langsyntaxhighlight Haskelllang="haskell">import Data.Numbers.Primes (isPrime)
import Data.List (unfoldr)
import Data.Tuple (swap)
Line 1,066 ⟶ 1,885:
[ take 25 $ filter isPernicious [1 ..]
, filter isPernicious [888888877 .. 888888888]
]</langsyntaxhighlight>
{{Out}}
<pre>[3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36]
Line 1,072 ⟶ 1,891:
 
=={{header|Icon}} and {{header|Unicon}}==
 
Works in both languages:
<langsyntaxhighlight lang="unicon">link "factors"
 
procedure main(A)
Line 1,089 ⟶ 1,907:
while n > 0 do c +:= 1(n%2, n/:=2)
return c
end</langsyntaxhighlight>
 
{{Out}}
Line 1,100 ⟶ 1,918:
 
=={{header|J}}==
 
Implementation:
 
<langsyntaxhighlight Jlang="j">ispernicious=: 1 p: +/"1@#:</langsyntaxhighlight>
 
Task (thru taken from the [[Loops/Downward_for#J|Loops/Downward for]] task).:
 
<langsyntaxhighlight Jlang="j"> 25{.I.ispernicious i.100
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
 
thru=: <. + i.@(+*)@-~
888888877 + I.(#~ ispernicious) 888888877 thru 888888888
888888877 888888878 888888880 888888883 888888885 888888886</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class Pernicious{
//very simple isPrime since x will be <= Long.SIZE
public static boolean isPrime(int x){
Line 1,143 ⟶ 1,960:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886 </pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
The most interesting detail in the following is perhaps the use of ''recurse/1'' to define the helper function ''bin'', which generates the binary bits.
<langsyntaxhighlight lang="jq"># is_prime is designed to work with jq 1.4
def is_prime:
if . == 2 then true
Line 1,186 ⟶ 2,004:
;
 
task</langsyntaxhighlight>
{{Out}}
[3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36]
Line 1,193 ⟶ 2,011:
=={{header|Julia}}==
{{works with|Julia|0.6}}
<syntaxhighlight lang="julia">using Primes
 
<lang julia>using Primes
 
ispernicious(n::Integer) = isprime(count_ones(n))
Line 1,209 ⟶ 2,026:
 
println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", ")) </langsyntaxhighlight>
 
{{out}}
Line 1,216 ⟶ 2,033:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun isPrime(n: Int): Boolean {
Line 1,262 ⟶ 2,079:
if (isPernicious(i)) print("$i ")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,273 ⟶ 2,090:
 
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Positive integer whose population count is a prime
 
# # Variables:
#
integer PNUM=25 MINN=888888877 MAXN=888888888
 
# # Functions:
#
# # Function _dec2bin(n) - return binary representation of decimal n
#
function _dec2bin {
typeset _n ; integer _n=$1
typeset _base ; integer _base=2
typeset _q _r _buff ; integer _q _r
typeset -a _arr _barr
 
(( _q = _n / _base ))
(( _r = _n % _base ))
_arr+=( ${_r} )
until (( _q == 0 )); do
_n=${_q}
(( _q = _n / _base ))
(( _r = _n % _base ))
_arr+=( ${_r} )
done
_revarr _arr _barr
_buff=${_barr[@]}
echo ${_buff// /}
}
 
# # Function _revarr(arr, barr) - reverse arr into barr
#
function _revarr {
typeset _arr ; nameref _arr="$1"
typeset _barr ; nameref _barr="$2"
typeset _i ; integer _i
 
for ((_i=${#_arr[*]}-1; _i>=0; _i--)); do
_barr+=( ${_arr[_i]} )
done
}
 
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
 
# # Function _sumdigits(n) return sum of n's digits
#
function _sumdigits {
typeset _n ; _n=$1
typeset _i _sum ; integer _i _sum=0
 
for ((_i=0; _i<${#_n}; _i++)); do
(( _sum+=${_n:${_i}:1} ))
done
echo ${_sum}
}
 
######
# main #
######
 
integer i sbi n=3 cnt=0
 
printf "First $PNUM Pernicious numbers:\n"
for ((n = cnt = 0; cnt < PNUM; n++)); do
bi=$(_dec2bin ${n}) # $n as Binary
sbi=${bi//0/} # Strip zeros (i.e. count ones)
_isprime ${#sbi} # One count prime?
(( $? )) && { printf "%4d " ${n} ; ((++cnt)) }
done
 
printf "\n\nPernicious numbers between %11,d and %11,d inclusive:\n" $MINN $MAXN
for ((i=MINN; i<=MAXN; i++)); do
bi=$(_dec2bin ${i})
sbi=${bi//0/}
_isprime ${#sbi}
(( $? )) && printf "%12,d " ${i}
done
echo
</syntaxhighlight>
{{out}}<pre>
First 25 Pernicious numbers:
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
 
Pernicious numbers between 888,888,877 and 888,888,888 inclusive:
888,888,877 888,888,878 888,888,880 888,888,883 888,888,885 888,888,886
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Test primality by trial division
function isPrime (x)
if x < 2 then return false end
Line 1,328 ⟶ 2,247:
-- Main procedure
pernicious(25)
pernicious(888888877, 888888888)</langsyntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Line 1,334 ⟶ 2,253:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">ispernicious := proc(n::posint)
return evalb(isprime(rhs(Statistics:-Tally(StringTools:-Explode(convert(convert(n, binary), string)))[-1])));
end proc;
Line 1,360 ⟶ 2,279:
end do;
return list_num;
end proc:</langsyntaxhighlight>
{{out}}
<pre>[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
Line 1,366 ⟶ 2,285:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">popcount[n_Integer] := IntegerDigits[n, 2] // Total
perniciousQ[n_Integer] := popcount[n] // PrimeQ
perniciouscount = 0;
Line 1,383 ⟶ 2,302:
, {i, 888888877, 888888888}]
Print["Pernicious numbers between 888,888,877 and 888,888,888 (inclusive)"]
perniciouslist2</langsyntaxhighlight>
{{out}}
<pre>first 25 pernicious numbers
Line 1,392 ⟶ 2,311:
===Alternate Code===
test function
<langsyntaxhighlight Mathematicalang="mathematica">perniciousQ[n_Integer] := PrimeQ@Total@IntegerDigits[n, 2]</langsyntaxhighlight>
First 25 pernicious numbers
<langsyntaxhighlight Mathematicalang="mathematica">n = 0; NestWhile[Flatten@{#, If[perniciousQ[++n], n, {}]} &, {}, Length@# < 25 &]</langsyntaxhighlight>
Pernicious numbers betweeen 888888877 and 888888888 inclusive
<langsyntaxhighlight Mathematicalang="mathematica">Cases[Range[888888877, 888888888], _?(perniciousQ@# &)]</langsyntaxhighlight>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show [first25, large]))]
 
first25 :: [num]
first25 = take 25 (filter pernicious [1..])
 
large :: [num]
large = filter pernicious [888888877..888888888]
 
pernicious :: num->bool
pernicious = prime . popcount
 
popcount :: num->num
popcount 0 = 0
popcount n = n mod 2 + popcount (n div 2)
 
prime :: num->bool
prime n = n >= 2 & and [n mod d ~= 0 | d<-[2..sqrt n]]
</syntaxhighlight>
{{out}}
<pre>[3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36]
[888888877,888888878,888888880,888888883,888888885,888888886]</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Pernicious;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,448 ⟶ 2,391:
 
ReadChar
END Pernicious.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import strutils
 
proc count(s: string,; sub: char): int =
var i = 0
while true:
Line 1,463 ⟶ 2,406:
inc result
 
proc popcount(n: int): int = n.toBin(64).count('1')
 
const primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61}
 
var p: = newSeqseq[int]()
var i = 0
while p.len < 25:
Line 1,481 ⟶ 2,424:
inc i
 
echo p</langsyntaxhighlight>
{{Out}}
<pre>@[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
@[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre>
 
=={{header|PARI/GPOCaml}}==
<syntaxhighlight lang="ocaml">let rec popcount n =
<lang parigp>pern(n)=isprime(hammingweight(n))
if n = 0 then 0 else succ (popcount (n land pred n))
select(pern, [1..36])
 
select(pern,[888888877..888888888])</lang>
let is_prime n =
let rec test d = d * d > n || n mod d <> 0 && test (d + 2) in
if n < 3 then n = 2 else n land 1 <> 0 && test 3
 
let is_pernicious n =
is_prime (popcount n)
 
let () =
Seq.ints 0 |> Seq.filter is_pernicious |> Seq.take 25
|> Seq.iter (Printf.printf " %u") |> print_newline
and () =
Seq.ints 888888877 |> Seq.take 12 |> Seq.filter is_pernicious
|> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{out}}
<pre>%1 = [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
%2 = [ 888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre>
 
=={{header|Panda}}==
<langsyntaxhighlight lang="panda">fun prime(a) type integer->integer
a where count{{a.factor}}==2
fun pernisc(a) type integer->integer
Line 1,501 ⟶ 2,457:
 
1..36.pernisc
888888877..888888888.pernisc</langsyntaxhighlight>
 
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">pern(n)=isprime(hammingweight(n))
select(pern, [1..36])
select(pern,[888888877..888888888])</syntaxhighlight>
{{out}}
<pre>%1 = [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
%2 = [888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre>
 
=={{header|Pascal}}==
Line 1,512 ⟶ 2,476:
 
Added easy counting of pernicious numbers for full Bit ranges like 32-Bit
<langsyntaxhighlight lang="pascal">program pernicious;
{$IFDEF FPC}
{$OPTIMIZATION ON,Regvar,ASMCSE,CSE,PEEPHOLE}// 3x speed up
Line 1,595 ⟶ 2,559:
inc(k,k);
until k>64;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,610 ⟶ 2,574:
=={{header|Perl}}==
{{trans|C}}
<langsyntaxhighlight lang="perl">sub is_pernicious {
my $n = shift;
my $c = 2693408940; # primes < 32 as set bits
Line 1,631 ⟶ 2,595:
}
 
print join ' ', @p;</langsyntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Line 1,638 ⟶ 2,602:
Alternately, generating the same output using a method similar to Pari/GP:
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/is_prime hammingweight/;
my $i = 1;
my @pern = map { $i++ while !is_prime(hammingweight($i)); $i++; } 1..25;
print "@pern\n";
print join(" ", grep { is_prime(hammingweight($_)) } 888888877 .. 888888888), "\n";</langsyntaxhighlight>
 
=={{header|Perl 6}}==
Straightforward implementation using Perl 6's ''is-prime'' built-in subroutine.
<lang perl6>sub is-pernicious(Int $n --> Bool) {
is-prime [+] $n.base(2).comb;
}
 
say (grep &is-pernicious, 0 .. *)[^25];
say grep &is-pernicious, 888_888_877 .. 888_888_888;</lang>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function is_prime(atom n)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
if n<2 then return false end if
<span style="color: #008080;">function</span> <span style="color: #000000;">pernicious</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
for i=2 to floor(sqrt(n)) do
<span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)))</span>
if mod(n,i)=0 then return false end if
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end for
return true
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
end function
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
 
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
function pernicious(integer n)
<span style="color: #008080;">if</span> <span style="color: #000000;">pernicious</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
return is_prime(sum(int_to_bits(n,32)))
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
sequence s = {}
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
integer n = 1
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
while length(s)<25 do
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
if pernicious(n) then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">888_888_877</span> <span style="color: #008080;">to</span> <span style="color: #000000;">888_888_888</span> <span style="color: #008080;">do</span>
s &= n
<span style="color: #008080;">if</span> <span style="color: #000000;">pernicious</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
n += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?s
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
s = {}
<!--</syntaxhighlight>-->
for i=888_888_877 to 888_888_888 do
if pernicious(i) then
s &= i
end if
end for
?s</lang>
{{out}}
<pre>
Line 1,690 ⟶ 2,637:
{888888877,888888878,888888880,888888883,888888885,888888886}
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
println(take_n(pernicious_number,25,1)),
println([J : J in 888888877..888888888, pernicious_number(J)]),
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.
 
pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).
 
pernicious_number(N) => prime(pop_count(N)).</syntaxhighlight>
 
{{out}}
<pre>[3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36]
[888888877,888888878,888888880,888888883,888888885,888888886]</pre>
 
=={{header|PicoLisp}}==
Using 'prime?' from [[Primality by trial division#PicoLisp]].
<langsyntaxhighlight PicoLisplang="picolisp">(de pernicious? (N)
(prime? (cnt = (chop (bin N)) '("1" .))) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">: (let N 0
(do 25
(until (pernicious? (inc 'N)))
Line 1,703 ⟶ 2,678:
 
: (filter pernicious? (range 888888877 888888888))
-> (888888877 888888878 888888880 888888883 888888885 888888886)</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
pern: procedure options (main);
declare (i, n) fixed binary (31);
Line 1,735 ⟶ 2,710:
 
end pern;
</syntaxhighlight>
</lang>
Results:
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886 888888889 888888890 888888892 888888897 888888898 888888900
</pre>
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To decide if a number is pernicious:
Find a population count of the number.
If the population count is prime, say yes.
Say no.
 
To find a population count of a number:
Privatize the number.
Loop.
If the number is 0, exit.
Bitwise and the number with the number minus 1.
Bump the population count.
Repeat.
 
To run:
Start up.
Show the first twenty-five pernicious numbers.
Show the pernicious numbers between 888888877 and 888888888.
Wait for the escape key.
Shut down.
 
To show the first twenty-five pernicious numbers:
Put 0 into a number.
Put 0 into a pernicious number count.
Loop.
If the pernicious number count is greater than 24, write "" on the console; exit.
If the number is pernicious, show the number; bump the pernicious number count.
Bump the number.
Repeat.
 
To show a number:
Convert the number to a string.
Write the string then " " on the console without advancing.
 
To show the pernicious numbers between a number and another number:
Privatize the number.
Subtract 1 from the number.
Loop.
If the number is past the other number, exit.
If the number is pernicious, show the number.
Repeat.</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function pop-count($n) {
(([Convert]::ToString($n, 2)).toCharArray() | where {$_ -eq '1'}).count
Line 1,771 ⟶ 2,793:
"pernicious numbers between 888,888,877 and 888,888,888"
"$(888888877..888888888 | where{isprime(pop-count $_)})"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,785 ⟶ 2,807:
 
The '''PopCount''' property is available in each of the returned integers.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Select-PerniciousNumber
{
Line 1,830 ⟶ 2,852:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$start, $end = 0, 999999
$range1 = $start..$end | Select-PerniciousNumber | Select-Object -First 25
Line 1,841 ⟶ 2,863:
 
"Pernicious numbers between {0} and {1}:`n{2}`n" -f $start, $end, ($range2 -join ", ")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,852 ⟶ 2,874:
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
<lang PureBasic>
EnableExplicit
 
Line 1,914 ⟶ 2,936:
CloseConsole()
EndIf
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,929 ⟶ 2,951:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">>>> def popcount(n): return bin(n).count("1")
 
>>> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61}
Line 1,948 ⟶ 2,970:
>>> p
[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]
>>> </langsyntaxhighlight>
 
===Functional===
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Pernicious numbers'''
 
from itertools import count, islice
Line 1,979 ⟶ 3,001:
'''
def go(x):
return Just(divmod(x, 2)) if 0 < x else Nothing()None
return sum(unfoldl(go)(n))
 
 
# TEST --------------------------- TEST -------------------------
# main :: IO ()
def main():
Line 1,989 ⟶ 3,011:
[888,888,877..888,888,888]
'''
 
print(
take(25)(
Line 1,995 ⟶ 3,018:
)
print([
x for x in enumFromTo(888888877)(888888888)
if 888888888isPernicious(x)
) if isPernicious(x)
])
 
 
# GENERIC ------------------------- GENERIC ------------------------
 
# Just :: a -> Maybe a
def Just(x):
'''Constructor for an inhabited Maybe (option type) value.
Wrapper containing the result of a computation.
'''
return {'type': 'Maybe', 'Nothing': False, 'Just': x}
 
 
# Nothing :: Maybe a
def Nothing():
'''Constructor for an empty Maybe (option type) value.
Empty wrapper returned where a computation is not possible.
'''
return {'type': 'Maybe', 'Nothing': True}
 
 
# enumFromTo :: Int -> Int -> [Int]
Line 2,023 ⟶ 3,029:
'''Enumeration of integer values [m..n]'''
def go(n):
return list(range(m, 1 + n))
return lambda n: go(n)
 
 
Line 2,060 ⟶ 3,066:
# unfoldl :: (b -> Maybe (b, a)) -> b -> [a]
def unfoldl(f):
'''A lazy (generator) list unfolded from a seed value
'''Dual to reduce or foldl.
Whereby theserepeated reduceapplication aof listf tountil ano summaryresidue value, unfoldlremains.
buildsDual ato list from a seed valuefold/reduce.
Where f returns Just(a,either b),None aor isjust appended(residue, to the list,value).
andFor thea residualstrict boutput islist, used aswrap the argumentresult for thewith nextlist()
application of f.
When f returns Nothing, the completed list is returned.
'''
def go(v):
x, rresidueValue = f(v, v)
xswhile = []residueValue:
while True: yield residueValue[1]
mbresidueValue = f(xresidueValue[0])
return go
if mb.get('Nothing'):
return xs
else:
x, r = mb.get('Just')
xs.insert(0, r)
return xs
return lambda x: go(x)
 
 
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre>
 
=={{header|RacketQuackery}}==
<syntaxhighlight lang="quackery"> [ $ "rosetta/seive.qky" loadfile
$ "rosetta/popcount.qky" loadfile ] now!
 
( i.e. using the code at )
<lang racket>#lang racket
( http://rosettacode.org/wiki/Sieve_of_Eratosthenes and )
( http://rosettacode.org/wiki/Population_count )
 
29 eratosthenes ( Precompute as many primes as are required )
( for the task. 888,888,888 is a 30 bit )
( number less than (2^30)-1 so primes up to )
( 29 will suffice. )
 
[ 1+ over - times
[ dup i^ +
dup popcount
isprime iff
[ echo sp ]
else drop ]
drop ] is perniciousrange ( n n --> )
 
25 echopopwith isprime cr
888888877 888888888 perniciousrange cr</syntaxhighlight>
 
{{out}}
 
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(require math/number-theory rnrs/arithmetic/bitwise-6)
 
Line 2,111 ⟶ 3,138:
(module+ test
(require rackunit)
(check-true (pernicious? 22)))</langsyntaxhighlight>
 
{{out}}
Line 2,119 ⟶ 3,146:
display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive).
888888877, 888888878, 888888880, 888888883, 888888885, 888888886</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Straightforward implementation using Raku's ''is-prime'' built-in subroutine.
<syntaxhighlight lang="raku" line>sub is-pernicious(Int $n --> Bool) {
is-prime [+] $n.base(2).comb;
}
 
say (grep &is-pernicious, 0 .. *)[^25];
say grep &is-pernicious, 888_888_877 .. 888_888_888;</syntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886</pre>
 
=={{header|REXX}}==
Line 2,141 ⟶ 3,182:
╚════════════════════════════════════════════════════════════════════════════════════════╝
</pre>
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays a number (and also a range) of pernicious numbers.*/
numeric digits 100 /*be able to handle large numbers. */
parse arg N L H . /*obtain optional arguments from the CL*/
Line 2,172 ⟶ 3,213:
return substr($, 2) /*return the results, sans 1st blank. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
popCount: return length( space( translate( x2b( d2x(arg(1))) +0,, 0), 0)) /*count 1's.*/</langsyntaxhighlight>
'''output''' &nbsp; when the default inputs are used:
<pre>
Line 2,184 ⟶ 3,225:
=={{header|Ring}}==
Programming note: &nbsp; as written, this program can't handle the large numbers required for the 2<sup>nd</sup> task requirement &nbsp; (it receives a '''Numeric Overflow''').
<langsyntaxhighlight lang="ring">
# Project : Pernicious numbers
 
Line 2,224 ⟶ 3,265:
next
return 1
</syntaxhighlight>
</lang>
Output:
<pre>
The first 25 pernicious numbers:
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
</pre>
 
=={{header|RPL}}==
≪ '''IF''' DUP 5 ≤ '''THEN'''
{ 2 3 5 } SWAP POS
'''ELSE'''
'''IF''' DUP 2 MOD NOT '''THEN''' 2
'''ELSE'''
DUP √ CEIL → lim
≪ 3 '''WHILE''' DUP2 MOD OVER lim ≤ AND '''REPEAT''' 2 + '''END'''
'''END''' MOD
'''END''' SIGN
≫ ''''PRIM?'''' STO
BIN R→B →STR 0
1 3 PICK SIZE '''FOR''' j
'''IF''' OVER j DUP SUB "1" == '''THEN''' 1 + '''END NEXT'''
SWAP DROP '''PRIM?'''
≫ ´'''PERN?'''’ STO
≪ { } 1 '''WHILE''' OVER SIZE 25 < '''REPEAT IF''' DUP PERN? '''THEN''' SWAP OVER + SWAP '''END''' 1 + '''END''' DROP
≫ ´'''TASK1'''’ STO
≪ { } 888888877 888888888 '''FOR''' n '''IF''' n '''PERN? THEN''' n + '''END NEXT'''
≫ ´'''TASK2'''’ STO
{{out}}
<pre>
2: { 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 }
1: { 888888877 888888878 888888880 888888883 888888885 888888886 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
class Integer
Line 2,247 ⟶ 3,319:
 
p 1.step.lazy.select(&:pernicious?).take(25).to_a
p ( 888888877..888888888).select(&:pernicious?)</langsyntaxhighlight>
{{out}}
<pre>
[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36]
[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
extern crate aks_test_for_primes;
 
use std::iter::Filter;
use std::ops::RangeFrom;
 
use aks_test_for_primes::is_prime;
 
fn main() {
for i in pernicious().take(25) {
print!("{} ", i);
}
println!();
for i in (888_888_877u64..888_888_888).filter(is_pernicious) {
print!("{} ", i);
}
}
 
fn pernicious() -> Filter<RangeFrom<u64>, fn(&u64) -> bool> {
(0u64..).filter(is_pernicious as fn(&u64) -> bool)
}
 
fn is_pernicious(n: &u64) -> bool {
is_prime(n.count_ones())
}
</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|S-lang}}==
<langsyntaxhighlight Slang="s-lang">% Simplistic prime-test from prime-by-trial-division:
define is_prime(n)
{
Line 2,301 ⟶ 3,406:
}
print(strjoin(list_to_array(plist), " "));
</syntaxhighlight>
</lang>
{{out}}
"3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36"
Line 2,308 ⟶ 3,413:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def isPernicious( v:Long ) : Boolean = BigInt(v.toBinaryString.toList.filter( _ == '1' ).length).isProbablePrime(16)
 
// Generate the output
Line 2,315 ⟶ 3,420:
println( Stream.from(2).filter( isPernicious(_) ).take(a).toList.mkString(",") )
println( {for( i <- b1 to b2 if( isPernicious(i) ) ) yield i}.mkString(",") )
}</langsyntaxhighlight>
{{out}}
<pre>3,5,6,7,9,10,11,12,13,14,17,18,19,20,21,22,24,25,26,28,31,33,34,35,36
Line 2,326 ⟶ 3,431:
is used to compute the population count of the bitset.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const set of integer: primes is {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
Line 2,351 ⟶ 3,456:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,360 ⟶ 3,465:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_pernicious(n) {
{{trans|Perl}}
n.sumdigits(2).is_prime
<lang ruby>func is_pernicious(n) {
var c = 2693408940; # primes < 32 as set bits
while (n > 0) { c >>= 1; n &= (n - 1) }
c & 1;
}
 
say is_pernicious.first(25).join(' ')
var (i, *p) = 0;
say is_pernicious.grep(888_888_877..888_888_888).join(' ')</syntaxhighlight>
while (p.len < 25) {
p << i if is_pernicious(i);
++i;
}
 
say p.join(' ');
 
var (i, *p) = 888888877;
while (i < 888888888) {
p << i if is_pernicious(i);
++i;
}
 
say p.join(' ');</lang>
 
{{out}}
Line 2,390 ⟶ 3,479:
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
<lang swift>import Foundation
 
extension BinaryInteger {
Line 2,424 ⟶ 3,512:
 
print("First 25 Pernicious numbers: \(Array(first25))")
print("Pernicious numbers between 888_888_877...888_888_888: \(Array(rng))")</langsyntaxhighlight>
 
{{out}}
Line 2,432 ⟶ 3,520:
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
 
<lang symsyn>
 
primes : 0b0010100000100000100010100010000010100000100010100010100010101100
Line 2,510 ⟶ 3,597:
return
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,518 ⟶ 3,605:
888888877 888888878 888888880 888888883 888888885 888888886 888888889
</pre>
 
=={{header|Tcl}}==
{{tcllib|math::numtheory}}
<langsyntaxhighlight lang="tcl">package require math::numtheory
 
proc pernicious {n} {
Line 2,535 ⟶ 3,621:
if {[pernicious $n]} {lappend p $n}
}
puts [join $p ","]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,543 ⟶ 3,629:
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Private Function population_count(ByVal number As Long) As Integer
Dim result As Integer
Dim digit As Integer
Line 2,591 ⟶ 3,677:
End If
Next n
End Sub</langsyntaxhighlight>{{out}}
<pre> 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886 </pre>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">'check if the number is pernicious
Function IsPernicious(n)
IsPernicious = False
Line 2,658 ⟶ 3,745:
End If
Next
WScript.StdOut.WriteLine</langsyntaxhighlight>
 
{{out}}
Line 2,671 ⟶ 3,758:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function PopulationCount(n As Long) As Integer
Line 2,715 ⟶ 3,802:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
Line 2,722 ⟶ 3,809:
=={{header|Wortel}}==
The following function returns true if it's argument is a pernicious number:
<langsyntaxhighlight lang="wortel">:ispernum ^(@isPrime \@count \=1 @arr &\`![.toString 2])</langsyntaxhighlight>
Task:
<langsyntaxhighlight lang="wortel">!-ispernum 1..36 ; returns [3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36]
!-ispernum 888888877..888888888 ; returns [888888877 888888878 888888880 888888883 888888885 888888886]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">var pernicious = Fn.new { |w|
var ff = 2.pow(32) - 1
var mask1 = (ff / 3).floor
var mask3 = (ff / 5).floor
var maskf = (ff / 17).floor
var maskp = (ff / 255).floor
w = w - (w >> 1 & mask1)
w = (w & mask3) + (w >>2 & mask3)
w = (w + (w >> 4)) & maskf
return 0xa08a28ac >> (w*maskp >> 24) & 1 != 0
}
 
var i = 0
var n = 1
while (i < 25) {
if (pernicious.call(n)) {
System.write("%(n) ")
i = i + 1
}
n = n + 1
}
System.print()
for (n in 888888877..888888888) {
if (pernicious.call(n)) System.write("%(n) ")
}
System.print()</syntaxhighlight>
 
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func IsPrime(N); \Return 'true' if N is prime
int N, D;
[if N <= 2 then return N = 2;
D:= 2;
while D*D <= N do
[if rem(N/D) = 0 then return false;
D:= D+1;
];
return true;
];
 
func BitCount(N); \Return number of set bits in N
int N, C;
[C:= 0;
while N do
[C:= C+1;
N:= N & N-1;
];
return C;
];
 
int N, C;
[N:= 0; C:= 0;
loop [if IsPrime(BitCount(N)) then
[IntOut(0, N); ChOut(0, ^ );
C:= C+1;
if C >= 25 then quit;
];
N:= N+1;
];
CrLf(0);
for N:= 888_888_877 to 888_888_888 do
if IsPrime(BitCount(N)) then
[IntOut(0, N); ChOut(0, ^ )];
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
888888877 888888878 888888880 888888883 888888885 888888886
</pre>
 
=={{header|zkl}}==
The largest number of bits is 30.
<langsyntaxhighlight lang="zkl">primes:=T(2,3,5,7,11,13,17,19,23,29,31,37,41);
N:=0; foreach n in ([2..]){
if(n.num1s : primes.holds(_)){
Line 2,738 ⟶ 3,903:
foreach n in ([0d888888877..888888888]){
if (n.num1s : primes.holds(_)) "%,d; ".fmt(n).print();
}</langsyntaxhighlight>
Int.num1s returns the number of 1 bits. eg (3).num1s-->2
{{out}}
Line 2,746 ⟶ 3,911:
</pre>
Or in a more functional style:
<langsyntaxhighlight lang="zkl">primes:=T(2,3,5,7,11,13,17,19,23,29,31,37,41);
p:='wrap(n){ primes.holds(n.num1s) };
 
[1..].filter(25,p).toString(*).println();
[0d888888877..888888888].filter(p).println();</langsyntaxhighlight>
'wrap is syntactic sugar for a closure - it creates a function that
wraps local data (variable primes in this case). We assign that function to p.
1,973

edits