How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the Julia programming language

Table of Contents

Problem Statement

The following definitions are needed for this task. A tetraprime is a positive integer which is the product of four distinct primes. For example, 1155 is a tetraprime because 1155 = 3 x 5 x 7 x 11. The neighboring pairs of a prime are the two consecutive numbers immediately preceding or immediately following that prime. For example, (5, 6) and (8, 9) are the neighboring pairs of the prime 7.

  1. Find and show here all primes less than 100,000 whose preceding neighboring pair are both tetraprimes.
  2. Find and show here all primes less than 100,000 whose following neighboring pair are both tetraprimes.
  3. Of the primes in 1 and 2 above how many have a neighboring pair one of whose members has a prime factor of 7?
  4. For the primes in 1 and 2 above, consider the gaps between consecutive primes. What are the minimum, median and maximum gaps?
  5. Repeat these calculations for all primes less than 1 million but for 1 and 2 just show the number of primes - don't print them out individually. If it is difficult for your language to meet all of these requirements, then just do what you reasonably can.

Repeat the calculations for all primes less than 10 million.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the Julia programming language

Importing Libraries

The code starts by importing the Statistics and Primes libraries, which provide functions for statistical analysis and working with prime numbers, respectively.

Main Function

The main content of the code is wrapped in a let block, which creates a local scope for the variables defined inside it.

Prime Number Generation

The code generates a list of prime numbers up to 1 million (primes1M).

Filtering for Tetraprime Neighbors

A function istetraprime(n) is defined to check if a given number n is a tetraprime. A tetraprime is a prime number whose neighboring four numbers (two on each side) are also prime.

Two functions, are_preceding_tetraprimes(n, cnt) and are_following_tetraprimes(n, cnt), are defined to check if the cnt preceding or following numbers of n are all tetraprimes.

Using these functions, the code filters the list of primes to find those with tetraprime neighbors. The resulting lists are assigned to pre1M and fol1M.

Filtering for a Specific Range

Two more lists, pre100k and fol100k, are created by filtering the pre1M and fol1M lists to include only primes less than 100,000.

Primes with a Neighboring Factor of 7

Two additional lists, pre1M_with7 and fol1M_with7, are created by filtering the pre1M and fol1M lists to include only primes that have a neighboring prime with a factor of 7. Similarly, pre100k_with7 and fol100k_with7 are created for primes less than 100,000.

Calculating Prime Gaps

The code calculates the gaps between consecutive primes in the pre1M and fol1M lists. These gaps are stored in the lists p_gaps1M and f_gaps1M, respectively. Similar lists, p_gaps100k and f_gaps100k, are created for primes less than 100,000.

Statistical Analysis of Prime Gaps

Statistical measures (minimum, median, and maximum) are calculated for the prime gaps. These are assigned to variables with names like pmin1M, pmedian1M, pmax1M, and so on.

Printing Results

The code enters a loop to print the results for each of the four sets of prime lists: pre100k, fol100k, pre1M, and fol1M.

For each set, it prints the number of primes found, the maximum value considered, whether the neighbors are preceding or following, and the minimum, median, and maximum gaps between those primes.

For the smaller sets (pre100k and fol100k), it also prints the primes in a table.

Additionally, it counts the number of primes with a neighboring prime with a factor of 7 and prints that information.

Source code in the julia programming language

""" rosettacode.org/wiki/Prime_numbers_whose_neighboring_pairs_are_tetraprimes """

using Statistics
using Primes

istetraprime(n) = (a = map(last, factor(n).pe); length(a) == 4 && all(==(1), a))
are_following_tetraprimes(n, cnt = 2) = all(istetraprime, n+1:n+cnt)
are_preceding_tetraprimes(n, cnt = 2) = all(istetraprime, n-cnt:n-1)

let
    primes1M = primes(10^7)
    pre1M = filter(are_preceding_tetraprimes, primes1M)       
    fol1M = filter(are_following_tetraprimes, primes1M)
    pre100k = filter(<(100_000), pre1M)
    fol100k = filter(<(100_000), fol1M)

    pre1M_with7 = filter(i -> any(k -> (i - k) % 7 == 0, 1:2), pre1M)
    fol1M_with7 = filter(i -> any(k -> (i + k) % 7 == 0, 1:2), fol1M)
    pre100k_with7 = filter(<(100_000), pre1M_with7)
    fol100k_with7 = filter(<(100_000), fol1M_with7)

    p_gaps1M = [pre1M[i] - pre1M[i - 1] for i in 2:lastindex(pre1M)]
    f_gaps1M = [fol1M[i] - fol1M[i - 1] for i in 2:lastindex(fol1M)]
    p_gaps100k = [pre1M[i] - pre1M[i - 1] for i in 2:lastindex(pre1M) if pre1M[i] < 100_000]
    f_gaps100k = [fol1M[i] - fol1M[i - 1] for i in 2:lastindex(fol1M) if fol1M[i] < 100_000]

    pmin1M, pmedian1M, pmax1M = minimum(p_gaps1M), median(p_gaps1M), maximum(p_gaps1M)
    fmin1M, fmedian1M, fmax1M = minimum(f_gaps1M), median(f_gaps1M), maximum(f_gaps1M)
    pmin100k, pmedian100k, pmax100k = minimum(p_gaps100k), median(p_gaps100k), maximum(p_gaps100k)
    fmin100k, fmedian100k, fmax100k = minimum(f_gaps100k), median(f_gaps100k), maximum(f_gaps100k)

    for (tet, s, s2, tmin, tmed, tmax, t7) in [
        (pre100k, "100,000", "preceding", pmin100k, pmedian100k, pmax100k, pre100k_with7),
        (fol100k, "100,000", "following", fmin100k, fmedian100k, fmax100k, fol100k_with7),
        (pre1M, "1,000,000", "preceding", pmin1M, pmedian1M, pmax1M, pre1M_with7),
        (fol1M, "1,000,000", "following", fmin1M, fmedian1M, fmax1M, fol1M_with7),
    ]
        print("Found $(length(tet)) primes under $s whose $s2 neighboring pair are tetraprimes")
        if s == "100,000"
            println(":")
            foreach(p -> print(rpad(p[2], 6), p[1] % 10 == 0 ? "\n" : ""), enumerate(tet))
            println()
        else
            println(".")
        end
        println("Minimum, median, and maximum gaps between those primes: $tmin $tmed $tmax")
        println("Of those primes, $(length(t7)) have a neighboring pair one of whose factors is 7.\n")
    end
end

  

You may also check:How to resolve the algorithm Index finite lists of positive integers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the Racket programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Permutations/Derangements step by step in the GAP programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the F# programming language