© 2022, The Author(s), under exclusive licence to Springer Nature Switzerland AG.Our aim is to construct and compute efficient generating functions enumerating the k-ary Lyndon words having prime number length which arise in many branches of mathematics and computer science. We prove that these generating functions coincide with the Apostol–Bernoulli numbers and their interpolation functions and obtain other forms of these generating functions including not only the Frobenius–Euler numbers, but also the Fubini type numbers. Moreover, we derive some identities, relations and combinatorial sums including the numbers of the k-ary Lyndon words, the Bernoulli numbers and polynomials, the Stirling numbers and falling factorials. Using these generating functions and recurrence relation for the Apostol–Bernoulli numbers, we give two algorithms to compute these generating functions. Using these algorithms, we compute some infinite series formulas including the number of the k-ary Lyndon words on some special classes of primes with the purpose of providing some numerical evaluations about these generating functions. In addition, we approximate these generating functions by the rational functions of the Apostol–Bernoulli numbers to show that the complexity of the aforementioned algorithms may be decreased by means of approximation method which are illustrated by some numerical evaluations with their plots for varying prime numbers. Finally, using Bell polynomials (i.e., exponential functions) approach to the numbers of Lyndon words, we construct the exponential generating functions for the numbers of Lyndon words. Finally, we define a new family of special numbers related to these special words and investigate some of their fundamental properties.