Pernicious numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|J}}: more compact)
m (syntax highlighting fixup automation)
Line 22:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F popcount(n)
R bin(n).count(‘1’)
 
Line 46:
L(i) 888888877..888888888
I is_prime(popcount(i))
print(i, end' ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 58:
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 183:
XDEC DS CL12 edit zone
YREGS
END PERNIC</langsyntaxhighlight>
{{out}}
<pre>
Line 194:
Uses package Population_Count from [[Population count#Ada]].
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Population_Count; use Population_Count;
 
procedure Pernicious is
Line 233:
end loop;
Ada.Text_IO.New_Line;
end;</langsyntaxhighlight>
 
 
Line 243:
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 256:
end loop;
Ada.Text_IO.Put_Line(Natural'Image(Counter));
end Count_Pernicious;</langsyntaxhighlight>
 
{{out}}
Line 268:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># calculate various pernicious numbers #
 
# returns the population (number of bits on) of the non-negative integer n #
Line 315:
OD;
print( ( newline ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 323:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight 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 ) ;
Line 375:
write();
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 383:
 
=={{header|AppleScript}}==
<langsyntaxhighlight 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
Line 448:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">pernicious?: function [n][
prime? size filter split as.binary n 'x -> x="0"
]
Line 471:
]
print ""
print select 888888877..888888888 => pernicious?</langsyntaxhighlight>
 
{{out}}
Line 480:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">c := 0
while c < 25
if IsPern(A_Index)
Line 495:
, 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
Line 501:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PERNICIOUS_NUMBERS.AWK
BEGIN {
Line 556:
return gsub(/1/,"&",n)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 568:
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 579:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
typedef unsigned uint;
Line 603:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 611:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 673:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 681:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
using namespace std;
Line 741:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 749:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn counting-numbers
([] (counting-numbers 1))
([n] (lazy-seq (cons n (counting-numbers (inc n))))))
Line 757:
(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)
Line 763:
 
=={{header|CLU}}==
<langsyntaxhighlight 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.
Line 810:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>First 25 pernicious numbers:
Line 818:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PERNICIOUS-NUMBERS.
Line 911:
CHECK-DSOR.
DIVIDE POPCOUNT BY DSOR GIVING DIV-RSLT.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</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 919:
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 929:
(loop for n from 888888877 to 888888888
when (primep (logcount n))
collect n))</langsyntaxhighlight>
 
{{Out}}
Line 936:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, core.bitop;
 
Line 942:
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 950:
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|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'sequences)
 
Line 964:
(take (filter pernicious? [888888877 .. 888888889]) #:all)
→ (888888877 888888878 888888880 888888883 888888885 888888886)
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,068:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,076:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
<lang Elixir>
defmodule SieveofEratosthenes do
def init(lim) do
Line 1,118:
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 1,130:
 
=={{header|F#|F sharp}}==
<langsyntaxhighlight lang="fsharp">open System
 
//Taken from https://gist.github.com/rmunn/bc49d32a586cdfa5bcab1c3e7b45d7ac
Line 1,151:
[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 1,157:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: lists lists.lazy math.bitwise math.primes math.ranges
prettyprint sequences ;
 
Line 1,163:
 
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>
Line 1,172:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: popcount { n -- u }
0
begin
Line 1,212:
888888877 888888888 pernicious_numbers_between
 
bye</langsyntaxhighlight>
 
{{out}}
Line 1,224:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program pernicious
implicit none
 
Line 1,278:
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 1,285:
=={{header|FreeBASIC}}==
{{trans|PureBasic}}
<langsyntaxhighlight lang="freebasic">
' FreeBASIC v1.05.0 win64
 
Line 1,343:
Sleep
End
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,357:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">isPernicious = {|x|
bits = countToDict[integerDigits[x,2]].get[1,0]
return bits > 1 and isPrime[bits]
Line 1,363:
 
println["First 25: " + first[select[count[1], isPernicious], 25]]
println[select[888_888_877 to 888_888_888, isPernicious]]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,379:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,411:
}
fmt.Println()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,419:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
class example{
static void main(String[] args){
Line 1,444:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,452:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">module Pernicious
where
 
Line 1,468:
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,475:
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,493:
[ 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,501:
 
Works in both languages:
<langsyntaxhighlight lang="unicon">link "factors"
 
procedure main(A)
Line 1,516:
while n > 0 do c +:= 1(n%2, n/:=2)
return c
end</langsyntaxhighlight>
 
{{Out}}
Line 1,530:
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.@(+*)@-~
(#~ 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,570:
}
}
}</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,578:
{{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,614:
;
 
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,622:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Primes
 
ispernicious(n::Integer) = isprime(count_ones(n))
Line 1,637:
 
println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", ")) </langsyntaxhighlight>
 
{{out}}
Line 1,644:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun isPrime(n: Int): Boolean {
Line 1,690:
if (isPernicious(i)) print("$i ")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,704:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,796:
done
echo
</syntaxhighlight>
</lang>
{{out}}<pre>
First 25 Pernicious numbers:
Line 1,806:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Test primality by trial division
function isPrime (x)
if x < 2 then return false end
Line 1,858:
-- 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,864:
 
=={{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,890:
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,897:
 
=={{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,913:
, {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,922:
===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|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Pernicious;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,978:
 
ReadChar
END Pernicious.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import strutils
 
proc count(s: string; sub: char): int =
Line 2,011:
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]
Line 2,017:
 
=={{header|Panda}}==
<langsyntaxhighlight lang="panda">fun prime(a) type integer->integer
a where count{{a.factor}}==2
fun pernisc(a) type integer->integer
Line 2,023:
 
1..36.pernisc
888888877..888888888.pernisc</langsyntaxhighlight>
 
{{out}}
Line 2,030:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">pern(n)=isprime(hammingweight(n))
select(pern, [1..36])
select(pern,[888888877..888888888])</langsyntaxhighlight>
{{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:
 
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 2,125:
inc(k,k);
until k>64;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,140:
=={{header|Perl}}==
{{trans|C}}
<langsyntaxhighlight lang="perl">sub is_pernicious {
my $n = shift;
my $c = 2693408940; # primes < 32 as set bits
Line 2,161:
}
 
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 2,168:
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|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,205:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
println(take_n(pernicious_number,25,1)),
Line 2,226:
pop_count(N) = sum([1: I in N.to_binary_string(), I = '1']).
 
pernicious_number(N) => prime(pop_count(N)).</langsyntaxhighlight>
 
{{out}}
Line 2,235:
=={{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 2,245:
 
: (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 2,277:
 
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
Line 2,284:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To decide if a number is pernicious:
Find a population count of the number.
If the population count is prime, say yes.
Line 2,323:
If the number is past the other number, exit.
If the number is pernicious, show the number.
Repeat.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,331:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function pop-count($n) {
(([Convert]::ToString($n, 2)).toCharArray() | where {$_ -eq '1'}).count
Line 2,360:
"pernicious numbers between 888,888,877 and 888,888,888"
"$(888888877..888888888 | where{isprime(pop-count $_)})"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,374:
 
The '''PopCount''' property is available in each of the returned integers.
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Select-PerniciousNumber
{
Line 2,419:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$start, $end = 0, 999999
$range1 = $start..$end | Select-PerniciousNumber | Select-Object -First 25
Line 2,430:
 
"Pernicious numbers between {0} and {1}:`n{2}`n" -f $start, $end, ($range2 -join ", ")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,441:
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
<lang PureBasic>
EnableExplicit
 
Line 2,503:
CloseConsole()
EndIf
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,518:
=={{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 2,537:
>>> 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 2,649:
# 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]
Line 2,656:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "rosetta/seive.qky" loadfile
$ "rosetta/popcount.qky" loadfile ] now!
 
Line 2,678:
25 echopopwith isprime cr
888888877 888888888 perniciousrange cr</langsyntaxhighlight>
 
{{out}}
Line 2,687:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory rnrs/arithmetic/bitwise-6)
 
Line 2,707:
(module+ test
(require rackunit)
(check-true (pernicious? 22)))</langsyntaxhighlight>
 
{{out}}
Line 2,720:
 
Straightforward implementation using Raku's ''is-prime'' built-in subroutine.
<syntaxhighlight lang="raku" perl6line>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;</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,751:
╚════════════════════════════════════════════════════════════════════════════════════════╝
</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,782:
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,794:
=={{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,834:
next
return 1
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,842:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
class Integer
Line 2,857:
 
p 1.step.lazy.select(&:pernicious?).take(25).to_a
p ( 888888877..888888888).select(&:pernicious?)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,865:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
extern crate aks_test_for_primes;
 
Line 2,890:
is_prime(n.count_ones())
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,898:
 
=={{header|S-lang}}==
<langsyntaxhighlight Slang="s-lang">% Simplistic prime-test from prime-by-trial-division:
define is_prime(n)
{
Line 2,944:
}
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,951:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def isPernicious( v:Long ) : Boolean = BigInt(v.toBinaryString.toList.filter( _ == '1' ).length).isProbablePrime(16)
 
// Generate the output
Line 2,958:
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,969:
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,994:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,003:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_pernicious(n) {
n.sumdigits(2).is_prime
}
 
say is_pernicious.first(25).join(' ')
say is_pernicious.grep(888_888_877..888_888_888).join(' ')</langsyntaxhighlight>
 
{{out}}
Line 3,018:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 3,051:
 
print("First 25 Pernicious numbers: \(Array(first25))")
print("Pernicious numbers between 888_888_877...888_888_888: \(Array(rng))")</langsyntaxhighlight>
 
{{out}}
Line 3,060:
=={{header|Symsyn}}==
 
<langsyntaxhighlight lang="symsyn">
 
primes : 0b0010100000100000100010100010000010100000100010100010100010101100
Line 3,137:
return
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,148:
=={{header|Tcl}}==
{{tcllib|math::numtheory}}
<langsyntaxhighlight lang="tcl">package require math::numtheory
 
proc pernicious {n} {
Line 3,161:
if {[pernicious $n]} {lappend p $n}
}
puts [join $p ","]</langsyntaxhighlight>
{{out}}
<pre>
Line 3,169:
 
=={{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 3,217:
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 3,285:
End If
Next
WScript.StdOut.WriteLine</langsyntaxhighlight>
 
{{out}}
Line 3,298:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function PopulationCount(n As Long) As Integer
Line 3,342:
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 3,349:
=={{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}}
<langsyntaxhighlight lang="ecmascript">var pernicious = Fn.new { |w|
var ff = 2.pow(32) - 1
var mask1 = (ff / 3).floor
Line 3,381:
if (pernicious.call(n)) System.write("%(n) ")
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 3,391:
=={{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 3,400:
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 3,408:
</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.
10,327

edits