Pernicious numbers: Difference between revisions
m
Added Easylang
(Frink) |
m (Added Easylang) |
||
(16 intermediate revisions by 12 users not shown) | |||
Line 22:
=={{header|11l}}==
<
I n < 2
R 0B
Line 36 ⟶ 33:
V cnt = 0
L
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)
cnt++
Line 45 ⟶ 42:
print()
L(i) 888888877..888888888
I is_prime(bits:popcount(i))
print(i, end' ‘ ’)</
{{out}}
Line 58 ⟶ 55:
For maximum compatibility, this program uses only the basic instruction set (S/360)
with 2 ASSIST macros (XDECO,XPRNT).
<
PERNIC CSECT
USING PERNIC,R13 base register and savearea pointer
Line 183 ⟶ 180:
XDEC DS CL12 edit zone
YREGS
END PERNIC</
{{out}}
<pre>
Line 190 ⟶ 187:
</pre>
=={{header|
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]].
<
procedure Pernicious is
Line 233 ⟶ 354:
end loop;
Ada.Text_IO.New_Line;
end;</
Line 243 ⟶ 364:
A small modification allows to count all the pernicious numbers between 1 and 2**32 in about 32 seconds:
<
begin
-- initialize array Prime; Prime(I) must be true if and only if I is a prime
Line 256 ⟶ 377:
end loop;
Ada.Text_IO.Put_Line(Natural'Image(Counter));
end Count_Pernicious;</
{{out}}
Line 268 ⟶ 389:
=={{header|ALGOL 68}}==
<
# returns the population (number of bits on) of the non-negative integer n #
Line 315 ⟶ 436:
OD;
print( ( newline ) )
</syntaxhighlight>
{{out}}
<pre>
Line 323 ⟶ 444:
=={{header|ALGOL W}}==
<
% returns the population count of n %
integer procedure populationCount( integer value n ) ;
Line 375 ⟶ 496:
write();
end
end.</
{{out}}
<pre>
Line 383 ⟶ 504:
=={{header|AppleScript}}==
<
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
Line 448 ⟶ 569:
end task
task()</
{{output}}
<
888888877 888888878 888888880 888888883 888888885 888888886"</
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pernicious?: function [n][
prime? size filter split as.binary n 'x -> x="0"
]
Line 471 ⟶ 591:
]
print ""
print select 888888877..888888888 => pernicious?</
{{out}}
Line 480 ⟶ 600:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
while c < 25
if IsPern(A_Index)
Line 495 ⟶ 615:
, x := (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
return p[(x * 0x0101010101010101) >> 56]
}</
{{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 501 ⟶ 621:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PERNICIOUS_NUMBERS.AWK
BEGIN {
Line 556 ⟶ 676:
return gsub(/1/,"&",n)
}
</syntaxhighlight>
{{out}}
<pre>
Line 562 ⟶ 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 568 ⟶ 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.
<
>8**`!#^_$@\<(^v^)>/#2^#\<2 2
^+**"X^yYo":+1<_:.48*,00v|: <%
v".D}Tx"$,+55_^#!p00:-1g<v |<
> * + : * * + ^^ ! % 2 $ <^ <^</
{{out}}
Line 579 ⟶ 855:
=={{header|C}}==
<
typedef unsigned uint;
Line 603 ⟶ 879:
return 0;
}</
{{out}}
<pre>
Line 611 ⟶ 887:
=={{header|C sharp|C#}}==
<
using System.Linq;
Line 673 ⟶ 949:
}
}
}</
{{out}}
<pre>
Line 681 ⟶ 957:
=={{header|C++}}==
<
#include <iostream>
using namespace std;
Line 741 ⟶ 1,017:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 749 ⟶ 1,025:
=={{header|Clojure}}==
<
([] (counting-numbers 1))
([n] (lazy-seq (cons n (counting-numbers (inc n))))))
Line 757 ⟶ 1,033:
(prime? (count (filter #(= % \1) (Integer/toString n 2)))))
(println (take 25 (filter pernicious? (counting-numbers))))
(println (filter pernicious? (range 888888877 888888889)))</
{{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 763 ⟶ 1,039:
=={{header|CLU}}==
<
% higher than the amount of bits in it (max 64)
% so we can get away with a very simple primality test.
Line 810 ⟶ 1,086:
end
end
end start_up</
{{out}}
<pre>First 25 pernicious numbers:
Line 818 ⟶ 1,094:
=={{header|COBOL}}==
<
PROGRAM-ID. PERNICIOUS-NUMBERS.
Line 911 ⟶ 1,187:
CHECK-DSOR.
DIVIDE POPCOUNT BY DSOR GIVING DIV-RSLT.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</
{{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 919 ⟶ 1,195:
Using <code>primep</code> from [[Primality_by_trial_division#Common_Lisp|Primality by trial division]] task.
<
(loop for n = 1 then (1+ n)
when (primep (logcount n))
Line 929 ⟶ 1,205:
(loop for n from 888888877 to 888888888
when (primep (logcount n))
collect n))</
{{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}}==
<
import std.stdio, std.algorithm, std.range, core.bitop;
Line 942 ⟶ 1,276:
uint.max.iota.filter!pernicious.take(25).writeln;
iota(888_888_877, 888_888_889).filter!pernicious.writeln;
}</
{{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 950 ⟶ 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:
<
=={{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}}==
<
(lib 'sequences)
Line 964 ⟶ 1,344:
(take (filter pernicious? [888888877 .. 888888889]) #:all)
→ (888888877 888888878 888888880 888888883 888888885 888888886)
</syntaxhighlight>
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,068 ⟶ 1,448:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,076 ⟶ 1,456:
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule SieveofEratosthenes do
def init(lim) do
Line 1,118 ⟶ 1,498:
def pernicious?(n,primes), do: Enum.member?(primes,ones(n))
end
</syntaxhighlight>
<syntaxhighlight lang="elixir">
PerniciousNumbers.take(25)
PerniciousNumbers.between(888_888_877..888_888_888)
</syntaxhighlight>
{{out}}
Line 1,130 ⟶ 1,510:
=={{header|F#|F sharp}}==
<
//Taken from https://gist.github.com/rmunn/bc49d32a586cdfa5bcab1c3e7b45d7ac
Line 1,151 ⟶ 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</
{{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,157 ⟶ 1,537:
=={{header|Factor}}==
<
prettyprint sequences ;
Line 1,163 ⟶ 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</
{{out}}
<pre>
Line 1,172 ⟶ 1,552:
=={{header|Forth}}==
{{works with|Gforth}}
<
0
begin
Line 1,212 ⟶ 1,592:
888888877 888888888 pernicious_numbers_between
bye</
{{out}}
Line 1,224 ⟶ 1,604:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<
implicit none
Line 1,278 ⟶ 1,658:
end if
end function
end program</
{{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,285 ⟶ 1,665:
=={{header|FreeBASIC}}==
{{trans|PureBasic}}
<
' FreeBASIC v1.05.0 win64
Line 1,343 ⟶ 1,723:
Sleep
End
</syntaxhighlight>
{{out}}
Line 1,357 ⟶ 1,737:
=={{header|Frink}}==
<
bits = countToDict[integerDigits[x,2]].get[1,0]
return bits > 1 and isPrime[bits]
Line 1,363 ⟶ 1,743:
println["First 25: " + first[select[count[1], isPernicious], 25]]
println[select[888_888_877 to 888_888_888, isPernicious]]</
{{out}}
<pre>
Line 1,372 ⟶ 1,752:
=={{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}}==
<
import "fmt"
Line 1,411 ⟶ 1,803:
}
fmt.Println()
}</
{{out}}
<pre>
Line 1,419 ⟶ 1,811:
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
class example{
static void main(String[] args){
Line 1,444 ⟶ 1,836:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,452 ⟶ 1,844:
=={{header|Haskell}}==
<
where
Line 1,468 ⟶ 1,860:
solution1 = take 25 $ filter isPernicious [1 ..]
solution2 = filter isPernicious [888888877 .. 888888888]</
{{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,475 ⟶ 1,867:
Or, in a point-free and applicative style, using unfoldr for the population count:
<
import Data.List (unfoldr)
import Data.Tuple (swap)
Line 1,493 ⟶ 1,885:
[ take 25 $ filter isPernicious [1 ..]
, filter isPernicious [888888877 .. 888888888]
]</
{{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,499 ⟶ 1,891:
=={{header|Icon}} and {{header|Unicon}}==
Works in both languages:
<
procedure main(A)
Line 1,516 ⟶ 1,907:
while n > 0 do c +:= 1(n%2, n/:=2)
return c
end</
{{Out}}
Line 1,527 ⟶ 1,918:
=={{header|J}}==
Implementation:
<
Task (thru taken from the [[Loops/Downward_for#J|Loops/Downward for]] task).:
<
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 888888878 888888880 888888883 888888885 888888886</
=={{header|Java}}==
<
//very simple isPrime since x will be <= Long.SIZE
public static boolean isPrime(int x){
Line 1,570 ⟶ 1,960:
}
}
}</
{{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,578 ⟶ 1,968:
{{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.
<
def is_prime:
if . == 2 then true
Line 1,614 ⟶ 2,004:
;
task</
{{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,621 ⟶ 2,011:
=={{header|Julia}}==
{{works with|Julia|0.6}}
<syntaxhighlight lang="julia">using Primes
ispernicious(n::Integer) = isprime(count_ones(n))
Line 1,637 ⟶ 2,026:
println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", ")) </
{{out}}
Line 1,644 ⟶ 2,033:
=={{header|Kotlin}}==
<
fun isPrime(n: Int): Boolean {
Line 1,690 ⟶ 2,079:
if (isPernicious(i)) print("$i ")
}
}</
{{out}}
Line 1,704 ⟶ 2,093:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 1,796 ⟶ 2,185:
done
echo
</syntaxhighlight>
{{out}}<pre>
First 25 Pernicious numbers:
Line 1,806 ⟶ 2,195:
=={{header|Lua}}==
<
function isPrime (x)
if x < 2 then return false end
Line 1,858 ⟶ 2,247:
-- Main procedure
pernicious(25)
pernicious(888888877, 888888888)</
{{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,864 ⟶ 2,253:
=={{header|Maple}}==
<
return evalb(isprime(rhs(Statistics:-Tally(StringTools:-Explode(convert(convert(n, binary), string)))[-1])));
end proc;
Line 1,890 ⟶ 2,279:
end do;
return list_num;
end proc:</
{{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,897 ⟶ 2,286:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
perniciousQ[n_Integer] := popcount[n] // PrimeQ
perniciouscount = 0;
Line 1,913 ⟶ 2,302:
, {i, 888888877, 888888888}]
Print["Pernicious numbers between 888,888,877 and 888,888,888 (inclusive)"]
perniciouslist2</
{{out}}
<pre>first 25 pernicious numbers
Line 1,922 ⟶ 2,311:
===Alternate Code===
test function
<
First 25 pernicious numbers
<
Pernicious numbers betweeen 888888877 and 888888888 inclusive
<
=={{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}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,978 ⟶ 2,391:
ReadChar
END Pernicious.</
=={{header|Nim}}==
{{trans|Python}}
<
proc count(s: string; sub: char): int =
Line 2,011 ⟶ 2,424:
inc i
echo p</
{{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|OCaml}}==
<syntaxhighlight lang="ocaml">let rec popcount n =
if n = 0 then 0 else succ (popcount (n land pred n))
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> 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|Panda}}==
<
a where count{{a.factor}}==2
fun pernisc(a) type integer->integer
Line 2,023 ⟶ 2,457:
1..36.pernisc
888888877..888888888.pernisc</
{{out}}
Line 2,030 ⟶ 2,464:
=={{header|PARI/GP}}==
<
select(pern, [1..36])
select(pern,[888888877..888888888])</
{{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]
Line 2,042 ⟶ 2,476:
Added easy counting of pernicious numbers for full Bit ranges like 32-Bit
<
{$IFDEF FPC}
{$OPTIMIZATION ON,Regvar,ASMCSE,CSE,PEEPHOLE}// 3x speed up
Line 2,125 ⟶ 2,559:
inc(k,k);
until k>64;
end.</
{{out}}
<pre>
Line 2,140 ⟶ 2,574:
=={{header|Perl}}==
{{trans|C}}
<
my $n = shift;
my $c = 2693408940; # primes < 32 as set bits
Line 2,161 ⟶ 2,595:
}
print join ' ', @p;</
{{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,168 ⟶ 2,602:
Alternately, generating the same output using a method similar to Pari/GP:
{{libheader|ntheory}}
<
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";</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 2,197 ⟶ 2,631:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 2,205 ⟶ 2,639:
=={{header|Picat}}==
<
println(take_n(pernicious_number,25,1)),
Line 2,226 ⟶ 2,660:
pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).
pernicious_number(N) => prime(pop_count(N)).</
{{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]].
<
(prime? (cnt = (chop (bin N)) '("1" .))) )</
Test:
<
(do 25
(until (pernicious? (inc 'N)))
Line 2,245 ⟶ 2,678:
: (filter pernicious? (range 888888877 888888888))
-> (888888877 888888878 888888880 888888883 888888885 888888886)</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
pern: procedure options (main);
declare (i, n) fixed binary (31);
Line 2,277 ⟶ 2,710:
end pern;
</syntaxhighlight>
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
Line 2,284 ⟶ 2,717:
=={{header|Plain English}}==
<
Find a population count of the number.
If the population count is prime, say yes.
Line 2,323 ⟶ 2,756:
If the number is past the other number, exit.
If the number is pernicious, show the number.
Repeat.</
{{out}}
<pre>
Line 2,331 ⟶ 2,764:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function pop-count($n) {
(([Convert]::ToString($n, 2)).toCharArray() | where {$_ -eq '1'}).count
Line 2,360 ⟶ 2,793:
"pernicious numbers between 888,888,877 and 888,888,888"
"$(888888877..888888888 | where{isprime(pop-count $_)})"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,374 ⟶ 2,807:
The '''PopCount''' property is available in each of the returned integers.
<syntaxhighlight lang="powershell">
function Select-PerniciousNumber
{
Line 2,419 ⟶ 2,852:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
$start, $end = 0, 999999
$range1 = $start..$end | Select-PerniciousNumber | Select-Object -First 25
Line 2,430 ⟶ 2,863:
"Pernicious numbers between {0} and {1}:`n{2}`n" -f $start, $end, ($range2 -join ", ")
</syntaxhighlight>
{{Out}}
<pre>
Line 2,441 ⟶ 2,874:
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
EnableExplicit
Line 2,503 ⟶ 2,936:
CloseConsole()
EndIf
</syntaxhighlight>
{{out}}
Line 2,518 ⟶ 2,951:
=={{header|Python}}==
===Procedural===
<
>>> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61}
Line 2,537 ⟶ 2,970:
>>> p
[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]
>>> </
===Functional===
{{Works with|Python|3.7}}
<
from itertools import count, islice
Line 2,649 ⟶ 3,082:
# MAIN ---
if __name__ == '__main__':
main()</
{{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,655 ⟶ 3,088:
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ $ "rosetta/seive.qky" loadfile
$ "rosetta/popcount.qky" loadfile ] now!
Line 2,678 ⟶ 3,110:
25 echopopwith isprime cr
888888877 888888888 perniciousrange cr</
{{out}}
Line 2,686 ⟶ 3,118:
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(require math/number-theory rnrs/arithmetic/bitwise-6)
Line 2,707 ⟶ 3,138:
(module+ test
(require rackunit)
(check-true (pernicious? 22)))</
{{out}}
Line 2,720 ⟶ 3,151:
Straightforward implementation using Raku's ''is-prime'' built-in subroutine.
<syntaxhighlight lang="raku"
is-prime [+] $n.base(2).comb;
}
say (grep &is-pernicious, 0 .. *)[^25];
say grep &is-pernicious, 888_888_877 .. 888_888_888;</
{{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,751 ⟶ 3,182:
╚════════════════════════════════════════════════════════════════════════════════════════╝
</pre>
<
numeric digits 100 /*be able to handle large numbers. */
parse arg N L H . /*obtain optional arguments from the CL*/
Line 2,782 ⟶ 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.*/</
'''output''' when the default inputs are used:
<pre>
Line 2,794 ⟶ 3,225:
=={{header|Ring}}==
Programming note: as written, this program can't handle the large numbers required for the 2<sup>nd</sup> task requirement (it receives a '''Numeric Overflow''').
<
# Project : Pernicious numbers
Line 2,834 ⟶ 3,265:
next
return 1
</syntaxhighlight>
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}}==
<
class Integer
Line 2,857 ⟶ 3,319:
p 1.step.lazy.select(&:pernicious?).take(25).to_a
p ( 888888877..888888888).select(&:pernicious?)</
{{out}}
<pre>
Line 2,865 ⟶ 3,327:
=={{header|Rust}}==
<
extern crate aks_test_for_primes;
Line 2,890 ⟶ 3,352:
is_prime(n.count_ones())
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,898 ⟶ 3,360:
=={{header|S-lang}}==
<
define is_prime(n)
{
Line 2,944 ⟶ 3,406:
}
print(strjoin(list_to_array(plist), " "));
</syntaxhighlight>
{{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,951 ⟶ 3,413:
=={{header|Scala}}==
<
// Generate the output
Line 2,958 ⟶ 3,420:
println( Stream.from(2).filter( isPernicious(_) ).take(a).toList.mkString(",") )
println( {for( i <- b1 to b2 if( isPernicious(i) ) ) yield i}.mkString(",") )
}</
{{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,969 ⟶ 3,431:
is used to compute the population count of the bitset.
<
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,994 ⟶ 3,456:
end for;
writeln;
end func;</
{{out}}
Line 3,003 ⟶ 3,465:
=={{header|Sidef}}==
<
n.sumdigits(2).is_prime
}
say is_pernicious.first(25).join(' ')
say is_pernicious.grep(888_888_877..888_888_888).join(' ')</
{{out}}
Line 3,017 ⟶ 3,479:
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
extension BinaryInteger {
Line 3,051 ⟶ 3,512:
print("First 25 Pernicious numbers: \(Array(first25))")
print("Pernicious numbers between 888_888_877...888_888_888: \(Array(rng))")</
{{out}}
Line 3,059 ⟶ 3,520:
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
primes : 0b0010100000100000100010100010000010100000100010100010100010101100
Line 3,137 ⟶ 3,597:
return
</syntaxhighlight>
{{out}}
Line 3,148 ⟶ 3,608:
=={{header|Tcl}}==
{{tcllib|math::numtheory}}
<
proc pernicious {n} {
Line 3,161 ⟶ 3,621:
if {[pernicious $n]} {lappend p $n}
}
puts [join $p ","]</
{{out}}
<pre>
Line 3,169 ⟶ 3,629:
=={{header|VBA}}==
{{trans|Phix}}<
Dim result As Integer
Dim digit As Integer
Line 3,217 ⟶ 3,677:
End If
Next n
End Sub</
<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}}==
<
Function IsPernicious(n)
IsPernicious = False
Line 3,285 ⟶ 3,745:
End If
Next
WScript.StdOut.WriteLine</
{{out}}
Line 3,298 ⟶ 3,758:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function PopulationCount(n As Long) As Integer
Line 3,342 ⟶ 3,802:
End Sub
End Module</
{{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 3,349 ⟶ 3,809:
=={{header|Wortel}}==
The following function returns true if it's argument is a pernicious number:
<
Task:
<
!-ispernum 888888877..888888888 ; returns [888888877 888888878 888888880 888888883 888888885 888888886]</
=={{header|Wren}}==
{{trans|Go}}
<
var ff = 2.pow(32) - 1
var mask1 = (ff / 3).floor
Line 3,381 ⟶ 3,841:
if (pernicious.call(n)) System.write("%(n) ")
}
System.print()</
{{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>
Line 3,391 ⟶ 3,894:
=={{header|zkl}}==
The largest number of bits is 30.
<
N:=0; foreach n in ([2..]){
if(n.num1s : primes.holds(_)){
Line 3,400 ⟶ 3,903:
foreach n in ([0d888888877..888888888]){
if (n.num1s : primes.holds(_)) "%,d; ".fmt(n).print();
}</
Int.num1s returns the number of 1 bits. eg (3).num1s-->2
{{out}}
Line 3,408 ⟶ 3,911:
</pre>
Or in a more functional style:
<
p:='wrap(n){ primes.holds(n.num1s) };
[1..].filter(25,p).toString(*).println();
[0d888888877..888888888].filter(p).println();</
'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.
|