suitescomplet.py

Created by plbt47

Created on September 25, 2024

6.69 KB

permet un grand nombre de calcul sur les suites


from math import log

# Fonctions pour les suites arithmétiques

def calculer_terme_arithmetique():
    print("Calcul du terme U(n) d'une suite arithmétique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    q = float(input("Entrez la raison q : "))
    n = int(input("Entrez le rang n : "))
    
    # Calcul du terme Un
    Un = U0 + q * n
    
    # Affichage des étapes de calcul
    print("\nÉtapes du calcul :")
    print("U(n) = U0 + n * q")
    print("U({}) = {} + {} * {}".format(n, U0, n, q))
    print("U({}) = {} + {}".format(n, U0, n * q))
    print("U({}) = {}\n".format(n, Un))
    
    print("Le terme U{} de la suite arithmétique est : {}".format(n, Un))
    
def somme_arithmetique():
    print("Calcul de la somme des n premiers termes d'une suite arithmétique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    q = float(input("Entrez la raison q : "))
    n = int(input("Entrez le nombre de termes n : "))
    
    # Calcul de la somme Sn
    Sn = n * (2 * U0 + (n - 1) * q) / 2
    
    # Affichage des étapes de calcul
    print("\nÉtapes du calcul :")
    print("S(n) = n * (2 * U0 + (n - 1) * q) / 2")
    print("S({}) = {} * (2 * {} + ({} - 1) * {}) / 2".format(n, n, U0, n, q))
    print("S({}) = {} * ({} + {}) / 2".format(n, n, 2 * U0, (n - 1) * q))
    print("S({}) = {}\n".format(n, Sn))
    
    print("La somme des {} premiers termes de la suite arithmétique est : {}".format(n, Sn))

# Fonctions pour les suites géométriques

def calculer_terme_geometrique():
    print("Calcul du terme U(n) d'une suite géométrique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    r = float(input("Entrez la raison r : "))
    n = int(input("Entrez le rang n : "))
    
    # Calcul du terme Un
    Un = U0 * r ** n
    
    # Affichage des étapes de calcul
    print("\nÉtapes du calcul :")
    print("U(n) = U0 * r^n")
    print("U({}) = {} * {}^{}".format(n, U0, r, n))
    print("U({}) = {} * {}".format(n, U0, r**n))
    print("U({}) = {}\n".format(n, Un))
    
    print("Le terme U{} de la suite géométrique est : {}".format(n, Un))
    
def somme_geometrique():
    print("Calcul de la somme des n premiers termes d'une suite géométrique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    r = float(input("Entrez la raison r : "))
    n = int(input("Entrez le nombre de termes n : "))
    
    # Calcul de la somme Sn
    if r == 1:
        Sn = U0 * n
        print("\nPuisque r = 1, la suite est constante et S(n) = U0 * n")
        print("S({}) = {} * {} = {}\n".format(n, U0, n, Sn))
    else:
        Sn = U0 * (1 - r ** n) / (1 - r)
        
        # Affichage des étapes de calcul
        print("\nÉtapes du calcul :")
        print("S(n) = U0 * (1 - r^n) / (1 - r)")
        print("S({}) = {} * (1 - {}^{}) / (1 - {})".format(n, U0, r, n, r))
        print("S({}) = {} * (1 - {}) / (1 - {})".format(n, U0, r**n, r))
        print("S({}) = {}\n".format(n, Sn))
    
    print("La somme des {} premiers termes de la suite géométrique est : {}".format(n, Sn))

# Résolution d'inéquations pour une suite arithmétique ou géométrique

def inequation_arithmetique():
    print("Résolution d'une inéquation dans une suite arithmétique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    q = float(input("Entrez la raison q : "))
    objectif = float(input("Entrez la valeur à atteindre ou dépasser (par exemple 200) : "))
    
    # Résolution de l'inéquation U(n) >= objectif
    if q == 0:
        print("La raison q est nulle, la suite est constante.")
        if U0 >= objectif:
            print("Le premier terme U0 = {} est déjà supérieur ou égal à {}.".format(U0, objectif))
        else:
            print("Le terme U0 = {} ne peut jamais atteindre {}.".format(U0, objectif))
    else:
        n = (objectif - U0) / q
        n_rounded = ceil(n)  # On arrondit à l'entier supérieur
        
        print("\nÉtapes du calcul :")
        print("U(n) = U0 + n * q >= {}".format(objectif))
        print("{} + n * {} >= {}".format(U0, q, objectif))
        print("n >= ({} - {}) / {}".format(objectif, U0, q))
        print("n >= {}".format(n))
        print("n doit être supérieur ou égal à {}\n".format(n_rounded))
        
        print("Le premier terme U(n) qui est supérieur ou égal à {} correspond à n = {}.".format(objectif, n_rounded))

def inequation_geometrique():
    print("Résolution d'une inéquation dans une suite géométrique")
    U0 = float(input("Entrez U0 (premier terme) : "))
    r = float(input("Entrez la raison r : "))
    objectif = float(input("Entrez la valeur à atteindre ou dépasser (par exemple 200) : "))
    
    # Résolution de l'inéquation U(n) >= objectif
    if r <= 0:
        print("La raison r doit être positive.")
    elif r == 1:
        if U0 >= objectif:
            print("Le premier terme U0 = {} est déjà supérieur ou égal à {}.".format(U0, objectif))
        else:
            print("Le terme U0 = {} ne peut jamais atteindre {}.".format(U0, objectif))
    else:
        n = log(objectif / U0) / log(r)
        n_rounded = ceil(n)  # On arrondit à l'entier supérieur
        
        print("\nÉtapes du calcul :")
        print("U(n) = U0 * r^n >= {}".format(objectif))
        print("{} * {}^n >= {}".format(U0, r, objectif))
        print("n >= log({} / {}) / log({})".format(objectif, U0, r))
        print("n >= {}".format(n))
        print("n doit être supérieur ou égal à {}\n".format(n_rounded))
        
        print("Le premier terme U(n) qui est supérieur ou égal à {} correspond à n = {}.".format(objectif, n_rounded))

# Menu principal

def menu():
    while True:
        print("\n=== Menu Principal ===")
        print("1. Calculer un terme d'une suite arithmétique")
        print("2. Calculer un terme d'une suite géométrique")
        print("3. Calculer la somme des n premiers termes d'une suite arithmétique")
        print("4. Calculer la somme des n premiers termes d'une suite géométrique")
        print("5. Résoudre une inéquation dans une suite arithmétique")
        print("6. Résoudre une inéquation dans une suite géométrique")
        print("0. Quitter")
        
        choix = input("Choisissez une option : ")

        if choix == '1':
            calculer_terme_arithmetique()
        elif choix == '2':
            calculer_terme_geometrique()
        elif choix == '3':
            somme_arithmetique()
        elif choix == '4':
            somme_geometrique()
        elif choix == '5':
            inequation_arithmetique()
        elif choix == '6':
            inequation_geometrique()
        elif choix == '0':
            print("Au revoir !")
            break
        else:
            print("Choix invalide, veuillez réessayer.")

# Lancer le programme
menu()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.