How to resolve the algorithm Strong and weak primes step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Strong and weak primes step by step in the Julia programming language

Table of Contents

Problem Statement

Note that the definition for   strong primes   is different when used in the context of   cryptography.

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strong and weak primes step by step in the Julia programming language

This Julia code finds and analyzes strong, weak, and balanced prime numbers. Here's a detailed explanation:

  1. Library Imports:

    • using Primes, Formatting: Imports the Primes module for prime number functions and the Formatting module for formatting numerical output.
  2. parseprimelist Function:

    • The program defines a function named parseprimelist that performs the following tasks:
  3. Generating Prime List:

    • It uses the primes() function to generate a list of prime numbers between 2 and 1,000,001,9. This list is stored in the primelist vector.
  4. Initializing Vectors:

    • Three vectors, strongs, weaks, and balanceds, are initialized to store strong, weak, and balanced prime numbers, respectively.
  5. Iterating over the Prime List:

    • The code iterates over each prime number p in the primelist vector.
  6. Calculating Midpoint:

    • For each p, it calculates the midpoint x as the average of the previous and next prime numbers in the list ((primelist[n - 1] + primelist[n + 1]) / 2).
  7. Classifying Primes:

    • Based on the comparison between x and p, the prime is classified as follows:
      • p is a strong prime if x > p.
      • p is a weak prime if x < p.
      • p is a balanced prime if x = p.
  8. Storing Primes:

    • The classified primes are pushed into the corresponding vectors (strongs, weaks, and balanceds).
  9. Printing Results:

    • After processing all the primes, the code prints various statistics and information:
      • The first 36 strong primes and the count of strong primes less than 1 million and 10 million.
      • The first 37 weak primes and the count of weak primes less than 1 million and 10 million.
      • The first 28 balanced primes and the count of balanced primes less than 1 million and 10 million.
  10. Calling the Function:

  • The parseprimelist function is called to execute the above tasks.

Source code in the julia programming language

using Primes, Formatting

function parseprimelist()
    primelist = primes(2, 10000019)
    strongs = Vector{Int64}()
    weaks = Vector{Int64}()
    balanceds = Vector{Int64}()
    for (n, p) in enumerate(primelist)
        if n == 1 || n == length(primelist)
            continue
        end
        x = (primelist[n - 1] + primelist[n + 1]) / 2
        if x > p
            push!(weaks, p)
        elseif x < p 
            push!(strongs, p)
        else
            push!(balanceds, p)
        end
    end
    println("The first 36 strong primes are: ", strongs[1:36])
    println("There are ", format(sum(map(x -> x < 1000000, strongs)), commas=true), " stromg primes less than 1 million.")
    println("There are ", format(length(strongs), commas=true), " strong primes less than 10 million.")    
    println("The first 37 weak primes are: ", weaks[1:37])
    println("There are ", format(sum(map(x -> x < 1000000, weaks)), commas=true), " weak primes less than 1 million.")
    println("There are ", format(length(weaks), commas=true), " weak primes less than 10 million.")    
    println("The first 28 balanced primes are: ", balanceds[1:28])
    println("There are ", format(sum(map(x -> x < 1000000, balanceds)), commas=true), " balanced primes less than 1 million.")
    println("There are ", format(length(balanceds), commas=true), " balanced primes less than 10 million.")    
end

parseprimelist()


  

You may also check:How to resolve the algorithm Unix/ls step by step in the Java programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the REBOL programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the R programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the C# programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Forth programming language