I didn't try this approach on large data but it should be able to give you desired output. Frankly speaking, my algorithm seems not optimized nor efficient.
A helper function NextCandidate is implemented to find valid values to be added to the existing path(s), otherwise, the "tree" will be pruned and the processes continues on the branches that survives (say, valid candidates to add can be found)
import itertools
def f(n):
v = list(range(1, n+1))
def NextCandidate(path):
e = path[-1]
c = [i for i in set(v) - set(path) if (i%e)*(e%i)==0]
return [path + [cc] for cc in c] if len(c)>0 else []
p, l = [[x] for x in v], 1
while len(p)>0:
pp = list(itertools.chain(*map(NextCandidate,p)))
if len(pp)>0:
p = pp
l += 1
else:
print(f'length of longest elementary path is: {l}\n')
print(f'desired path sequence: \n')
print(*p, sep = '\n')
break
Examples
length of longest elementary path is: 9
desired path sequence:
[4, 8, 1, 5, 10, 2, 6, 3, 9]
[4, 8, 1, 9, 3, 6, 2, 10, 5]
[4, 8, 2, 6, 3, 9, 1, 10, 5]
[4, 8, 2, 6, 3, 9, 1, 5, 10]
[4, 8, 2, 10, 5, 1, 9, 3, 6]
[4, 8, 2, 10, 5, 1, 6, 3, 9]
[5, 10, 1, 4, 8, 2, 6, 3, 9]
[5, 10, 1, 8, 4, 2, 6, 3, 9]
[5, 10, 1, 9, 3, 6, 2, 8, 4]
[5, 10, 1, 9, 3, 6, 2, 4, 8]
[5, 10, 2, 4, 8, 1, 9, 3, 6]
[5, 10, 2, 4, 8, 1, 6, 3, 9]
[5, 10, 2, 6, 3, 9, 1, 8, 4]
[5, 10, 2, 6, 3, 9, 1, 4, 8]
[5, 10, 2, 8, 4, 1, 9, 3, 6]
[5, 10, 2, 8, 4, 1, 6, 3, 9]
[6, 3, 9, 1, 4, 8, 2, 10, 5]
[6, 3, 9, 1, 5, 10, 2, 8, 4]
[6, 3, 9, 1, 5, 10, 2, 4, 8]
[6, 3, 9, 1, 8, 4, 2, 10, 5]
[8, 4, 1, 5, 10, 2, 6, 3, 9]
[8, 4, 1, 9, 3, 6, 2, 10, 5]
[8, 4, 2, 6, 3, 9, 1, 10, 5]
[8, 4, 2, 6, 3, 9, 1, 5, 10]
[8, 4, 2, 10, 5, 1, 9, 3, 6]
[8, 4, 2, 10, 5, 1, 6, 3, 9]
[9, 3, 6, 1, 4, 8, 2, 10, 5]
[9, 3, 6, 1, 5, 10, 2, 8, 4]
[9, 3, 6, 1, 5, 10, 2, 4, 8]
[9, 3, 6, 1, 8, 4, 2, 10, 5]
[9, 3, 6, 2, 4, 8, 1, 10, 5]
[9, 3, 6, 2, 4, 8, 1, 5, 10]
[9, 3, 6, 2, 8, 4, 1, 10, 5]
[9, 3, 6, 2, 8, 4, 1, 5, 10]
[9, 3, 6, 2, 10, 5, 1, 8, 4]
[9, 3, 6, 2, 10, 5, 1, 4, 8]
[10, 5, 1, 4, 8, 2, 6, 3, 9]
[10, 5, 1, 8, 4, 2, 6, 3, 9]
[10, 5, 1, 9, 3, 6, 2, 8, 4]
[10, 5, 1, 9, 3, 6, 2, 4, 8]
length of longest elementary path is: 11
desired path sequence:
[5, 10, 2, 8, 4, 12, 6, 3, 9, 1, 11]
[5, 10, 2, 8, 4, 12, 6, 3, 9, 1, 13]
[5, 10, 2, 8, 4, 12, 6, 3, 9, 1, 7]
[7, 1, 5, 10, 2, 8, 4, 12, 6, 3, 9]
[7, 1, 9, 3, 6, 12, 4, 8, 2, 10, 5]
[9, 3, 6, 12, 4, 8, 2, 10, 5, 1, 11]
[9, 3, 6, 12, 4, 8, 2, 10, 5, 1, 13]
[9, 3, 6, 12, 4, 8, 2, 10, 5, 1, 7]
[11, 1, 5, 10, 2, 8, 4, 12, 6, 3, 9]
[11, 1, 9, 3, 6, 12, 4, 8, 2, 10, 5]
[13, 1, 5, 10, 2, 8, 4, 12, 6, 3, 9]
[13, 1, 9, 3, 6, 12, 4, 8, 2, 10, 5]
i % j == 0 || j % i == 0.