-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_41.rb
48 lines (45 loc) · 873 Bytes
/
prob_41.rb
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
44
45
46
47
=begin
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?
=end
def ist_prim(x)
if ((x+1) % 6) == 0 or ((x-1) % 6) == 0
i = 0
for i in 2..Math.sqrt(x).floor
if x % i == 0
return 0
end
end
return 1
else
return 0
end
end
n = 2
i = 0
j = 0
z = 0
produkt = 1
a = Array.new
for n in 3..9
for i in ((10**(n-1))+1)..((10**n)-3)
for j in 1..n
a[j] = 0
end
for j in 1..n
z = i.to_s[j-1].to_i
if z <= n and z > 0
a[z] += 1
end
end
for j in 1..n
produkt *= a[j]
end
if produkt == 1
if ist_prim(i) == 1
puts "#{i} ist pandigital UND prim!"
end
end
produkt = 1
end
end