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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Pernicious numbers step by step in the Julia 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 Julia programming language

The provided Julia code defines functions and uses built-in functions for working with pernicious numbers. A pernicious number is a number whose number of ones in its binary representation is itself a prime number. Here's a breakdown of the code:

  1. Import the Primes module:

    using Primes

    This module provides functions for working with prime numbers.

  2. Define the ispernicious function:

    ispernicious(n::Integer) = isprime(count_ones(n))

    This function checks if a given integer n is pernicious. It counts the number of ones in the binary representation of n using count_ones, and then checks if the count is a prime number using isprime.

  3. Define the nextpernicious function:

    nextpernicious(n::Integer) = begin n += 1; while !ispernicious(n) n += 1 end; return n end

    This function finds the next pernicious number after n. It increments n by 1 and keeps incrementing it until it finds a pernicious number.

  4. Define the perniciouses function:

    function perniciouses(n::Int)
       rst = Vector{Int}(n)
       rst[1] = 3
       for i in 2:n
           rst[i] = nextpernicious(rst[i-1])
       end
       return rst
    end

    This function generates a vector of the first n pernicious numbers. It initializes the first element to 3 (the smallest pernicious number) and then iteratively finds the next pernicious number using nextpernicious.

  5. Define the perniciouses function with a range:

    perniciouses(a::Integer, b::Integer) = filter(ispernicious, a:b)

    This function returns a vector of pernicious numbers within a specified range from a (inclusive) to b (inclusive). It uses the filter function to apply the ispernicious function to each element in the range a:b.

  6. Print the results:

    println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
    println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", "))

    These lines print the first 25 pernicious numbers and the pernicious numbers in the range from 888888877 to 888888888.

In summary, this code defines functions to work with pernicious numbers and demonstrates their usage by printing the first 25 pernicious numbers and the pernicious numbers within a specified range.

Source code in the julia programming language

using Primes

ispernicious(n::Integer) = isprime(count_ones(n))
nextpernicious(n::Integer) = begin n += 1; while !ispernicious(n) n += 1 end; return n end
function perniciouses(n::Int)
    rst = Vector{Int}(n)
    rst[1] = 3
    for i in 2:n
        rst[i] = nextpernicious(rst[i-1])
    end
    return rst
end
perniciouses(a::Integer, b::Integer) = filter(ispernicious, a:b)

println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", "))


  

You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the D programming language
You may also check:How to resolve the algorithm Dot product step by step in the Octave programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Sylvester's sequence step by step in the Python programming language
You may also check:How to resolve the algorithm Descending primes step by step in the jq programming language