Skip to content

Commit f323bff

Browse files
committed
Added better isprimepower method; put DJB algorithms to the side
1 parent 725a4b3 commit f323bff

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

src/CodingTheory.jl

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

77
module CodingTheory
88

9-
using Primes: isprime, primes
9+
using Nemo: isprime, factor, fmpz
10+
# using Primes: isprime, primes
1011
using LinearAlgebra: I
1112
using FLoops: @floop, ThreadedEx
1213
using Polynomials, StaticArrays
@@ -41,12 +42,15 @@ export FinitePolynomial, list_polys, multiplication_table, list_span, islinear,
4142
isirreducible, normal_form!, normal_form, equivalent_code!, equivalent_code,
4243
generator!, generator, parity_check, syndrome, isincode
4344

45+
# Powers
46+
export isprimepower, isperfectpower
47+
4448
# Levenshtein
4549
export levenshtein, levenshtein!
4650

4751
include("abstract_types.jl")
4852
include("rref.jl")
49-
include("messages.jl") # implicitly exports distance.jl and primes.jl
53+
include("messages.jl") # implicitly exports distance.jl and powers.jl
5054
include("bounds.jl")
5155
include("algebra.jl")
5256
include("levenshtein.jl")

src/messages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
=#
66

77
include("distance.jl")
8-
include("primes.jl")
8+
include("powers.jl")
99

1010
"""
1111
```julia

src/powers.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function __isprime(n)
2+
return isprime(fmpz(n))
3+
end
4+
5+
function __factors(n)
6+
return n == 0 ? [] : factor(fmpz(n))
7+
end
8+
# function __factors(n)
9+
# first(i) for i in Primes.factor(n)
10+
# end
11+
12+
function primedivisors(n)
13+
n == 0 && return []
14+
__isprime(n) && return [fmpz(n)]
15+
f = __factors(n)
16+
return sort!([p for (p, e) f])
17+
end
18+
19+
function ω(n)
20+
nprimedivisors = 0
21+
if n == 0
22+
nprimedivisors = 0
23+
elseif __isprime(n)
24+
nprimedivisors = 1
25+
else
26+
nprimedivisors = length(__factors(n))
27+
end
28+
29+
return fmpz(nprimedivisors)
30+
31+
# # original definition:
32+
# return fmpz(length(primedivisors(n)))
33+
end
34+
35+
function Ω(n)
36+
n == fmpz(0) && return 0
37+
__isprime(n) && return fmpz(1)
38+
39+
return sum(e for (__, e) __factors(n))
40+
end
41+
42+
function isperfectpower(n)
43+
return ω(n) == 1 && Ω(n) != 1
44+
end
45+
46+
function isprimepower(n)
47+
return ω(n) == 1
48+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### TAKE TWO
2+
13
using Primes: primes, factor, isprime
24

35
### Helpers

src/primes.jl renamed to src/wip/primes.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,55 @@
44
"${BASH_SOURCE[0]}" "$@"
55
=#
66

7+
# TAKE ONE
8+
9+
using Nemo, Primes
10+
11+
function _isprime(n)
12+
return Nemo.isprime(Nemo.fmpz(n))
13+
end
14+
15+
function _factors(n)
16+
return n == 0 ? [] : Nemo.factor(Nemo.fmpz(n))
17+
end
18+
# function _factors(n)
19+
# first(i) for i in Primes.factor(n)
20+
# end
21+
22+
function primedivisors(n)
23+
n == 0 && return []
24+
_isprime(n) && return [Nemo.fmpz(n)]
25+
f = _factors(n)
26+
return sort!([p for (p, e) ∈ f])
27+
end
28+
29+
function ω(n)
30+
nprimedivisors = 0
31+
if n == 0
32+
nprimedivisors = 0
33+
end
34+
if _isprime(n)
35+
nprimedivisors = length(Nemo.fmpz(n))
36+
end
37+
38+
return length(_factors(n))
39+
40+
# return fmpz(length(primedivisors(n)))
41+
end
42+
43+
function Ω(n)
44+
n == fmpz(0) && return 0
45+
_isprime(n) && return fmpz(1)
46+
47+
return sum(e for (_, e) ∈ _factors(n))
48+
end
49+
50+
function isperfectpower(n)
51+
return ω(n) == 1 && Ω(n) != 1
52+
end
53+
54+
#---------------------------
55+
756
#=
857
You find the smallest pair (a, n) (small with respect to n) such that a^n is your target number. Then you need to check if a is prime.
958
=#

0 commit comments

Comments
 (0)