-
Notifications
You must be signed in to change notification settings - Fork 1
/
p41.wax
55 lines (50 loc) · 1.12 KB
/
p41.wax
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
48
49
50
51
52
53
54
55
(@include math)
(func isPrime (param n int) (result int)
(for i 2 (<= i (cast (call sqrt n) int)) 1 (do
(if (= (% n i) 0) (then
(return 0)
))
))
(return 1)
)
(func permute (param max int) (param digits int) (param n int) (result int)
(if (= digits 0) (then
; Digit permutation is complete; test for primality
(if (call isPrime n) (then
(return n)
)(else
(return 0))
)
)(else
; Return the max of all children (permutations that are prime)
(let best int 0)
(for i 1 (<= i max) 1 (do
(let b int (<< 1 (- i 1)))
(if (& b digits) (then
(let r int (call permute max (^ digits b) (+ i (* n 10))))
(if (> r best) (then
(set best r)
))
))
))
(return best)
))
)
(func findPrimeNPandigital (param max int) (result int)
(let b int 0)
(for i 1 (<= i max) 1 (do
(set b (| b (<< 1 (- i 1))))
))
(return (call permute max b 0))
)
(func main (result int)
; First try 9 digits, then 8, ...
(let digitCount int 9)
(let solution int 0)
(while (= solution 0) (do
(set solution (call findPrimeNPandigital digitCount))
(set digitCount (- digitCount 1))
))
(print solution)
(return 0)
)