• # Code lua toto2

    Posté par . En réponse au journal Javascript plus rapide que python ! (une suite possible). Évalué à 5.


    local nb_entries = 10000
    local d = {}
    local i, j

    for i=2,nb_entries/2 do
    for j=2*i,nb_entries,i do
    d[j]= (d[j] or 1) + i
    end
    end

    for i=1,#d do
    if d[i] ~= nil and d[i] < nb_entries and d[d[i]] == i and i <= d[i] then
    print(i,d[i])
    end
    end


    real 0m0.099s
    user 0m0.050s
    sys 0m0.007s

    Ça roxe du ponayz, sachant que l'approche naïve (toto.lua) mettait 21 secondes sur la même machine.

    The capacity of the human mind for swallowing nonsense and spewing it forth in violent and repressive action has never yet been plumbed. -- Robert A. Heinlein