Aller au contenu principal
ShadowAcademy

Les langages / Lua / Operateurs et mathematiques

Bibliothèque math et algorithmes

Lua offre une bibliothèque mathématique complète. Combinée avec des algorithmes efficaces, elle permet de résoudre des problèmes complexes de calcul et de traitement de données.

Fonctions mathématiques de base

-- Fonctions trigonométriques
local angle = math.pi / 4  -- 45 degrés en radians
print("sin(45°):", math.sin(angle))  -- ~0.707
print("cos(45°):", math.cos(angle))  -- ~0.707
print("tan(45°):", math.tan(angle))  -- ~1.0

-- Fonctions exponentielles et logarithmiques
print("exp(1):", math.exp(1))      -- e (~2.718)
print("log(10):", math.log(10))    -- ln(10)
print("log10(100):", math.log10(100))  -- 2

-- Arrondis et valeur absolue
print("floor(3.7):", math.floor(3.7))  -- 3
print("ceil(3.2):", math.ceil(3.2))    -- 4
print("abs(-5):", math.abs(-5))        -- 5

-- Min, max, puissance
print("min(3,7,2):", math.min(3, 7, 2))  -- 2
print("max(3,7,2):", math.max(3, 7, 2))  -- 7
print("pow(2,3):", math.pow(2, 3))       -- 8

Génération de nombres aléatoires

-- Initialiser le générateur
math.randomseed(os.time())

-- Nombre aléatoire entre 0 et 1
local r1 = math.random()
print("Aléatoire [0,1]:", r1)

-- Nombre entier entre 1 et n
local r2 = math.random(10)
print("Aléatoire [1,10]:", r2)

-- Nombre entre min et max
local r3 = math.random(5, 15)
print("Aléatoire [5,15]:", r3)

-- Mélanger un tableau (algorithme de Fisher-Yates)
function melanger(t)
  for i = #t, 2, -1 do
    local j = math.random(i)
    t[i], t[j] = t[j], t[i]
  end
  return t
end

local cartes = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
melanger(cartes)
for _, v in ipairs(cartes) do
  print(v)
end

Algorithmes de tri

-- Tri à bulles (pour l'apprentissage)
function triBulles(t)
  local n = #t
  for i = 1, n do
    for j = 1, n - i do
      if t[j] > t[j + 1] then
        t[j], t[j + 1] = t[j + 1], t[j]
      end
    end
  end
  return t
end

-- Tri rapide (QuickSort)
function triRapide(t, debut, fin)
  debut = debut or 1
  fin = fin or #t
  
  if debut < fin then
    local pivot = t[fin]
    local i = debut - 1
    
    for j = debut, fin - 1 do
      if t[j] <= pivot then
        i = i + 1
        t[i], t[j] = t[j], t[i]
      end
    end
    
    t[i + 1], t[fin] = t[fin], t[i + 1]
    local p = i + 1
    
    triRapide(t, debut, p - 1)
    triRapide(t, p + 1, fin)
  end
  
  return t
end

local nombres = {64, 34, 25, 12, 22, 11, 90}
triRapide(nombres)
for _, v in ipairs(nombres) do
  print(v)
end

Recherche et calculs

-- Recherche binaire (tableau trié)
function rechercheBinaire(t, valeur)
  local debut, fin = 1, #t
  
  while debut <= fin do
    local milieu = math.floor((debut + fin) / 2)
    if t[milieu] == valeur then
      return milieu
    elseif t[milieu] < valeur then
      debut = milieu + 1
    else
      fin = milieu - 1
    end
  end
  
  return nil
end

local nombres = {1, 3, 5, 7, 9, 11, 13, 15}
print(rechercheBinaire(nombres, 7))  -- 4

-- Calcul du PGCD (Plus Grand Commun Diviseur)
function pgcd(a, b)
  while b ~= 0 do
    a, b = b, a % b
  end
  return a
end

print("PGCD(48, 18):", pgcd(48, 18))  -- 6

-- Calcul de factorielle
function factorielle(n)
  if n <= 1 then return 1 end
  return n * factorielle(n - 1)
end

print("5! =", factorielle(5))  -- 120

2 exercices pour cette lecon

Les exercices demandent un compte : il faut bien enregistrer votre progression quelque part. La lecture, elle, reste libre. Creer un compte

  • Math.random moyen 20 XP
  • Fonctions mathématiques moyen 20 XP