How to resolve the algorithm Sphenic numbers step by step in the Julia programming language
How to resolve the algorithm Sphenic numbers step by step in the Julia programming language
Table of Contents
Problem Statement
A sphenic number is a positive integer that is the product of three distinct prime numbers. More technically it's a square-free 3-almost prime (see Related tasks below). For the purposes of this task, a sphenic triplet is a group of three sphenic numbers which are consecutive. Note that sphenic quadruplets are not possible because every fourth consecutive positive integer is divisible by 4 (= 2 x 2) and its prime factors would not therefore be distinct. 30 (= 2 x 3 x 5) is a sphenic number and is also clearly the first one. [1309, 1310, 1311] is a sphenic triplet because 1309 (= 7 x 11 x 17), 1310 (= 2 x 5 x 31) and 1311 (= 3 x 19 x 23) are 3 consecutive sphenic numbers. Calculate and show here:
- All sphenic numbers less than 1,000.
- All sphenic triplets less than 10,000.
- How many sphenic numbers are there less than 1 million?
- How many sphenic triplets are there less than 1 million?
- What is the 200,000th sphenic number and its 3 prime factors?
- What is the 5,000th sphenic triplet? Hint: you only need to consider sphenic numbers less than 1 million to answer 5. and 6.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sphenic numbers step by step in the Julia programming language
1. Introduction
This code is written in Julia programming language and it solves the following problem: Find all sphenic numbers and sphenic triplets less than 1 million.
Definition: A sphenic number is a natural number that is the product of three distinct prime numbers. A sphenic triplet is a set of three consecutive sphenic numbers.
2. Code Overview
The code can be divided into the following parts:
-
Functions:
issphenic(n)
: Checks if the given numbern
is a sphenic number.issphenictriple(n)
: Checks if the given numbern
is a part of a sphenic triplet.printlntriple(n)
: Prints the given numbern
as a sphenic triplet (in the form(n, n+1, n+2)
).
-
Main Program:
-
Computes all sphenic numbers less than 1 million and stores them in the set
shenums
. -
Computes all sphenic triplets less than 1 million and stores them in the set
shetrip
. -
Prints the sphenic numbers and sphenic triplets based on the requirements of the problem.
-
3. Functions in Detail
3.1. issphenic(n)
Function:
- Checks if the given number
n
is a sphenic number. - Uses a loop to find the prime factors of
n
. - If
n
has exactly 3 distinct prime factors, it returnstrue
. - Otherwise, it returns
false
.
3.2. issphenictriple(n)
Function:
- Checks if the given number
n
is a part of a sphenic triplet. - Calls the
issphenic()
function to check ifn
,n+1
, andn+2
are all sphenic numbers. - Returns
true
if all three numbers are sphenic, otherwise returnsfalse
.
3.3. printlntriple(n)
Function:
- Prints the given number
n
as a sphenic triplet (in the form(n, n+1, n+2)
).
4. Main Program in Detail
- Computes all sphenic numbers less than 1 million using the
filter(issphenic, 2:1_000_000)
expression. - Computes all sphenic triplets less than 1 million using the
filter(issphenictriple, 2:1_000_000)
expression. - Prints the first 15 sphenic numbers, all sphenic triplets less than 10,000, and the answers to the remaining requirements of the problem.
5. Example Output
Sphenic numbers less than 1,000:
[30, 42, 66, 70, 78, 102, 105, 110, 114, 130, 138, 154, 165, 170, 174, 182, 186, 190, 195, 222, 230, 231, 238, 258, 266, 273, 282, 285, 286, 290, 306, 310, 318, 322, 345, 354, 357, 366, 370, 378, 385, 390, 399, 406, 410, 418, 426, 429, 434, 435, 442, 455, 462, 465, 470, 474, 483, 494, 495, 498, 506, 510, 522, 525, 530, 534, 546, 550, 558, 561, 570, 574, 575, 585, 590, 594, 595, 602, 609, 610, 618, 627, 633, 634, 638, 645, 646, 650, 651, 654, 658, 663, 670, 678, 682, 685, 690, 694, 702, 705, 706, 710, 714, 715, 722, 726, 730, 734, 735, 741, 742, 754, 759, 765, 770, 774, 786, 790, 795, 798, 805, 814, 822, 825, 826, 830, 834, 843, 854, 855, 858, 861, 870, 874, 875, 882, 885, 890, 891, 894, 903, 906, 910, 915, 922, 925, 930, 931, 934, 938, 951, 954, 957, 962, 966, 970, 974, 975, 987, 990, 994, 995, 999]
Sphenic triplets less than 10,000:
(30, 31, 32)
(42, 43, 44)
(66, 67, 68)
(70, 71, 72)
(102, 103, 104)
(105, 10
<div id="sourcecode"/>
## Source code in the julia programming language
{% raw %}
```julia
const SPHENIC_NUMBERS = Set{Int64}()
const NOT_SPHENIC_NUMBERS = Set{Int64}()
function issphenic(n::Int64)
n in SPHENIC_NUMBERS && return true
n in NOT_SPHENIC_NUMBERS && return false
nin = n
sqn = isqrt(nin)
npfactors = 0
isrepeat = false
i = 2
while n > 1 && !(npfactors == 0 && i >= sqn)
if n % i == 0
npfactors += 1
if isrepeat || npfactors > 3
push!(NOT_SPHENIC_NUMBERS, nin)
return false
end
isrepeat = true
n ÷= i
continue
end
i += 1
isrepeat = false
end
if npfactors < 3
push!(NOT_SPHENIC_NUMBERS, nin)
return false
end
push!(SPHENIC_NUMBERS, nin)
return true
end
issphenictriple(n::Integer) = issphenic(n) && issphenic(n+1) && issphenic(n+2)
printlntriple(n::Integer) = println("($(n), $(n+1), $(n+2))")
shenums = filter(issphenic, 2:1_000_000)
shetrip = filter(issphenictriple, 2:1_000_000)
# 1. All sphenic numbers less than 1,000.
println("Sphenic numbers less than 1,000:")
less1000 = filter(<(1000), shenums)
foreach(println, Iterators.partition(less1000, 15))
# 2. All sphenic triplets less than 10,000.
println("Sphenic triplets less than 10,000:")
less10000 = filter(<(10_000 - 6), shetrip)
foreach(printlntriple, less10000)
# 3. How many sphenic numbers are there less than 1 million?
println("Number of sphenic numbers that are less than 1 million: ", length(shenums))
# 4. How many sphenic triplets are there less than 1 million?
println("Number of sphenic triplets that are less than 1 million: ", length(shetrip))
# 5. What is the 200,000th sphenic number and its 3 prime factors?
println("The 200,000th sphenic number is: ", shenums[200_000])
# 6. What is the 5,000th sphenic triplet?
print("The 5,000h sphenic triplet is: ")
printlntriple(shetrip[5_000])
{% endraw %}
You may also check:How to resolve the algorithm Jensen's Device step by step in the Huginn programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the C# programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the Wren programming language
You may also check:How to resolve the algorithm Infinity step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the Go programming language