How to resolve the algorithm Pernicious numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pernicious numbers step by step in the Wren programming language

Table of Contents

Problem Statement

A   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.

22   (which is   10110   in binary)   has a population count of   3,   which is prime,   and therefore
22   is a pernicious number.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pernicious numbers step by step in the Wren programming language

Source code in the wren programming language

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()

  

You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Binary strings step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Objeck programming language
You may also check:How to resolve the algorithm Sudan function step by step in the CLU programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the PL/I programming language