3 ms·
Well, in Julia you can use good old goto :) using Combinatorics # Initialization numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] primes = [3, 5, 7, 11, 13, 17
by plafl 6y ago
Well, in Julia you can use good old goto :)
using Combinatorics
# Initialization
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
primes = [3, 5, 7, 11, 13, 17]
winners = []
# Loop though the permutations, collecting only prime pairs
for p in permutations(numbers)
for i in 2:9
if !(p[i-1] + p[i] in primes)
@goto nextpermutation
end
end
push!(winners, p)
@label nextpermutation
end
If I were to use a non-brute force approach I would consider computing all the hamiltonian paths of a graph where each vertex is a number and two vertices are connected only if their sum is a prime number
- newen 6y agoGotos can be so useful in situations like this. I do not like the trend in new languages of not allowing goto and instead make the programmer do gymnastics to implement relatively straightforward logic.