-
Notifications
You must be signed in to change notification settings - Fork 0
/
p041.jl
44 lines (34 loc) · 1.01 KB
/
p041.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#=
Pandigital prime
Problem 41
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
=#
using Printf
include("utils/permutations.jl")
include("utils/prime_factorization.jl")
function make_int(a::Array)
# concatenate elements of a permutation (an array), into a single string
output = ""
for i in 1:length(a)
output = output * string(a[i])
end
output = parse(Int, output)
#println(output)
return(output)
end
global largest = 1
for n in 1:9
global largest
p = permutations(Array(1:n))
for i in 1:length(p)
thisint = make_int(p[i])
if thisint > largest
if length(prime_factorization(thisint)) == 1 # is prime
println(thisint)
largest = thisint
end
end
end
end
@printf("The largest pandigital prime is %d ", largest)