How to resolve the algorithm Sexy primes step by step in the Julia programming language
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:
-
Importing the Primes Module:
- The code begins by importing the
Primes
module, which provides functions for working with prime numbers in Julia.
- The code begins by importing the
-
nextby6
Function:- This function takes two arguments:
n
(an index) anda
(an array of primes). - It calculates
possiblenext
asa[n] + 6
. - It iteratively checks if
possiblenext
exists asa[i]
,a[j]
, ora[k]
fori
,j
, andk
being consecutive indices aftern
. - If found, it returns the index of the match; otherwise, it returns
n
.
- This function takes two arguments:
-
lastones
Function:- This function takes two arguments:
dict
(a dictionary) andn
(a number). - It extracts the keys from
dict
, sorts them, and stores them in thearr
array. - It calculates
beginidx
based on the values ofn
and the length ofarr
. - It returns a subarray of
arr
starting frombeginidx
.
- This function takes two arguments:
-
lastoneslessthan
Function:- This function takes three arguments:
dict
(a dictionary),n
(a number), andceiling
(a threshold). - It filters the keys of
dict
to include only those less thanceiling
. - It calls
lastones
to get the lastn+3
keys and then applies another filter to exclude keys greater than or equal toceiling
. - Finally, it returns the last
n
keys.
- This function takes three arguments:
-
primesbysexiness
Function:- This function takes one argument:
x
(a limit). - It initializes four dictionaries:
twins
,triplets
,quadruplets
, andquintuplets
, which will store the starting indices of consecutive prime number groups. - It calculates
possibles
, which represents the list of prime numbers up tox + 30
. - It filters
possibles
to retain only primes less than or equal tox - 6
and stores them insingles
. - It initializes an unordered dictionary
unsexy
with the primes fromsingles
as keys. This dictionary will track unsexy primes. - It iterates through the primes in
singles
usingfor
loop:- It calculates the next prime index
twinidx
that is 6 more than the current indexi
. - If
twinidx
is greater thani
, it means that the prime at indexi
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
, andquintuplets
if the previous condition is met.
- It calculates the next prime index
- 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
usinglastoneslessthan
. - It displays the last 5 triplet, quadruplet, and quintuplet primes less than
x
usinglastones
. - It prints the count of unsexy primes less than
x
and displays the last 10 unsexy primes. - Finally, it calls the
primesbysexiness
function with1000035
as the limit.
- This function takes one argument:
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