-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathp32.sh
45 lines (41 loc) · 995 Bytes
/
p32.sh
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
#!/bin/bash
# Store sum and seen products
sum=0
products=
# Check all equations for a given permutation of digits
check()
{
# Inputs are length i and j. Product must be >= max length of i and j, so only use range [1, 4]
for ((i=1; i <= 4; i++)); do
a=${1:0:i}
for ((j=1; j <= 4; j++)); do
b=${1:i:j}
c=${1:i+j}
product=$(( $a * $b ))
if [[ $product = $c ]]; then
if [[ !(${products[product]} = 1) ]]; then
echo "$a * $b = $c"
sum=$((sum+product))
products[product]=1
fi
fi
done
done
}
# Check all permutations of digits
permute()
{
local i
local c
if [[ ${#1} > 0 ]]; then
for ((i=0; i < ${#1}; i++)); do
c=${1:i:1}
permute "${1/$c/''}" "$2$c"
done
else
check $2
fi
}
# Sum all pandigital products
permute "123456789"
echo "Sum: $sum"