How to resolve the algorithm Penta-power prime seeds step by step in the Julia programming language
Published on 22 June 2024 08:30 PM
How to resolve the algorithm Penta-power prime seeds step by step in the Julia programming language
Table of Contents
Problem Statement
Generate the sequence of penta-power prime seeds: positive integers n such that:
I can find no mention or record of this sequence anywhere. Perhaps I've invented it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Penta-power prime seeds step by step in the Julia programming language
The provided code is written in Julia programming language and it is designed to perform operations on pentagonal power prime seeds. Here's a detailed explanation of what the code does:
-
Importing Libraries:
using Primes, Printf
: This line imports thePrimes
module for working with prime numbers and thePrintf
module for formatted printing.
-
Function
ispenta
:- This function determines if a given number
n
is a pentagonal power prime seed. It does this by checking ifn^i + n + 1
is prime fori
in the range 0 to 4. - If all these values are prime, it returns
true
, indicating thatn
is a pentagonal power prime seed.
- This function determines if a given number
-
Function
firstpenta
:- This function generates the first
m
pentagonal power prime seeds. - It uses the
countfrom
iterator to generate an infinite sequence of positive integers starting from 1. - The
filter
iterator is then used to filter out non-pentagonal power prime seeds using theispenta
function. - Finally, the
take
iterator is used to limit the sequence to the firstm
elements, which are collected into an array.
- This function generates the first
-
Function
table_display
:- This function displays a table of numbers arranged in a grid with a specified number of columns.
- It takes two arguments: an array of numbers and the desired number of columns.
- It iterates through the array and prints each number in a table format, ensuring that there are
num_columns
numbers per row.
-
Function
stretch_penta
:- This function generates pentagonal power prime seeds until it reaches a specified goal.
- It uses the same approach as the
firstpenta
function to generate pentagonal power prime seeds using iterators. - However, it uses the
takewhile
iterator to continue generating seeds until the last seed exceeds thegoal
. - The collected seeds are returned as an array.
-
Function
run_rosetta
:- This function serves as the main entry point for the program.
- It calls the
firstpenta
function to generate the first 30 pentagonal power prime seeds and displays them in a table usingtable_display
. - It then calls the
stretch_penta
function to generate pentagonal power prime seeds until they exceed 20,000,000. - For every million milestone (1,000,000, 2,000,000, etc.), it finds the index of the first seed that exceeds the milestone and prints the seed value and its index.
-
Program Execution Check:
- The last part of the code checks if the program is being executed as the main script (
abspath(PROGRAM_FILE) == @__FILE__
). - If so, it calls the
run_rosetta
function to execute the program's main functionality.
- The last part of the code checks if the program is being executed as the main script (
Source code in the julia programming language
using Primes, Printf
function ispenta(n)
all(0:4) do i
isprime(n^i + n + 1)
end
end
function firstpenta(m, T=BigInt)
nums = Iterators.countfrom(T(1))
pentas = Iterators.filter(ispenta, nums)
firstn = Iterators.take(pentas, m)
return collect(firstn)
end
function table_display(nums, num_columns)
num_elements = length(nums)
num_rows = div(num_elements, num_columns)
remaining_elements = num_elements % num_columns
for i in 1:num_rows
for j in 1:num_columns
index = (i - 1) * num_columns + j
print(nums[index], "\t")
end
println()
end
for i in 1:remaining_elements
index = num_rows * num_columns + i
print(nums[index], "\t")
end
println()
end
function stretch_penta(goal, T=BigInt)
nums = Iterators.countfrom(T(1))
pentas = Iterators.filter(ispenta, nums)
firstn = Iterators.takewhile(<=(goal), pentas)
return collect(firstn)
end
function run_rosetta()
fp = firstpenta(30)
println("First 30 Penta power prime seeds:")
table_display(fp, 10)
sp = stretch_penta(20000000)
milestones = 1000000 .* (1:10)
for milestone in milestones
index = findfirst(>(milestone), sp)
@printf "First element over %9i: %9i, index:%4i\n" milestone sp[index] index
end
end
if abspath(PROGRAM_FILE) == @__FILE__
run_rosetta()
end
You may also check:How to resolve the algorithm Fixed length records step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Verhoeff algorithm step by step in the Raku programming language
You may also check:How to resolve the algorithm Nested function step by step in the R programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Wren programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Racket programming language