Skip to content

Commit 04c7705

Browse files
committed
Refined prime power and perfect power checkers
1 parent 8fcda61 commit 04c7705

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ Manifest.toml
2828

2929
# miscellaneous stuff
3030
other/
31+
PRIME_README/

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ version = "0.2.3"
55

66
[deps]
77
FLoops = "cc61a311-1640-44b5-9fba-1b764f453329"
8+
Hecke = "3e1990a7-5d81-5526-99ce-9ba3ff248f21"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
10-
Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a"
1111
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
1212
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
1313
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

src/CodingTheory.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
module CodingTheory
88

9-
using Nemo: isprime, factor, fmpz
9+
# using Nemo: isprime, factor, fmpz # for prime power function
10+
using Hecke: ispower, isprime_power # for perfect power function
1011
# using Primes: isprime, primes
1112
using LinearAlgebra: I
1213
using FLoops: @floop, ThreadedEx

src/powers.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
# Perfect power function
2+
3+
@doc raw"""
4+
```julia
5+
isperfectpower(n::Integer) -> Bool
6+
```
7+
8+
Given an integer `n`, returns `true` or `false` depending on whether or not it is a perfect power.
9+
10+
Here, a perfect power is some number of the form ``a^b``, where ``a, b \in \mathbb{N}``, and ``b > 1``.
11+
12+
This function is a wrapper around [Hecke.jl's really excellent and efficient `ispower` function](https://github.com/thofma/Hecke.jl/blob/master/src/Misc/Integer.jl#L413-L443).
13+
14+
!!! note
15+
16+
It should be noted that there is another defintion for "perfect powers":
17+
> some number of the form ``p^b``, where ``p, b \in \mathbb{N}``, ``b > 1``, *and ``p`` is a prime number``.
18+
Here, we call numbers of this form *prime powers*, or *proper perfect powers*. By this definition, 36 is *not* a perfect power, because 6 is not prime.
19+
"""
20+
function isperfectpower(n::Integer)
21+
(n == 0) && return false
22+
e = first(Hecke.ispower(n))
23+
return (e != 1 && e != 0)
24+
end
25+
26+
"""
27+
```julia
28+
isprimepower(n::Integer) -> Bool
29+
```
30+
31+
This function is a wrapper around [Hecke.jl's really excellent and effcient `ispower` function](https://github.com/thofma/Hecke.jl/blob/master/src/Misc/Integer.jl#L756-L769).
32+
33+
"""
34+
function isprimepower(n::Integer)
35+
return isprime_power(n)
36+
end
37+
38+
## Prime power function
39+
40+
#=
141
function __isprime(n)
242
return isprime(fmpz(n))
343
end
@@ -39,10 +79,14 @@ function Ω(n)
3979
return sum(e for (__, e) ∈ __factors(n))
4080
end
4181
82+
# this is a slow version compared to the Hecke one
4283
function isperfectpower(n)
4384
return ω(n) == 1 && Ω(n) != 1
4485
end
4586
87+
# this is slower than the Hecke verson
4688
function isprimepower(n)
4789
return ω(n) == 1
4890
end
91+
92+
=#

src/utils.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Check that all elements in a list are equal to each other.
5555
end
5656
@inline allequal(a::T...) where {T} = allequal(a)
5757

58+
@inline function allequal2(A)
59+
return !A=A[1:1]==A∪A
60+
end
61+
5862
"""
5963
```julia
6064
aredistinct(A) -> Bool

src/wip/primes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ function perfectpower_brute_force(n::Integer)
9292
n ≤ 1 && return nothing
9393

9494
for a in 1:n
95-
for m in 1:n
96-
isequal(a^m, n) && return a, m
95+
for m in 2:n
96+
isequal(a^m, n) && return m, a
9797
end
9898
end
9999

0 commit comments

Comments
 (0)