September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,17 +1,14 @@
A   ''[[wp:Pernicious number|pernicious number]]''   is a positive integer whose   [[population count]]   is a prime.
The &nbsp; ''population count'' &nbsp; (also known as ''pop count'') &nbsp; is the number of <big>1</big>'s &nbsp;(ones) in the binary representation of a non-negative integer.
A &nbsp; [[wp:Pernicious number|pernicious number]] &nbsp; is a positive integer whose &nbsp; [[population count]] &nbsp; is a prime. &nbsp; The population count is the number of ones in the binary representation of a non-negative integer.
;Example:
'''22''' &nbsp; (which is &nbsp; '''10110''' &nbsp; in binary) &nbsp; has a population count of &nbsp; '''3''' &nbsp; (which is prime), &nbsp; and therefore
<br>'''22''' &nbsp; is a pernicious number.
;Example
'''22''' &nbsp; (which is &nbsp; '''10110''' &nbsp; in binary) &nbsp; has a population count of &nbsp; '''3''', &nbsp; which is prime, and therefore &nbsp; '''22''' &nbsp; is a pernicious number.
'''Task requirements'''
:* display the first &nbsp; 25 &nbsp; pernicious numbers.
:* display all pernicious numbers between &nbsp; 888,888,877 &nbsp; and &nbsp; 888,888,888 &nbsp; (inclusive).
:* display each list of integers on one line (which may or may not include a title). <br>
;Task
* display the first &nbsp; '''25''' &nbsp; pernicious numbers.
* display all pernicious numbers between &nbsp; '''888,888,877''' &nbsp; and &nbsp; '''888,888,888''' &nbsp; (inclusive).
* display each list of integers on one line &nbsp; (which may or may not include a title).
;See also

View file

@ -0,0 +1,24 @@
class example{
static void main(String[] args){
def n=0;
def counter=0;
while(counter<25){
if(print(n)){
counter++;}
n=n+1;
}
println();
def x=888888877;
while(x<888888889){
print(x);
x++;}
}
static def print(def a){
def primes=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47];
def c=Integer.toBinaryString(a);
String d=c;
def e=0;
for(i in d){if(i=='1'){e++;}}
if(e in primes){printf(a+" ");return 1;}
}
}

View file

@ -0,0 +1,47 @@
// version 1.0.5-2
fun isPrime(n: Int): Boolean {
if (n < 2) return false
if (n % 2 == 0) return n == 2
if (n % 3 == 0) return n == 3
var d : Int = 5
while (d * d <= n) {
if (n % d == 0) return false
d += 2
if (n % d == 0) return false
d += 4
}
return true
}
fun getPopulationCount(n: Int): Int {
if (n <= 0) return 0
var nn = n
var sum = 0
while (nn > 0) {
sum += nn % 2
nn /= 2
}
return sum
}
fun isPernicious(n: Int): Boolean = isPrime(getPopulationCount(n))
fun main(args: Array<String>) {
var n = 1
var count = 0
println("The first 25 pernicious numbers are:\n")
do {
if (isPernicious(n)) {
print("$n ")
count++
}
n++
}
while (count < 25)
println("\n")
println("The pernicious numbers between 888,888,877 and 888,888,888 inclusive are:\n")
for (i in 888888877..888888888) {
if (isPernicious(i)) print("$i ")
}
}

View file

@ -0,0 +1,28 @@
function is_prime(atom n)
if n<2 then return false end if
for i=2 to floor(sqrt(n)) do
if mod(n,i)=0 then return false end if
end for
return true
end function
function pernicious(integer n)
return is_prime(sum(int_to_bits(n,32)))
end function
sequence s = {}
integer n = 1
while length(s)<25 do
if pernicious(n) then
s &= n
end if
n += 1
end while
?s
s = {}
for i=888_888_877 to 888_888_888 do
if pernicious(i) then
s &= i
end if
end for
?s

View file

@ -0,0 +1,44 @@
function Select-PerniciousNumber
{
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$InputObject
)
Begin
{
function Test-Prime ([int]$n)
{
$n = [Math]::Abs($n)
if ($n -eq 0 -or $n -eq 1) {return $false}
for ($m = 2; $m -le [Math]::Sqrt($n); $m++)
{
if (($n % $m) -eq 0) {return $false}
}
return $true
}
[scriptblock]$popCount = {(([Convert]::ToString($this, 2)).ToCharArray() | Where-Object {$_ -eq '1'}).Count}
}
Process
{
foreach ($object in $InputObject)
{
$object | Add-Member -MemberType ScriptProperty -Name PopCount -Value $popCount -Force -PassThru | ForEach-Object {
if (Test-Prime $_.PopCount)
{
$_
}
}
}
}
}

View file

@ -0,0 +1,9 @@
$start, $end = 0, 999999
$range1 = $start..$end | Select-PerniciousNumber | Select-Object -First 25
"First {0} pernicious numbers:`n{1}`n" -f $range1.Count, ($range1 -join ", ")
$start, $end = 888888877, 888888888
$range2 = $start..$end | Select-PerniciousNumber
"Pernicious numbers between {0} and {1}:`n{2}`n" -f $start, $end, ($range2 -join ", ")

View file

@ -3,7 +3,7 @@ require "prime"
class Integer
def popcount
to_s(2).count("1")
to_s(2).count("1") #Ruby 2.4: digits(2).count(1)
end
def pernicious?

View file

@ -0,0 +1,46 @@
% Simplistic prime-test from prime-by-trial-division:
define is_prime(n)
{
if (n <= 1) return(0);
if (n == 2) return(1);
if ((n & 1) == 0) return(0);
variable mx = int(sqrt(n)), i;
_for i (3, mx, 1) {
if ((n mod i) == 0)
return(0);
}
return(1);
}
define population(n)
{
variable pc = 0;
do {
if (n & 1) pc++;
n /= 2;
}
while (n);
return(pc);
}
define is_pernicious(n)
{
return(is_prime(population(n)));
}
variable plist = {}, n = 0;
while (length(plist) < 25) {
n++;
if (is_pernicious(n))
list_append(plist, string(n));
}
print(strjoin(list_to_array(plist), " "));
plist = {};
_for n (888888877, 888888888, 1) {
if (is_pernicious(n))
list_append(plist, string(n));
}
print(strjoin(list_to_array(plist), " "));

View file

@ -0,0 +1,10 @@
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(_)){
print(n," ");
if((N+=1)==25) break;
}
}
foreach n in ([0d888888877..888888888]){
if (n.num1s : primes.holds(_)) "%,d; ".fmt(n).print();
}

View file

@ -0,0 +1,5 @@
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();