How to resolve the algorithm Sexy primes step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Sexy primes step by step in the Julia programming language

Table of Contents

Problem Statement

In mathematics, sexy primes are prime numbers that differ from each other by six. For example, the numbers 5 and 11 are both sexy primes, because 11 minus 6 is 5. The term "sexy prime" is a pun stemming from the Latin word for six: sex. Sexy prime pairs: Sexy prime pairs are groups of two primes that differ by 6. e.g. (5 11), (7 13), (11 17) See sequences: OEIS:A023201 and OEIS:A046117 Sexy prime triplets: Sexy prime triplets are groups of three primes where each differs from the next by 6. e.g. (5 11 17), (7 13 19), (17 23 29) See sequences: OEIS:A046118, OEIS:A046119 and OEIS:A046120 Sexy prime quadruplets: Sexy prime quadruplets are groups of four primes where each differs from the next by 6. e.g. (5 11 17 23), (11 17 23 29) See sequences: OEIS:A023271, OEIS:A046122, OEIS:A046123 and OEIS:A046124 Sexy prime quintuplets: Sexy prime quintuplets are groups of five primes with a common difference of 6. One of the terms must be divisible by 5, because 5 and 6 are relatively prime. Thus, the only possible sexy prime quintuplet is (5 11 17 23 29)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sexy primes step by step in the Julia programming language

The provided Julia code finds and displays statistics about prime numbers satisfying certain criteria of "sexiness" up to a specified limit. Here's a detailed explanation of the code:

  1. Importing the Primes Module:

    • The code begins by importing the Primes module, which provides functions for working with prime numbers in Julia.
  2. nextby6 Function:

    • This function takes two arguments: n (an index) and a (an array of primes).
    • It calculates possiblenext as a[n] + 6.
    • It iteratively checks if possiblenext exists as a[i], a[j], or a[k] for i, j, and k being consecutive indices after n.
    • If found, it returns the index of the match; otherwise, it returns n.
  3. lastones Function:

    • This function takes two arguments: dict (a dictionary) and n (a number).
    • It extracts the keys from dict, sorts them, and stores them in the arr array.
    • It calculates beginidx based on the values of n and the length of arr.
    • It returns a subarray of arr starting from beginidx.
  4. lastoneslessthan Function:

    • This function takes three arguments: dict (a dictionary), n (a number), and ceiling (a threshold).
    • It filters the keys of dict to include only those less than ceiling.
    • It calls lastones to get the last n+3 keys and then applies another filter to exclude keys greater than or equal to ceiling.
    • Finally, it returns the last n keys.
  5. primesbysexiness Function:

    • This function takes one argument: x (a limit).
    • It initializes four dictionaries: twins, triplets, quadruplets, and quintuplets, which will store the starting indices of consecutive prime number groups.
    • It calculates possibles, which represents the list of prime numbers up to x + 30.
    • It filters possibles to retain only primes less than or equal to x - 6 and stores them in singles.
    • It initializes an unordered dictionary unsexy with the primes from singles as keys. This dictionary will track unsexy primes.
    • It iterates through the primes in singles using for loop:
      • It calculates the next prime index twinidx that is 6 more than the current index i.
      • If twinidx is greater than i, it means that the prime at index i is part of a twin prime pair.
      • It removes the current prime and its twin from the unsexy dictionary.
      • It stores the twin indices in the twins dictionary.
      • It checks for triplet, quadruplet, and quintuplet primes by repeating the same process for triplets, quadruplets, and quintuplets if the previous condition is met.
    • It prints the count of twin, triplet, quadruplet, and quintuplet primes less than x.
    • It displays the last 5 twin primes less than x - 6 using lastoneslessthan.
    • It displays the last 5 triplet, quadruplet, and quintuplet primes less than x using lastones.
    • It prints the count of unsexy primes less than x and displays the last 10 unsexy primes.
    • Finally, it calls the primesbysexiness function with 1000035 as the limit.

In summary, this code finds and presents detailed statistics about the distribution of twin, triplet, quadruplet, and quintuplet primes, as well as unsexy primes, up to a specified limit.

Source code in the julia programming language

using Primes

function nextby6(n, a)
    top = length(a)
    i = n + 1
    j = n + 2
    k = n + 3
    if n >= top
        return n
    end
    possiblenext = a[n] + 6
    if i <= top && possiblenext == a[i]
        return i
    elseif j <= top && possiblenext == a[j]
        return j
    elseif k <= top && possiblenext == a[k]
        return k
    end
    return n
end

function lastones(dict, n)
    arr = sort(collect(keys(dict)))
    beginidx = max(1, length(arr) - n + 1)
    arr[beginidx: end]
end

function lastoneslessthan(dict, n, ceiling)
    arr = filter(y -> y < ceiling, lastones(dict, n+3))
    beginidx = max(1, length(arr) - n + 1)
    arr[beginidx: end]
end

function primesbysexiness(x)
    twins = Dict{Int64, Array{Int64,1}}()
    triplets = Dict{Int64, Array{Int64,1}}()
    quadruplets = Dict{Int64, Array{Int64,1}}()
    quintuplets = Dict{Int64, Array{Int64,1}}()
    possibles = primes(x + 30)
    singles = filter(y -> y <= x - 6, possibles)
    unsexy = Dict(p => true for p in singles)
    for (i, p) in enumerate(singles)
        twinidx = nextby6(i, possibles)
        if twinidx > i
            delete!(unsexy, p)
            delete!(unsexy, p + 6)
            twins[p] = [i, twinidx]
            tripidx = nextby6(twinidx, possibles)
            if tripidx > twinidx
                triplets[p] = [i, twinidx, tripidx]
                quadidx = nextby6(tripidx, possibles)
                if quadidx > tripidx
                    quadruplets[p] = [i, twinidx, tripidx, quadidx]
                    quintidx = nextby6(quadidx, possibles)
                    if quintidx > quadidx
                        quintuplets[p] = [i, twinidx, tripidx, quadidx, quintidx]
                    end
                end
            end
        end
    end
    # Find and display the count of each group
    println("There are:\n$(length(twins)) twins,\n",
            "$(length(triplets)) triplets,\n",
            "$(length(quadruplets)) quadruplets, and\n",
            "$(length(quintuplets)) quintuplets less than $x.")
    println("The last 5 twin primes start with ", lastoneslessthan(twins, 5, x - 6))
    println("The last 5 triplet primes start with ", lastones(triplets, 5))
    println("The last 5 quadruplet primes start with ", lastones(quadruplets, 5))
    println("The quintuplet primes start with ", lastones(quintuplets, 5))
    println("There are $(length(unsexy)) unsexy primes less than $x.")
    lastunsexy = sort(collect(keys(unsexy)))[length(unsexy) - 9: end]
    println("The last 10 unsexy primes are: $lastunsexy")
end

primesbysexiness(1000035)


  

You may also check:How to resolve the algorithm Longest common subsequence step by step in the Haskell programming language
You may also check:How to resolve the algorithm Bitcoin/public point to address step by step in the Wren programming language
You may also check:How to resolve the algorithm Tropical algebra overloading step by step in the Factor programming language
You may also check:How to resolve the algorithm Esthetic numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Logo programming language