Millipedes and earthworms have similar lifestyles.
Both burrow in soil, eating dead and decaying vegetation, but they have evolved very different methods for forcing their way through the soil.
Worms use the strong muscles in their body walls to build up pressure in the body cavity, and so develop the forces needed to push forward or widen a crevice in the soil. Millipedes, however, use their legs to push through the soil.
The more legs the animal has, the harder it can push.
Millipedes are different from centipedes.
They have very large numbers of short legs because long legs would be a liability in a burrow. Centipedes, which spend their time on the surface or among leaf litter, have fewer, longer legs.
They have little need to push, but have to run faster than millipedes.
Millipedes, centipedes, and earthworms all have long, slender bodies, divided into large numbers of segments.
Except at the two ends of the body, all the segments are built to more or less the same design. Similarly, many products of human engineering are built largely from a series of identical modules.
For example, identical seats and windows are repeated many times along the length of a bus. The advantage of this is that one design will serve for all the seats, and one machine can make them all.
In the same way, the repetition of segments in animals reduces the quantity of genetic information needed for development.
Millipedes presumably evolved from an ancestor with fewer segments and correspondingly fewer legs, simply by changes in the genes that specify the number of segments.